第四周课程总结&试验报告(二)

mac2022-06-30  31

第四周课程总结

1.String类 (1)实例化String类对象:一、String可以直接采用赋值的形式进行处理,这一点感觉上和基本数据类型是非常相似的;二:使用标准的new调用构造方法完成实例化。 (2)使用equals()方法对String的内容进行比较,以及使用“==”比较字符串内容的区别。 (3)字符串内容不可改变。 (4)String类中常用方法:P110~P111。 2.学习总结: 实验题第一题自己还能较快得解决,但是第二题就心有余而力不足了。在写第二题的时候参考了其他同学的代码用以解决自己遇到的一些问题,例如:日期显示等。但是自己的代码依旧不够完善,程序在修改了密码后终止运行了,暂时没有找到解决的办法。

试验报告(二)

1.写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。要求该类具有:

(1) 使用构造函数完成各属性的初始赋值。

(2) 使用get…()和set…()的形式完成属性的访问及修改。

(3) 提供计算面积的getArea()方法和计算周长的getLength()方法。

源码1:

package shiyan2; public class Rectangle { private double w; private double h; private String c; public Rectangle(){ } public Rectangle(double w,double h,String c){ this.setW(w); this.setH(h); this.setC(c); } public void setW(double i) { w=i; } public void setH(double j) { h=j; } public void setC(String k) { c=k; } public double getW() { return w; } public double getH() { return h; } public String getC() { return c; } public double getArea(){ return getW()*getH(); } public double getLength(){ return 2*getW()+2*getH(); } public static void main(String [] args){ Rectangle R=new Rectangle(11.f,11.f,"red"); System.out.println("Color:"+R.c); System.out.println("Area:"+R.getArea()); System.out.println("Length:"+R.getLength()); } }

截图1:

2.银行的账户记录Account有账户的唯一性标识(11个长度的字符和数字的组合),用户的姓名,开户日期,账户密码(六位的数字,可以用0开头),当前的余额。银行规定新开一个账户时,银行方面提供一个标识符、账户初始密码123456,客户提供姓名,开户时客户可以直接存入一笔初始账户金额,不提供时初始余额为0。定义该类,并要求该类提供如下方法:存款、取款、变更密码、可以分别查询账户的标识、姓名、开户日期、当前余额等信息。

源码2:

package shiyan2; import java.util.Scanner; import java.util.Date; class Account { private String accId; private String name; private double money; String code; private Date date; public Account(String accId, String name){ this(accId, name, 0); } private Account(String accId, String name, double money) { this.accId = accId; this.name = name; this.money = money; this.code="123456"; this.date=new Date(); } public void saveMoney(double money) { if (money <= 0) { System.out.println("存款金额必须大于0"); } this.money += money; System.out.println("存款成功"+"\n"+"存入:"+money+"元"); } public double getMoney(double money) { if (money <= 0) { System.out.println("取款金额必须大于0"); return 0; } if (this.money <= money) { System.out.println("余额不足,无法取款"); return 0; } this.money -= money; System.out.println("取款成功"+"\n"+"取出:"+money+"元"); return money; } public String alter(String code){ this.code=code; System.out.println("修改成功"); return code; } public double getBalance() { return this.money; } public double getOverdraft() { return 0; } public void tell(){ System.out.println("账户:"+accId+"\n"+"姓名:"+name+"\n"+"开户日期"+date+"\n"+"余额:"+money+"\n\n\n"); } public void tell1(){ System.out.println("账户:"+accId); } public void tell2(){ System.out.println("姓名:"+name); } public void tell3(){ System.out.println("余额:"+money+"元"); } } class Bank{ public static void main(String[] args){ System.out.println("请输入开户人姓名"); @SuppressWarnings("resource") Scanner m=new Scanner(System.in); String v=m.next(); Account per=new Account("98765432101",v); per.tell(); System.out.println("请输入密码"); @SuppressWarnings("resource") Scanner x=new Scanner(System.in); String chr=x.next(); while(chr.equals(per.code)) { System.out.println("1.取款\n2.存款\n3.变更密码\n4.账户标识\n5.姓名\n6.当前余额\n7.退出"+"\n\n"); @SuppressWarnings("resource") Scanner n=new Scanner(System.in); int t=n.nextInt(); if(t>6) { System.out.println("结束操作"); break; } switch(t){ case 1: System.out.println("请输入取款金额"); Scanner a=new Scanner(System.in); double y=a.nextDouble(); per.getMoney(y); break; case 2: System.out.println("请放入存款金额"); Scanner b=new Scanner(System.in); double s=b.nextDouble(); per.saveMoney(s); break; case 3: Scanner z=new Scanner(System.in); String c=z.next(); per.alter(c); break; case 4: per.tell1(); break; case 5: per.tell2(); break; case 6: per.tell3(); break; } } } }

截图2:

转载于:https://www.cnblogs.com/leichen210/p/11558751.html

相关资源:微信小程序源码-合集4.rar
最新回复(0)