第四周课程总结和实验报告

实验二 Java简单类与对象

实验目的

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

实验内容

1、写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。要求该类具有:

(1) 使用构造函数完成各属性的初始赋值

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

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

实验代码

package 作业;

public class Rectangle {
	private double width;          //定义
	private double height;
	private String color;
	
	public Rectangle(double width, double height, String color) {
		this.setWidth(width);
		this.setHeight(height);           //定义有3个参数的构造方法,再对其进行初始化
		this.setColor(color);
	}
	public Rectangle() {
		// TODO Auto-generated constructor stub  //无参构造
	}
	public void setWidth(double width) {
		this.width = width;
	}
	public void setHeight(double height) {
		this.height = height;
	}
	public void setColor(String color) {                   //设置宽度,长度和颜色
		this.color = color;
	}
	public double getWidth() {
		return width;
	}
	public double getHeight() {
		return height;
	}
	public String getColor() {                             //取得宽度,长度和颜色
		return color;
	}
	public double getArea() {
		return this.width*this.height;               //计算面积
	}
	public double getGetlength() {
		return (this.height+this.width)*2;        //计算周长
	}
	public static void main(String[] args) {
		Rectangle a=null;
		a = new Rectangle();          //声明对象,并进行实例化和赋值
		a.width=5.0;                                          
		a.height=6.0;
		System.out.println(+a.getArea());                //输出
		System.out.println(+a.getGetlength());
	}
}

再运行代码时出现了一个错误Exception in thread "main" java.lang.Error: Unresolved compilation problem: The constructor Rectangle() is undefined,检查后发现需要定义一个无参构造,

public Rectangle() {
		// TODO Auto-generated constructor stub
	}

这道题目还是不难的,因此错误也不多。

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

实验代码

package 作业;

public class Bank {
	public static void main(String[] args) {
        Account a = new Account();
        a.setAll();
        a.changebalance(1000.0);
        a.setAll();
        a.changepsd("123456");
        a.setAll();
    }
}

class Account {
    String ID;
    String psd;
    String name; 
    int year;
    int month;
    int day;
    double balance;         
    Account() {
        this.ID="1sunzeliang";
        this.name="szl";
        this.psd="195315";
        this.year=2008;
        this.month=8;
        this.day=8;
        this.balance=10000.0;
    }
    Account(String ID, String name, String psd, int year, int month, int day, double balance) {
        this.ID = ID;
        this.name = name;
        this.psd = psd;
        this.year = year;
        this.month = month;
        this.day = day;
        this.balance = balance;
    }

    public void changebalance(double single) {
        this.balance += single;
    }

    public void changepsd(String single) {
        this.psd = single;
    }

    public void setAll() {
        System.out.println(this.ID);
        System.out.println(this.name);
        System.out.println(this.year+"."+this.month+"."+this.day);
        System.out.println(+this.balance);
    }

}

本题一开始时不知道怎么处理,到后来参考了本班某位同学的代码,然后自己打了一遍出现了错误,错误的原因时未定义构造函数账户,然后我把在Account a = new Account()代码里的信息删除了

并把代码

  Account() {
        this.ID="1sunzeliang";
        this.name="null";
        this.year=0;
        this.month=0;
        this.day=0;
        this.psd="null";
        this.balance=0;
    }

改为了

 Account() {
        this.ID="1sunzeliang";
        this.name="szl";
        this.psd="195315";
        this.year=2008;
        this.month=8;
        this.day=8;
        this.balance=10000.0;
    }

成功解决了问题。

本周学习内容总结

1、String对象的内容比较:

有两种比较方法1、通过==进行内容的比较。2、使用equals()方法对String的内容进行比较。

对象不同,内容相同,"=="返回false,equals返回true。
同一对象,"=="和equals结果相同。
如果值不相同,对象就不相同,所以"==" 和equals结果一样。

两种方法的区别是==是Java提供的关系运算符,主要是进行数值相等判断的。
equals()是由String提供的一个方法,专门负责进行字符串内容的比较。

2、String有两种实例化方式,一种是通过直接赋值的方式,另外一种是使用标准的new调用构造方法完成实例化

使用String直接赋值后,只要是以后声明的字符串内容相同,则不会再开辟新的内存空间。对于String的以上操作,在java中称为共享设计,
这种设计思路是,在java中形成一个对象池,在这个对象池中保存多个对象,新实例化的对象如果已经在池中定义了,则不再重新定义,
而从池中直接取出继续使用。String就是因为采用了这样的设计,所以当内容重复时,会将对象指向已存在的实例空间。

3、String类中常用的方法

在书本的P110

s.concat("E");对s添加新的元素E。
s.replace('C', 'F');把C替换成F
replace可以进行替换,也可以用来删除。

本周自己还自学了一点东西

1、System.currentTimeMillis()计算方式

在开发过程中,通常很多人都习惯使用new Date()来获取当前时间。new Date()所做的事情其实就是调用了System.currentTimeMillis()。如果仅仅是需要或者毫秒数,那么完全可以使用System.currentTimeMillis()去代替new Date(),效率上会高一点。如果需要在同一个方法里面多次使用new Date(),通常性能就是这样一点一点地消耗掉,这里其实可以声明一个引用。
System.currentTimeMillis()+36001000)可以这样解读:System.currentTimeMillis()相当于是毫秒为单位,但是,后头成了1000,就变成了以秒为单位。那么,3600秒=1小时,所以输出为当前时间的1小时后。
我们可以这样控制时间:System.currentTimeMillis()+time
1000),里面传入的time是以秒为单位,当传入60,则输出:当前时间的一分钟后。

本周课堂上内容都听懂了,关于课堂习题自己错误的,也已经搞清楚了,而且自己还自学了一点点,坚持,努力加油下去。

原文地址:https://www.cnblogs.com/neir/p/11551463.html