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

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

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

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

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

```

package shiyanbaogao2;

public class Rectangle {

public static void main(String[] args) {
Rectangle rec=new Rectangle();
rec.setWidth(4);
rec.setHeight(6);
rec.setColor("黄色");
rec.getArea();
rec.getLength();
System.out.println("长:"+rec.getWidth()+"\n高:"+rec.getHeight()+"\n颜色:"+rec.getColor());
}

double width,height;
String color="red";
public double getHeight() {
return height;
}
public double getWidth() {
return width;
}
public String getColor() {
return color;
}
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 void getArea() {
double area=0;
area=this.height*this.width;
System.out.println("面积为"+area);
}
public void getLength() {
double length=0;
length=(this.height+this.width)*2;
System.out.println("周长为"+length);

}

```

 

 

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

```

package shiyanbaogao2;

public class dob2 {

public static void main(String[] args) {
Account re = new Account("bingo","ldh",2019,9,20,"123456",2000);
re.setAll();
re.changebalance(100);
re.setAll();
re.changepsd("123456");
re.setAll();
}
}

class Account {
String ID;
String psd;
String name;
double balance;
int month,day,year;

Account() {
this.ID="binggo";
this.name="null";
this.year=0;
this.month=0;
this.day=0;
this.psd="null";
this.balance=0;
}

Account(String ID, String name, int year, int month, int day, String psd, double balance) {
this.ID = ID;
this.name = name;
this.year = year;
this.month = month;
this.day = day;
this.psd = psd;
this.balance = balance;
}
public void changebalance(double single) {
this.balance += single;
}

public void changepsd(String single){
this.psd = single;
}
public void setAll(){
System.out.println(this.ID);
System.out.println(this.name);
System.out.println(this.year+"."+this.month+"."+this.day);
System.out.println("balance:"+this.balance);
}

}

```

posted @ 2019-10-23 16:04  宣文  阅读(69)  评论(0编辑  收藏  举报