Java实验报告
班级计科二班 学号20188437 姓名何磊
完成时间2019/9/12
评分等级
实验二 Java简单类与对象
实验目的
掌握类的定义,熟悉属性、构造函数、方法的作用,掌握用类作为类型声明变量和方法返回值;
理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性;
理解static修饰付对类、类成员变量及类方法的影响。
实验内容
写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。要求该类具有:
(1) 使用构造函数完成各属性的初始赋值
(2) 使用get…()和set…()的形式完成属性的访问及修改
(3) 提供计算面积的getArea()方法和计算周长的getLength()方法
银行的账户记录Account有账户的唯一性标识(11个长度的字符和数字的组合),用户的姓名,开户日期,账户密码(六位的数字,可以用0开头),当前的余额。银行规定新开一个账户时,银行方面提供一个标识符、账户初始密码123456,客户提供姓名,开户时客户可以直接存入一笔初始账户金额,不提供时初始余额为0。定义该类,并要求该类提供如下方法:存款、取款、变更密码、可以分别查询账户的标识、姓名、开户日期、当前余额等信息。
实验过程
(请自己调整格式)
1.
(一)实验代码:
packageRectangle表示矩形;
publicclassRectangle {
doublewidth,height,area,length;
String color;
publicRectangle(doublewidth,doubleheight,String color){
this.setWidth(width);
this.setHeight(height);
this.setColor(color);
this.setArea(width,height);
this.setLength(width,height);
}
publicvoidtell(){
System.out.println("宽:"+getWidth()+",高:"+getHeight()+",颜色:"+getColor()+",面积:"+getArea()+",周长:"+getLength());
}
publicdoublegetWidth() {
returnwidth;
}
publicvoidsetWidth(doublewidth) {
this.width= width;
}
publicdoublegetHeight() {
returnheight;
}
publicvoidsetHeight(doubleheight) {
this.height= height;
}
publicString getColor() {
returncolor;
}
publicvoidsetColor(String color) {
this.color= color;
}
publicdoublegetArea(){
returnarea;
}
publicvoidsetArea(doublewidth,doubleheight){
this.area=width*height;
}
publicdoublegetLength(){
returnlength;
}
publicvoidsetLength(doublewidth,doubleheight){
this.length=2*(width+height);
}
}
publicclass调用Rectangle类{
publicstaticvoidmain(String[] args) {
Rectangle rec=newRectangle(15,20,"red");
rec.tell();
}
}
(二)运行结果
2.
(一)实验代码:
package银行系统;importjava.util.Scanner;importjava.util.Date;classchuli {private String user,password,name="none",date;private doublebalance;public chuli(String password,doublebalance){this.setPassword(password);this.setBalance(balance);
}publicString getUser() {returnuser;
}public voidsetUser(String user) {this.user =user;
}publicString getPassword() {returnpassword;
}public voidsetPassword(String password) {this.password =password;
}publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}public doublegetBalance() {returnbalance;
}public void setBalance(doublebalance) {this.balance =balance;
}publicString getDate() {returndate;
}public voidsetDate(String date) {this.date =date;
}
}public classbank {static chuli per[]=new chuli[100];static int i=-1;static int id=0;staticString s;public static voidmain(String[] args) {//TODO Auto-generated method stub
for(int j=0;j<100;j++){
per[j]=new chuli("123456",0.0);
}
zhuyemian();
}public static void yewu(inti) {//TODO Auto-generated method stub
System.out.println("1:存款 2:取款 3:变更密码 4:查询账户标识 5:查询姓名 6:查询开户日期 7:当前余额 0:返回");
Scanner input=newScanner(System.in);switch(input.nextInt()){case 1:System.out.println("存款金额:");
per[i].setBalance(input.nextDouble()+per[i].getBalance());
System.out.println("存款成功!");yewu(i);case 2:System.out.println("取款金额:");if(per[i].getBalance()-input.nextDouble()<0){
System.out.println("余额不足!");yewu(i);
}else{
per[i].setBalance(per[i].getBalance()-input.nextDouble());
System.out.println("取款成功!");yewu(i);
}case 3:System.out.println("请输入新密码:");
per[i].setPassword(input.next());
System.out.println("修改成功!");yewu(i);case 4:System.out.println("您的账户标识为:"+per[i].getUser());yewu(i);case 5:System.out.println("您的姓名为:"+per[i].getName());yewu(i);case 6:System.out.println("您的开户日期为:"+per[i].getDate());yewu(i);case 7:System.out.println("您的余额为:"+per[i].getBalance());yewu(i);case 0:zhuyemian();
}
}public static voiddenglu() {//TODO Auto-generated method stub
System.out.println("请输入您的姓名:");
Scanner input=newScanner(System.in);
s=input.next();
i=-1;while(i<99){
i++;if(s.equals(per[i].getName())){
System.out.println("请输入您的密码:");if(input.next().equals(per[i].getPassword())){
System.out.println("登陆成功!");
yewu(i);
}else{
System.out.println("姓名或密码有误,请重新输入");
denglu();
}
}
}
System.out.println("没有此用户!");
zhuyemian();
}public static voidkaihu() {//TODO Auto-generated method stub
per[id]=new chuli("123456",0.0);if(id>=100){
s="uniqueid"+id;
per[id].setUser(s);
}else if(id>=10){
s="uniqueid0"+id;
per[id].setUser(s);
}else{
s="uniqueid00"+id;
per[id].setUser(s);
}
Date date=newDate();
per[id].setDate(date.toString());
System.out.println("请输入您的姓名:");
Scanner input=newScanner(System.in);
per[id].setName(input.next());
System.out.println("开户成功,您的初始密码为123456,请尽快更改!");
id++;
denglu();
}public static voidzhuyemian() {//TODO Auto-generated method stub
System.out.println("1:开户 2:业务办理");
Scanner input=newScanner(System.in);
s=input.next();if(s.equals("1")){
kaihu();
}if(s.equals("2")){
denglu();
}
}
}
(二)运行结果
1:开户2:业务办理
2
请输入您的姓名:
何磊
没有此用户!
1:开户2:业务办理
1
请输入您的姓名:
何磊
开户成功,您的初始密码为123456,请尽快更改!
请输入您的姓名:
何磊
请输入您的密码:
123456
登陆成功!
1:存款2:取款3:变更密码4:查询账户标识5:查询姓名6:查询开户日期7:当前余额0:返回
3
请输入新密码:
654321
修改成功!
1:存款2:取款3:变更密码4:查询账户标识5:查询姓名6:查询开户日期7:当前余额0:返回
0
1:开户2:业务办理
2
请输入您的姓名:
何磊
请输入您的密码:
123456
姓名或密码有误,请重新输入
请输入您的姓名:
何磊
请输入您的密码:
654321
登陆成功!
1:存款2:取款3:变更密码4:查询账户标识5:查询姓名6:查询开户日期7:当前余额0:返回
7
您的余额为:0.0
1:存款2:取款3:变更密码4:查询账户标识5:查询姓名6:查询开户日期7:当前余额0:返回
6
您的开户日期为:Sat Sep 14 13:22:58 CST 2019
1:存款2:取款3:变更密码4:查询账户标识5:查询姓名6:查询开户日期7:当前余额0:返回
5
您的姓名为:何磊
1:存款2:取款3:变更密码4:查询账户标识5:查询姓名6:查询开户日期7:当前余额0:返回
4
您的账户标识为:uniqueid000
1:存款2:取款3:变更密码4:查询账户标识5:查询姓名6:查询开户日期7:当前余额0:返回
1
存款金额:
10
存款成功!
1:存款2:取款3:变更密码4:查询账户标识5:查询姓名6:查询开户日期7:当前余额0:返回
7
您的余额为:10.0
1:存款2:取款3:变更密码4:查询账户标识5:查询姓名6:查询开户日期7:当前余额0:返回
2
取款金额:
5
取款成功!
1:存款2:取款3:变更密码4:查询账户标识5:查询姓名6:查询开户日期7:当前余额0:返回
7
您的余额为:5.0
1:存款2:取款3:变更密码4:查询账户标识5:查询姓名6:查询开户日期7:当前余额0:返回
2
取款金额:
6
余额不足!
1:存款2:取款3:变更密码4:查询账户标识5:查询姓名6:查询开户日期7:当前余额0:返回
0
1:开户2:业务办理
1
请输入您的姓名:
李代传
开户成功,您的初始密码为123456,请尽快更改!
请输入您的姓名:
李代传
请输入您的密码:
123456
登陆成功!
1:存款2:取款3:变更密码4:查询账户标识5:查询姓名6:查询开户日期7:当前余额0:返回
7
您的余额为:0.0
1:存款2:取款3:变更密码4:查询账户标识5:查询姓名6:查询开户日期7:当前余额0:返回
6
您的开户日期为:Sat Sep 14 13:24:45 CST 2019
1:存款2:取款3:变更密码4:查询账户标识5:查询姓名6:查询开户日期7:当前余额0:返回
5
您的姓名为:李代传
1:存款2:取款3:变更密码4:查询账户标识5:查询姓名6:查询开户日期7:当前余额0:返回
4
您的账户标识为:uniqueid001
1:存款2:取款3:变更密码4:查询账户标识5:查询姓名6:查询开户日期7:当前余额0:返回
总结:
这一次的java实验教上一周难度有所提升,但也只是新知识的基础应用,完成的还算顺利吧。
第二题的想法是运用对象数组实现用户信息的储存,用主页面方法、开户办法、业务办理方法、登陆方法这几个主要方法,业务办理方法中实现各种要求的功能。Date方法获取日期来自百度。
第二题遇到的主要问题是没有将对象数组实例化导致程序运行异常中断。
本周学习了String类、this关键字、static关键字、对象数组、包。内容很多, 需要课余复习消化。