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

实验二 Java简单类与对象

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

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

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

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

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

  • 实验过程
    1.写一个名为Rectangle的类表示矩形。
class Rectangle {                                      //定义矩形类
	private double width;
	private double height;
	private String color;
	public Rectangl(double width,double height,String color){
		this.setWidth(width);
		this.setHeight(height);
		this.setColor(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 getArea(double Area){                   //计算面积的方法
		Area = width * height;                            
		return Area;
	}
	public double getLenght(double Lenght){               //计算周长的方法
		Lenght = width + height;
		return Lenght;
	}
}

public class Demo1 {                                      //定义测试类
	public static void main(String args[]){
		Rectangle r1 = new Rectangle();
	}
}

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

package 实验二;

import java.util.Scanner;
import java.util.Date;
class Account{                                                 //创建一个银行账户类
	private String id;
	private String name;
	private Date createTime;
	private int balance;
	private String password = "123456";
	public Account(String number,String name,int date,String password,int balance){     //一个构造方法
		this.setId(id);
		this.setName(name);
		this.setCreateTime(createTime);
		this.setBalance(balance);
	}
	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 getCreateTime() {
		  return createTime;
	}
	public void setCreateTime(Date createTime){
		this.createTime = createTime;
	}
	public int getBalance() {
		return balance;
	}
	public void setBalance(int balance) {
		this.balance = balance;
	}
	public void newpassword() {                                    //修改密码的方法                  
		  Scanner sc=new Scanner(System.in);
		     String password = sc.next();
		     this.password = password;
	}
	public void deposit(int amount){                                //存款的方法
		this.balance += amount;
	}
	public void withdraw(int amount){                               //取款的方法
		this.balance -= amount;
		}
	public String toString(){                                       //查询账户信息的方法
		 String str="账户名:"+this.getId()+" 姓名:"+this.getName()+"开户日期:"+this.getCreateTime()+" 当前余额:"+this.getBalance();
		 return str;
		 }
}

public class Bank {                                                 //编写测试类
	public static void main(String[] args){
		Account s1 = new Account();
	}
}

  • 实验总结
    学习的东西有点快,感觉有点跟不上了,特别是实验的第二题,真心觉得有点无力了,对于JAVA的很多方法的设定还不是很熟悉,可能这是一个煎熬漫长的过程吧,上课还是能跟上,但是就是作业做起来有点难度。
原文地址:https://www.cnblogs.com/LuZhenYu/p/11560207.html