2019秋JAVA第三周课程总结及实验报告(二)
实验二 Java简单类与对象
实验内容:
1.写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。要求该类具有:
(1) 使用构造函数完成各属性的初始赋值
(2) 使用get…()和set…()的形式完成属性的访问及修改
(3) 提供计算面积的getArea()方法和计算周长的getLength()方法
2.银行的账户记录Account有账户的唯一性标识(11个长度的字符和数字的组合),用户的姓名,开户日期,账户密码(六位的数字,可以用0开头),当前的余额。银行规定新开一个账户时,银行方面提供一个标识符、账户初始密码123456,客户提供姓名,开户时客户可以直接存入一笔初始账户金额,不提供时初始余额为0。定义该类,并要求该类提供如下方法:存款、取款、变更密码、可以分别查询账户的标识、姓名、开户日期、当前余额等信息。
Rectangle.java
1 import java.util.*; 2 public class Rectangle { 3 private double width, height; 4 private String color; 5 6 Rectangle(double width, double height, String color){ 7 this.width = width; 8 this.height = height; 9 this.color = color; 10 } 11 12 public void setWidth(double width) { 13 this.width = width; 14 } 15 16 public void setHeight(double height) { 17 this.height = height; 18 } 19 20 public void setColor(String color) { 21 this.color = color; 22 } 23 24 public double getWidth() { 25 return width; 26 } 27 28 public double getHeight() { 29 return height; 30 } 31 32 public String getColor() { 33 return color; 34 } 35 36 public double getArea(){ 37 return this.height*this.width; 38 } 39 40 public double getPerimeter(){ 41 return (this.height+this.width)*2; 42 } 43 44 45 }
Bank.java
1 import java.util.*; 2 3 public class Bank { 4 private String account; 5 private String name, password; 6 private long date; 7 private double balance; 8 private Base64.Encoder encoder = Base64.getEncoder(); 9 10 Bank(String name){ 11 this.date = System.currentTimeMillis(); 12 this.account = encoder.encodeToString(String.valueOf(date).getBytes()); 13 this.name = name; 14 this.password = "123456"; 15 this.balance = 0.0; 16 } 17 18 Bank(double balance,String name){ 19 this.date = System.currentTimeMillis(); 20 this.name = name; 21 this.balance = balance; 22 this.password = "123456"; 23 } 24 25 public String getAccount() { 26 return account; 27 } 28 29 public String getName() { 30 return name; 31 } 32 33 public long getDate() { 34 return date; 35 } 36 37 public String getPassword() { 38 return password; 39 } 40 41 public double getBalance() { 42 return balance; 43 } 44 45 public void setPassword(String password) { 46 this.password = password; 47 } 48 49 public void deposit(double num){ 50 this.balance += num; 51 } 52 53 public void withdraw(double num){ 54 this.balance -= num; 55 } 56 57 58 }
总结
本次实验内容为类的基本操作,银行账户的唯一性标识采用base64编码,其余部分仔细审题即可。

浙公网安备 33010602011771号