Java第三周作业

Java实验报告二

一、题目

1.写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。要求该类具有:
(1) 使用构造函数完成各属性的初始赋值
(2) 使用get…()和set…()的形式完成属性的访问及修改
(3) 提供计算面积的getArea()方法和计算周长的getLength()方法
2.银行的账户记录Account有账户的唯一性标识(11个长度的字符和数字的组合),用户的姓名,开户日期,账户密码(六位的数字,可以用0开头),当前的余额。银行规定新开一个账户时,银行方面提供一个标识符、账户初始密码123456,客户提供姓名,开户时客户可以直接存入一笔初始账户金额,不提供时初始余额为0。定义该类,并要求该类提供如下方法:存款、取款、变更密码、可以分别查询账户的标识、姓名、开户日期、当前余额等信息。

二、实验代码

package Test;

public class Rectangle {
	//定义属性
	private double width;
	private double height;
	private String 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 Rectangle(double width,double height,String color) {
		this.width=width;
		this.height=height;
		this.color=color;
	}
	
	//进行一些需要的操作
	public double getArea() {
		return this.width*this.height;//计算面积
	}
	public double getLength() {
		return 2*this.width+2*this.height;//计算周长
	}
	public String color() {
		return this.color;
	}
	public static void main(String[] args) {
		Rectangle rec = new Rectangle(5,6,"red");
		System.out.println("矩形的面积为:"+rec.getArea()+"平方米");
		System.out.println("矩形的周长为:"+rec.getLength()+"米");
		System.out.println("矩形的颜色为:"+rec.getColor());
	}
}
package Test;

import java.util.Scanner;
import java.util.Date;

public class Account {
	//定义属性
	private String id;
	private String name;
	private Date date;
	private int password;
	private int money;
	//自动生成setter和getter方法
	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 getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	public int getPassword() {
		return password;
	}
	public void setPassword(int password) {
		this.password = password;
	}
	public int getMoney() {
		return money;
	}
	public void setMoney(int money) {
		this.money = money;
	}
	//构造方法
	public Account(String id, String name, int money) {
		this.id = id;
		this.name = name;
		this.money = money;
		this.date = new Date();
		this.password = password;
	}
	//计算有进账情况的余额
	public void in(int num) {
		this.money = this.money+num;
	}
	//计算有出账情况的余额
	public void out(int num) {
		this.money = this.money-num;
	}
	//输入密码
	public void password() {
		Scanner sc=new Scanner(System.in);
	    int password=sc.nextInt();
	    this.password = password;
	    sc.close();
	   
	}
	public static void main(String[] args) {
		Account acc=new Account("Supreme","Holland",1000);
        acc.in(0);
        acc.out(0);
        acc.password();
	    System.out.println("标识符:"+acc.getId());
		System.out.println("姓名:"+acc.getName());
		System.out.println("日期:"+acc.getDate());
		System.out.println("余额:"+acc.getMoney());
		System.out.println("密码:"+acc.getPassword());
	}

}

三、运行结果截图

四、总结

这次实验加深了对构造方法的理解,但是第二题自己做了好久都做不对感觉跟第一题还是有很大区别的,所以第二题从开始做一直卡了很久没有办法,后面去搜了很多资料才大概做出来,但是我感觉我做的还是错的。

猜你喜欢

转载自www.cnblogs.com/huangxuannn/p/12594458.html
今日推荐