第四周总结与试验
第一题
- 写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。要求该类具有:
(1) 使用构造函数完成各属性的初始赋值
(2) 使用get…()和set…()的形式完成属性的访问及修改
(3) 提供计算面积的getArea()方法和计算周长的getLength()方法.
package test2;
class Rectangle {
private double height;
private double width;
private String color;
public Rectangle(){
}
public Rectangle(double width,double height,String color){
this.setColor(color);
this.setHeight(height);
this.setWidth(width);
}
public void setHeight(double height) {
this.height = height;
}
public void setWidth(double width) {
this.width = width;
}
public void setColor(String color) {
this.color = color;
}
public double getHeight() {
return height;
}
public double getWidth() {
return width;
}
public String getColor() {
return color;
}
public double getArea(){
double area;
area=this.height*this.width;
return area;
}
public double getLength(){
double length;
length=width+height+width+height;
return length;
}
};
public class Demo1 {
public static void main(String args[]) {
Rectangle rec=null;
rec=new Rectangle(3.0f,4.0f, "红色");
System.out.println(rec.getArea());
System.out.println(rec.getLength());
System.out.println("长:"+rec.getHeight());
System.out.println("宽:"+rec.getWidth())
经过修改
2.银行的账户记录Account有账户的唯一性标识(11个长度的字符和数字的组合),用户的姓名,开户日期,账户密码(六位的数字,可以用0开头),当前的余额。银行规定新开一个账户时,银行方面提供一个标识符、账户初始密码123456,客户提供姓名,开户时客户可以直接存入一笔初始账户金额,不提供时初始余额为0。定义该类,并要求该类提供如下方法:存款、取款、变更密码、可以分别查询账户的标识、姓名、开户日期、当前余额等信息。
import java.util.Scanner;
import java.util.Date;
class Account {
private String id;
private String name;
private Date date;
private int password;
private int money;
public Account(String id, String name, int money) {
super();
this.id = id;
this.name = name;
this.money = money;
this.date = new Date();
this.password = 123456;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void put(int num) {
this.money = this.money+num;
}
public void out(int num) {
this.money = this.money-num;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public int getPassword() {
return password;
}
public void setPassword(int password) {
this.password = password;
}
public void Test3() {
Scanner sc=new Scanner(System.in);
int password=sc.nextInt();
this.password = password;
sc.close();
}
}
心得: 弄了两个小时毫无头绪, 最后跟着书本好不容易和大佬的帮助勉强完成。