第四周Java实验总结二
1.写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。要求该类具有:
(1) 使用构造函数完成各属性的初始赋值
(2) 使用get…()和set…()的形式完成属性的访问及修改
(3) 提供计算面积的getArea()方法和计算周长的getLength()方法
Rectangle
1 package cn.work.day0917.demo01; 2 3 public class Rectangle { 4 5 private double width; 6 private double height; 7 private String color; 8 9 Rectangle(double width, double height, String color) { 10 this.width = width; 11 this.height = height; 12 this.color = color; 13 } 14 15 public void setColor(String color) { 16 this.color = color; 17 } 18 19 public void setWidth(double width){ 20 this.width=width; 21 } 22 23 public void setHeight(double height){ 24 this.height=height; 25 } 26 27 public String getColor(){ 28 return color; 29 } 30 31 public double getWidth() { 32 return width; 33 } 34 35 public double getHeight() { 36 return height; 37 } 38 39 public double getArea(){ 40 return width*height; 41 } 42 43 public double getLength(){ 44 return (width*height)*2; 45 } 46 } 47 48 package cn.work.day0917.demo01; 49 50 public class Demo01 { 51 52 public static void main(String[] args) { 53 Rectangle rectangle=new Rectangle(10.0,20.0,"yellow"); 54 System.out.println("矩形的面积是:"+rectangle.getArea()); 55 System.out.println("矩形的周长是:"+rectangle.getLength()); 56 } 57 }
2.银行的账户记录Account有账户的唯一性标识(11个长度的字符和数字的组合),用户的姓名,开户日期,账户密码(六位的数字,可以用0开头),当前的余额。银行规定新开一个账户时,银行方面提供一个标识符、账户初始密码123456,客户提供姓名,开户时客户可以直接存入一笔初始账户金额,不提供时初始余额为0。定义该类,并要求该类提供如下方法:存款、取款、变更密码、可以分别查询账户的标识、姓名、开户日期、当前余额等信息。
Bank
1 public class Bank { 2 private String aco; 3 private String name; 4 private String time; 5 private double num; 6 private int[] password; 7 8 public Bank(String aco, String name, String time, double num, int[] password) { 9 this.aco = aco; 10 this.name = name; 11 this.time = time; 12 this.num = num; 13 this.password = password; 14 } 15 16 public String getAco() { 17 return aco; 18 } 19 20 public void setAco(String aco) { 21 this.aco = aco; 22 } 23 24 public String getName() { 25 return name; 26 } 27 28 public void setName(String name) { 29 this.name = name; 30 } 31 32 public String getTime() { 33 return time; 34 } 35 36 public void setTime(String time) { 37 this.time = time; 38 } 39 40 public double getNum() { 41 return num; 42 } 43 44 public void setNum(double num) { 45 this.num = num; 46 } 47 48 public int[] getPassword() { 49 return password; 50 } 51 52 public void setPassword(int[] password) { 53 this.password = password; 54 } 55 56 57 public void Cun(double num){ 58 this.num=this.num+num; 59 } 60 61 public void Qu(double num){ 62 this.num=this.num-num; 63 } 64 65 66 public String kaiHuTime(){ 67 //getInstance() 68 //使用默认时区和语言环境获得一个日历。 69 //getTime() 70 //返回一个表示此 Calendar 时间值(从历元至现在的毫秒偏移量)的 Date 对象。 71 Date currDate= Calendar.getInstance().getTime(); 72 SimpleDate sdf=new SimpleDate("yyyy-MM-dd"); 73 this.time=sdf.format(currDate); 74 return time; 75 } 76 77 }
总结:多去刷题,在做题的时候,认真去审题。