第四周课程总结和实验报告
一、写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。要求该类具有:
(1) 使用构造函数完成各属性的初始赋值
(2) 使用get…()和set…()的形式完成属性的访问及修改
(3) 提供计算面积的getArea()方法和计算周长的getLength()方法 代码如下
public class Rectangle { private double width; private double height; private String color; public void tell() { System.out.print("面积是"+(width*height)+"\n"+"周长是"+2*(width+height)+"\n"); System.out.println("高为"+height); System.out.println("宽为"+width); System.out.println("颜色为"+color); } public String getColor(){ return color; } public void setColor(String n){ color=n; } public double getWidth(){ return width; } public void setWidth(double a){ width=a; } public double getHeight() { return height; } public void setHeight(double b) { height=b; } } public class fff{ public static void main(String arge[]) { Rectangle r1=new Rectangle(); a1.setColor("黄色"); a1.setHeight(20); a1.setWidth(10); a1.tell(); } }二、银行的账户记录Account有账户的唯一性标识(11个长度的字符和数字的组合),用户的姓名,开户日期,账户密码(六位的数字,可以用0开头),当前的余额。银行规定新开一个账户时,银行方面提供一个标识符、账户初始密码123456,客户提供姓名,开户时客户可以直接存入一笔初始账户金额,不提供时初始余额为0。定义该类,并要求该类提供如下方法:存款、取款、变更密码、可以分别查询账户的标识、姓名、开户日期、当前余额等信息。
import java.util.Scanner; public class Account { public int id; public int password; public String name; public int money; public Account(int id, int password, String name, int money) { this.id = id; this.password = password; this.name = name; this.money = money; } // 有一个方法show(),显示账户的账号、姓名和余额信息 public void show(){ System.out.println("账户:" + id); System.out.println("姓名:" + name); System.out.println("余额:" + money); } /*有一个取款方法 takeMoney(),先让用户输入密码验证, 密码正确后输入取款金额,取款成功后余额减除相应的金额*/ public void takeMoney(){ while(true){ Scanner sc = new Scanner(System.in); System.out.println("请输入密码进行验证!"); int pass = sc.nextInt(); if(pass == password){ System.out.println("请输入需要取款的金额:"); int withdrawals = sc.nextInt(); if(withdrawals <= money) { money= money-withdrawals; System.out.println("余额为:" + money); }else { System.out.println("当前余额不足" ); } break; }else{ System.out.println("你输入的密码有误,请重新输入!"); } } } // 有一个存款方法saveMoney(int money),存款是直接传入存款金额,账户余额增加相应的金额 public void saveMoney(int moneys){ money = money+moneys; System.out.println("此次存款为:" + moneys); System.out.println("账户余额为:" + money); } public static void main(String[] args) { Account acc = new Account(10010,123456,"阿桦",100000); /* acc.id = 10010; acc.name = "阿桦"; acc.money = 100000; acc.password = 123456; */ Scanner sc = new Scanner(System.in); System.out.println("请输入需要执行的操作"); System.out.println("***1银行账户信息***"); System.out.println("***2取款操作***"); System.out.println("***3存款操作***"); System.out.println("***4退出系统***"); int s = sc.nextInt(); switch(s) { case 1: System.out.println("***银行账户信息***"); acc.show(); break; case 2: System.out.println("***取款操作***"); acc.takeMoney(); break; case 3: System.out.println("***存款操作***"); acc.saveMoney(1000); break; case 4: System.exit(0); break; } } }总结; 通过学习,使用构造函数完成各属性的初始赋值与String类的两种形式学习。 第一题比较简单,第二题询问了室友。
转载于:https://www.cnblogs.com/c11c11cyy/p/11559837.html
相关资源:吴恩达深度学习deeplearning第四课第四周课后测验及编程作业(含答案)