1、设计一个BankAccount类,实现银行某账号的资金往来账目管理,包括建账号、存入、取出等。BankAccount类包括,账号(BankAccountId)、开户日期Date(日期),Rest(余额)。另有三个成员函数Bankin()(处理存入账),Bankout()处理取出账。
package EveryDay ;
public class BankAccount {
private String bankAccountId;
private String username;
private String password;
private String name;
private String tel;
private String data;
private double rest;
public String getBankAccountId ( ) {
return bankAccountId;
}
public void setBankAccountId ( String bankAccountId) {
this . bankAccountId = bankAccountId;
}
public String getUsername ( ) {
return username;
}
public void setUsername ( String username) {
this . username = username;
}
public String getPassword ( ) {
return password;
}
public void setPassword ( String password) {
this . password = password;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public String getTel ( ) {
return tel;
}
public void setTel ( String tel) {
this . tel = tel;
}
public String getData ( ) {
return data;
}
public void setData ( String data) {
this . data = data;
}
public double getRest ( ) {
return rest;
}
public void setRest ( double rest) {
this . rest = rest;
}
public BankAccount ( ) {
}
public BankAccount ( String bankAccountId, String username, String password, String name, String tel, String data, double rest) {
this . bankAccountId = bankAccountId;
this . username = username;
this . password = password;
this . name = name;
this . tel = tel;
this . data = data;
this . rest = rest;
}
public void bankIn ( double money) {
rest += money;
}
public void bankOut ( double money) {
if ( rest < money) {
System . out. println ( "余额不足!" ) ;
return ;
}
rest -= money;
}
}
package EveryDay ;
public class BankAccountTest {
public static void main ( String [ ] args) {
BankAccount account = new BankAccount ( ) ;
account. setBankAccountId ( "16878166666" ) ;
account. setData ( "2022-08-25" ) ;
account. setRest ( 1000 ) ;
account. setName ( "0123" ) ;
account. setTel ( "18609761563" ) ;
account. setUsername ( "user" ) ;
account. setPassword ( "12456" ) ;
BankAccount account1 = new BankAccount ( ) ;
System . out. println ( account. getRest ( ) ) ;
account. bankIn ( 1000 ) ;
System . out. println ( account. getRest ( ) ) ;
System . out. println ( account. getRest ( ) ) ;
account. bankOut ( 1000 ) ;
System . out. println ( account. getRest ( ) ) ;
account. bankOut ( 2000 ) ;
System . out. println ( account. getRest ( ) ) ;
}
}
2、编写一个程序,输入N个学生数据,包括学号、姓名、成绩,求学生的成绩总和、平均值、最大值、最小值
package EveryDay ;
public class Data {
private int id;
private String name;
private int score;
public int getId ( ) {
return id;
}
public void setId ( int id) {
this . id = id;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public int getScore ( ) {
return score;
}
public void setScore ( int score) {
this . score = score;
}
}
package EveryDay ;
import java. util. Scanner ;
public class DataTest {
public static void main ( String [ ] args) {
DataTest d = new DataTest ( ) ;
d. score ( ) ;
}
private void score ( ) {
Scanner sr = new Scanner ( System . in) ;
System . out. println ( "请输入学生的个数:" ) ;
int N = sr. nextInt ( ) ;
Student [ ] students = new Student [ N ] ;
for ( int i = 0 ; i < N ; i++ ) {
Student student = new Student ( ) ;
students[ i] = student;
System . out. println ( "请输入学号:" ) ;
int id = sr. nextInt ( ) ;
student. setId ( id) ;
System . out. println ( "请输入姓名:" ) ;
String name = sr. next ( ) ;
student. setName ( name) ;
System . out. println ( "请输入成绩:" ) ;
double score = sr. nextDouble ( ) ;
student. setScore ( ( int ) score) ;
}
double sum = 0 ;
for ( int i = 0 ; i < N ; i++ ) {
sum += students[ i] . getScore ( ) ;
}
double avg;
avg = sum / N ;
double max = students[ 0 ] . getScore ( ) ;
double min = students[ 0 ] . getScore ( ) ;
for ( int i = 0 ; i < N ; i++ ) {
if ( students[ i] . getScore ( ) > max) {
max = students[ i] . getScore ( ) ;
}
if ( students[ i] . getScore ( ) < min) {
min = students[ i] . getScore ( ) ;
}
}
System . out. println ( "学生成绩的总和为:" + sum) ;
System . out. println ( "学生成绩的总平均值:" + avg) ;
System . out. println ( "学生成绩的总最大值:" + max) ;
System . out. println ( "学生成绩的总最小值:" + min) ;
}
}
3、设计一个类Student,该类包括姓名、学号和成绩。设计一个方法,对有5名学生按照成绩从高到低的顺序输出姓名、学号和成绩信息
package EveryDay ;
public class Student {
private String name;
private int id;
private int score;
public Student ( ) {
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public int getId ( ) {
return id;
}
public void setId ( int id) {
this . id = id;
}
public int getScore ( ) {
return score;
}
public void setScore ( int score) {
this . score = score;
}
public Student ( String name, int id, int score) {
show ( ) ;
this . name = name;
this . id = id;
this . score = score;
}
public void show ( ) {
System . out. println ( "id:" + id + ",name=" + name + ",score=" + score) ;
}
public static void sort ( Student [ ] stus) {
Student stu;
for ( int i = 0 ; i < stus. length - 1 ; i++ ) {
for ( int j = 0 ; j < stus. length - i - 1 ; j++ ) {
if ( stus[ j] . score < stus[ j + 1 ] . score) {
stu = stus[ j] ;
stus[ j] = stus[ j + 1 ] ;
stus[ j + 1 ] = stu;
}
}
}
}
}
package EveryDay ;
public class StudentTest {
public static void main ( String [ ] args) {
Student [ ] stus = new Student [ 5 ] ;
stus[ 0 ] = new Student ( "张三" , 01 , 90 ) ;
stus[ 1 ] = new Student ( "李四" , 02 , 78 ) ;
stus[ 2 ] = new Student ( "王五" , 03 , 86 ) ;
stus[ 3 ] = new Student ( "赵六" , 04 , 100 ) ;
stus[ 4 ] = new Student ( "刘七" , 05 , 80 ) ;
Student . sort ( stus) ;
for ( Student stu : stus) {
stu. show ( ) ;
}
}
}