- 写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。要求该类具有:
(1) 使用构造函数完成各属性的初始赋值
(2) 使用get…()和set…()的形式完成属性的访问及修改
(3) 提供计算面积的getArea()方法和计算周长的getLength()方法
实验代码:
class test {
private double width;
private double height;
private String color;
public test() {
}
public test(double width, double height, String color) {
this.setWidth(width);
this.setHeight(height);
this.setColor(color);
}
public void setWidth(double w) {
width = w;
}
public void setHeight(double h) {
height = h;
}
public void setColor(String c) {
color = c;
}
public double getWidth() {
return width;
}
public double getHeight() {
return height;
}
public String getColor() {
return color;
}
public double getArea() {
return width * height;
}
public double getLength() {
return 2 * (width + height);
}
}
public class Rectangle {
public static void main(String[] args) {
test t = null;
t = new test(15, 18, "blue");
System.out.println("周长为"+t.getLength());
System.out.println("面积为"+t.getArea());
}
private double width;
private double height;
private String color;
public test() {
}
public test(double width, double height, String color) {
this.setWidth(width);
this.setHeight(height);
this.setColor(color);
}
public void setWidth(double w) {
width = w;
}
public void setHeight(double h) {
height = h;
}
public void setColor(String c) {
color = c;
}
public double getWidth() {
return width;
}
public double getHeight() {
return height;
}
public String getColor() {
return color;
}
public double getArea() {
return width * height;
}
public double getLength() {
return 2 * (width + height);
}
}
public class Rectangle {
public static void main(String[] args) {
test t = null;
t = new test(15, 18, "blue");
System.out.println("周长为"+t.getLength());
System.out.println("面积为"+t.getArea());
}
银行的账户记录Account有账户的唯一性标识(11个长度的字符和数字的组合),用户的姓名,开户日期,账户密码(六位的数字,可以用0开头),当前的余额。银行规定新开一个账户时,银行方面提供一个标识符、账户初始密码123456,客户提供姓名,开户时客户可以直接存入一笔初始账户金额,不提供时初始余额为0。定义该类,并要求该类提供如下方法:存款、取款、变更密码、可以分别查询账户的标识、姓名、开户日期、当前余额等信息。
实验代码:
import java.util.Date;
public class Account {
private String id;
private String name;
private Date tim;
private String mima;
private int money;
public Account(String id, String name, int money) {
super();
this.id = id;
this.name = name;
this.tim = new Date();
this.mima = "123456";
this.money = money;
}
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 Date getTim() {
return tim;
}
public void setTim(Date tim) {
this.tim = tim;
}
public String getMima() {
return mima;
}
public void setMima(String mima) {
this.mima = mima;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public void change(){
Scanner sc = new Scanner(System.in);
String mima=sc.nextLine();
this.mima=mima; sc.close(); } public void withdraw(int withdrawa) {
money-=withdrawa;
System.out.print("此次取款为:"+withdrawa);
System.out.print("账户余额为:"+money);
}
public void deposit(int amount) {
money+=amount;
System.out.print("此次存款为:"+amount);
System.out.print("账户余额为:"+money);
}
}
public class Account {
private String id;
private String name;
private Date tim;
private String mima;
private int money;
public Account(String id, String name, int money) {
super();
this.id = id;
this.name = name;
this.tim = new Date();
this.mima = "123456";
this.money = money;
}
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 Date getTim() {
return tim;
}
public void setTim(Date tim) {
this.tim = tim;
}
public String getMima() {
return mima;
}
public void setMima(String mima) {
this.mima = mima;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public void change(){
Scanner sc = new Scanner(System.in);
String mima=sc.nextLine();
this.mima=mima; sc.close(); } public void withdraw(int withdrawa) {
money-=withdrawa;
System.out.print("此次取款为:"+withdrawa);
System.out.print("账户余额为:"+money);
}
public void deposit(int amount) {
money+=amount;
System.out.print("此次存款为:"+amount);
System.out.print("账户余额为:"+money);
}
}
总结:
这周主要学习了String类string类中常用的方法。在学习String的时候还是有许多不懂得地方,在课堂上老师讲解的时候还是有些听不懂,希望在接下来的学习过程中自己继续努力。