- 写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。要求该类具有:
(1) 使用构造函数完成各属性的初始赋值
(2) 使用get…()和set…()的形式完成属性的访问及修改
(3) 提供计算面积的getArea()方法和计算周长的getLength()方法
设计:
创建一个长为8cm,宽为4cm,颜色Red的Rectangle的实例,用变量
r1表示,
再创建一个长6cm,宽为3cm,颜色为Green的的Rectangle的实例,用变量
r2表示.
代码源
package Rectangle;
public class Rectangle {
private double width;
private double height;
private String color;
public Rectangle(double width,double height,String color){
this.width=width;
this.height=height;
this.color=color;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double area(){
double area=0;
area=width*height;
return area;
}
public double length(){
double length=0;
length=(width+height)*2;
return length;
}
}
class Student {
public static void main (String[] args){
Rectangle r1=new Rectangle(4.0,8.0,"red");
Rectangle r2=new Rectangle(3.0,6.0,"green");
if(r1.area()>r2.area()){
System.out.println(r1.getHeight()+","+r1.getWidth());
}
else{System.out.println(r2.getHeight()+","+r2.getWidth());
}
System.out.println(r1.area());
System.out.println(r2.area());
System.out.println(r1.length());
System.out.println(r2.length());
}
}
结果图:



- 银行的账户记录Account有账户的唯一性标识(11个长度的字符和数字的组合),用户的姓名,开户日期,账户密码(六位的数字,可以用0开头),当前的余额。银行规定新开一个账户时,银行方面提供一个标识符、账户初始密码123456,客户提供姓名,开户时客户可以直接存入一笔初始账户金额,不提供时初始余额为0。定义该类,并要求该类提供如下方法:存款、取款、变更密码、可以分别查询账户的标识、姓名、开户日期、当前余额等信息。
代码源:
package Bank;
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 void amount (String id,String name,int balance)
{
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.printf(请输入新的密码:);
String mima=sc.next String();
this.mima=new mima;
}
public static void main(String[] args){
bank a=new bank(1234567890,dongxi,1000);
a.deposit(100);
a.withdraw(150);
a.changemima();
a.createTime();
System.out.println(账户账号:+a.getld());
System.out.println(账户姓名:+a.getName());
System.out.println(账户日期:+a.getCreateTime());
System.out.println(账户余额:+a.getAmount());
}
}
结果图:



老师这个题目还有些问题,不会,不知道怎么做了,还请帮忙解答一下。
总结:
感觉对此次实验报告做的有点晕头,里面好多参数,还有设计不过,参考了别人设计,但经过思考一下啊,有晕,对Java还不够熟练,思维也没得到强化。