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

实验报告

1.写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。要求该类具有:
(1) 使用构造函数完成各属性的初始赋值
(2) 使用get…()和set…()的形式完成属性的访问及修改
(3) 提供计算面积的getArea()方法和计算周长的getLength()方法

代码

package Rectangle;

import java.util.*;
class Rectangle {
	private double width;
	private double height;
	private String color;
	
	public Rectangle(String color) {
		this.color = color;
	}
	public void setHeight(double h) {
		height=h;
	}
	public void setWidth(double w) {
		width=w;
	}
	public void setColor(String c) {
		color=c;
	}
	public double getWidth() {
		return width;
	}
	public double getHeight() {
		return height;
	}
	public String getColor() {
		return color;
	}
	
	public void InputwidthAndheight() {
		Scanner pw = new Scanner(System.in);
		
		
		System.out.println("输入矩形宽度:");
		double width = pw.nextDouble();
		this.width = width;
		
		System.out.println("输入矩形长度:");
		double height = pw.nextDouble();
		this.height = height;
		
		
		pw.close();
	}
	public double getArea() {
		return height*width;
	}
	public double getLength() {
		return (width+height)*2;
	}
	
}

package Rectangle;

public class Test {
	public static void main(String[] args) {
		Rectangle re = null;
		re = new Rectangle("red");
		
		re.InputwidthAndheight();
		System.out.println("矩形颜色:"+ re.getColor());
		System.out.println("矩形面积:"+ re.getArea());
		System.out.println("矩形周长:"+ re.getLength());
	}
}


遇到的问题

无。

运行结果

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

代码

package text2;

import java.util.*;
class Account {
	private String name;   //姓名;
	private String id;    //标识
	private Date openday;   //开户日期
	private String password;    //密码
	private int balance;    //余额
	
	public Account(String id,String name,int balance) {   //开户初始状态
		this.setId(id);
		this.setBalance(balance);
		this.setName(name);
		this.openday = new Date();
		this.password = "123456";
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public int getBalance() {
		return balance;
	}

	public void setBalance(int balance) {
		this.balance = balance;
	}
	
	public void setOpenday(Date openday) {
		this.openday = openday;
	}
	
	public Date getOpenday() {
		return openday;
	}
	
	

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void changepassword() {
		Scanner pw = new Scanner(System.in);
		System.out.println("输入你的新密码:");
		

		String password = pw.nextLine();
		if(password.length()!=6)
		{
			System.out.println("账户密码需为6位数字!");
			System.exit(0);
		}
			
		this.password = password;
		
		pw.close();
	}
	
	public void Savemoney(int money) {    //存钱
		this.balance = balance+money;
	}
	public void Drawmoney(int money) {
		this.balance = balance-money;
	}
	
}

package text2;

public class test2 {
	public static void main(String[] args) {
		Account str = null;
		str = new Account("nishidi01ge","Leemay",0);   //开户状态
		
		
		str.changepassword();
		str.Savemoney(1000);
		str.Drawmoney(600);
		
		System.out.println("标识:"+ str.getId());
		System.out.println("姓名:"+ str.getName());
		System.out.println("余额:"+ str.getBalance());
		System.out.println("开户日期:"+ str.getOpenday());
		System.out.println("密码:"+ str.getPassword());
		
	}

}


遇到的问题

1.报错,无法输入更改内容;

解决方法:把nextString();改为nextLine(); 删除new; 关闭pw变量。

运行结果

课程总结

本周学习了

数组

定义
初始化
元素表示方式

作用、定义、类的导入

String类

两种实例化方式:1.直接赋值;2.new。
两种实例化区别:1方法只会开辟一块堆内存空间,且会自动保存在对象池中以供下次重复使用;2方法会开辟两块堆内存空间,其中一块会成为垃圾空间。
两种比较方式:1.“==”比的是地址值;2.equals()方法比的是内容。
字符串的内容不可改变,如果同一个字符变量的内容改变了,则说明该变量指向了存储更改后的内容的空间(更改后的内容存在了一个新的空间处)的地址。
String类常用方法。

这周学的很多的String类的方法,不怎么会用,数组和以前学的差不多,不难理解,包的话只算是提了一下,还没讲到重点。

posted @ 2019-09-20 17:40  LeeMayZ  阅读(157)  评论(2编辑  收藏  举报