第四周课程总结&实验报告二

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

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

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

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

public class Rectangle {
private double height;
private double width;
private String color;
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Rectangle(double width,double height,String color){
this.setColor(color);
this.setHeight(height);
this.setWidth(width);
}
public void getArea(){
double area=0;
area=this.height*this.width;
System.out.println("矩形的面积为"+area);
}
public String toString(){
String recStr="矩形的高度:"+this.getHeight()+"宽度:"+this.getWidth()
+"颜色:"+this.getColor();
return recStr;
}
/**
* 测试函数
* @param args
*/
public static void main(String[] args) {
Rectangle rec=new Rectangle(3, 4, "红色");
rec.getArea();
System.out.println(rec.toString());
}
}
  1. 银行的账户记录Account有账户的唯一性标识(11个长度的字符和数字的组合),用户的姓名,开户日期,账户密码(六位的数字,可以用0开头),当前的余额。银行规定新开一个账户时,银行方面提供一个标识符、账户初始密码123456,客户提供姓名,开户时客户可以直接存入一笔初始账户金额,不提供时初始余额为0。定义该类,并要求该类提供如下方法:存款、取款、变更密码、可以分别查询账户的标识、姓名、开户日期、当前余额等信息。
    import java.util.Date;
    import java.util.Scanner;
    public class Bank{
    private String id;
    private String name;
    private Date createTime;
    private String mima;
    private int balance;
    public amount(Sring id,String name,int balance){
    }
    super();
    this.id=id;
    this.name=name;
    this.balance=balance;
    this.createTime=new Date();
    this.mima=123456;
    }
        public void deposit(int amount){
    this.balance+=amount;
    }
    public void withdraw(int amount){
    this.balance-=amount;
    }
    public Date getCreateTime()
    {
    return createTime;
    }
    public void setCreateTime(Date createTime){
    this.createTime=createTime;
    }
    public void changemima(){
    Scanner sc=new Scanner(System.in);
    System.out.println(请输入新的密码:);
    String mima=sc.nextString();
    this.mima=new mima;
    }
    public static void main(String[] args){
    Bank a=new Bank(1234567890,luxi,1000);
    a.deposit(100);
    a.withdraw(150);
    a.changemima();
    a.createTime();
    System.out.println(账户账户:+a.etld()):
    System.out.println(账户姓名:+a.getName());
    System.out.println(账户日期:+a.getCreateTime());
    System.out.println(账户余额:+a.getAmount()):
    }
    }

    总结:这周学习了String类对象和this关键字,main方法等。这些内容还是好理解的,掌握的还不错。这周的实验还是有一点难度的,首先代码行数比较多,然后也比较复杂,算法上想了很久。

猜你喜欢

转载自www.cnblogs.com/zcl666/p/11548199.html