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

实验二 Java简单类与对象

实验目的

掌握类的定义,熟悉属性、构造函数、方法的作用,掌握用类作为类型声明变量和方法返回值;
理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性;
理解static修饰付对类、类成员变量及类方法的影响。

实验内容

1.写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。要求该类具有:
(1) 使用构造函数完成各属性的初始赋值

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

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

实验源码:

package second;
public class Rectangle {
	private double width;	
	private double heigth;
	private String color;
	//生成无参数的构造函数
	public Rectangle() {	
		
	}
	
	//生成带两个参数的构造函数
	public Rectangle(double width, double heigth) {     
		this.width = width;
		this.heigth = heigth;
	}
	//生成带三个参数的构造函数
   public Rectangle(double width, double heigth,String color) {
		this.width = width;
		this.heigth = heigth;
		this.color=color;
	}
	//获取宽的值
	public double getWidth() {
		return width;
	}
	//修改宽的值
	public void setWidth(double width) {
		this.width = width;
	}
	
	public double getHeigth() {
		return heigth;
	}
	
	public void setHeigth(double heigth) {
		this.heigth = heigth;
	}
	
	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}
	/**
	 * 计算面积的方法
	 * @return 面积
	 */
	public double getArea() {
		return this.getHeigth()*this.getWidth();
		
	}
	/**
	 * 计算周长的方法
	 * @return 周长
	 */
	public double getLength() {
		return (this.getHeigth()+this.getWidth())*2;
		
	}
	public static void main(String[] args) {
		Rectangle tets=new Rectangle(20,40,"红色");
		System.out.println("宽:"+tets.getWidth()+" 高:"+tets.getHeigth()+" 颜色:"+tets.getColor());
		System.out.println("面积:"+tets.getArea());
		System.out.println("周长:"+tets.getLength());
		tets.setHeigth(30);
		System.out.println();
		System.out.println("修改高后的结果:");
		System.out.println("宽:"+tets.getWidth()+" 高:"+tets.getHeigth()+" 颜色:"+tets.getColor());
		System.out.println("面积:"+tets.getArea());
		System.out.println("周长:"+tets.getLength());
	}
}

实验结果:

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

实验源码:

package second;

public class Account {
	
	private String id;//唯一标识码
	private String username;//用户姓名
	private String date;//开户日期
	private int password;//用户密码
	private double count;//当前余额
	/**
	 * 带三个参数的构造函数
	 * @param id 唯一标识码  
	 * @param password 用户密码 
	 * @param date 开户日期
	 */
	public Account(String id, int password ,String date) {

		this.id = id;
		this.date = date;
		this.password = password;
	}
	/**
	 * 存钱的方法
	 * @param money 要存的钱
	 */
	public void save(double money) {
		this.count=this.getCount()+money;
	}
	/**
	 * 取钱的方法
	 * @param money 要取的钱
	 */
	public void fetch(double money) {
		this.count=this.getCount()-money;
	}
	
	/**
	 * 
	 * @return 用户所有信息
	 */
	public String inof() {
		return "账户标识:" +this.getId() + "
客户姓名:" + this.getUsername()+ "
账户密码:" + this.getPassword() + "
开户时间:" +this.getDate() + "
账户余额:"
				+ this.getCount();
	}
	
	/**
	 * 查询账户的标识
	 * @return 账户的标识
	 */
		public String getId() {
			return id;
		}
		
		/**
		 * 查询账户的姓名
		 * @return 账户的姓名
		 */
	public String getUsername() {
		return username;
	}

	/**
	 * 修改账户的姓名
	 * @param username 账户的姓名
	 */
	public void setUsername(String username) {
		this.username = username;
	}
	
	/**
	 * 查询开户日期
	 * @return 返回开户日期
	 */
	public String getDate() {
		return date;
	}
	
	/**
	 * 查询账户密码
	 * @return 账户密码
	 */
	public int getPassword() {
		return password;
	}
	
	/**
	 * 修改账户密码
	 * @param password 新密码
	 */
	public void setPassword(int password) {
		this.password = password;
	}
	
	/**
	 * 查询账户余额
	 * @return 账户余额
	 */
	public double getCount() {
		return count;
	}

实验结果:

实验过程:

第一题还行,大致把老师讲的都结合起来了。先生成有无参数的构造函数,再通过get和set获取和修改参数的值。第二题的话,是我能力有限。看别人写的超全面,给我写的话得琢磨好久估计。第二题的日期,怎么直接调用系统日期还是有点困难。

课程总结:

1.String类
①两种实例化方法(例给name赋值zy):直接赋值(String name= "zy");直接调用String类的构造方法(String name=new String("zy"))。
②两种比较内容方法:“==”,“equals()”(将内容进行比较)。
③:String类中常用方法(jdk中可查看)。
④注意:字符串就是一个String类的匿名对象;使用String类声明后的字符串内容不可改变。
⑤ps:replace可作为替换或删除(per=per.replace(" "," "));for循环(for(char e:c))。
2.数组
一维数组的声明与分配内存:申明:数据类型 数组名称[ ]=null 或 数据类型[ ] 数组名称=null; 分配内存:数组名称=new 数据类型[长度];

学习总结:

每上一次李津老师的课,就像打开了新世界的大门。零零碎碎的知识点很多,不知道怎么记住,应该是多做题就熟悉了吧。

原文地址:https://www.cnblogs.com/qzy7/p/11558818.html