第四周总结和实验报告二

一、 实验目的

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

二、 实验内容

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

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

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

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

实验代码:

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);
        this.setColor(color);
}
    public void setWidth(double w) {
        width=w;
    }
        public void setHeight(double h) {
            height=h;
        }
        public void setColor(String c) {
            color=c;
    }
        public double getWidth() {
            return width;
        }
            public double getHeight() {
                return height;
            }
            public String getColor() {
                return color;
        }
            public double area() {
                return width*height;
            }
            public double length() {
                return (width+height)*2;
            }
};

测试代码:

public class text1{
    public static void main(String args[]) {
        Rectangle rect = null;
        rect =new Rectangle(5,6,"blue");
        System.out.println("宽:"+rect.getWidth());
        System.out.println("高:"+rect.getHeight());
        System.out.println("颜色:"+rect.getColor());
        System.out.println("面积:"+rect.area());
        System.out.println("周长:"+rect.length());
    }
}

测试结果:

总结:结合书上的例子写的,先从最基本的类开始分析,有一些基本程序设计思路了,而且定义变量时类型要慎重,数字不用引号,必须进行封装。

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

(1)实验代码:

import java.util.Date;
public class Account {
private String id;
private String name;
private Date tim;
private String mima;
private int money;

public Account(String id, String name, int money) {
    super();
    this.id = id;
    this.name = name;
    this.tim = new Date();
    this.mima = "123456";
    this.money = money;
}

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 getTim() {
    return tim;
}

public void setTim(Date tim) {
    this.tim = tim;
}

public String getMima() {
    return mima;
}

public void setMima(String mima) {
    this.mima = mima;
}

public int getMoney() {
    return money;
}

public void setMoney(int money) {
    this.money = money;
}

public void change(){
    Scanner sc = new Scanner(System.in);
String mima=sc.nextLine();
    this.mima=mima;
    sc.close();
}

    public void withdraw(int withdrawa) {
        money-=withdrawa;
        System.out.print("此次取款为:"+withdrawa);
        System.out.print("账户余额为:"+money);
    }

    public void deposit(int amount) {
        money+=amount;
        System.out.print("此次存款为:"+amount);
        System.out.print("账户余额为:"+money);
}
}

测试类:

(2)实验结果

(3)总结:花了一天的时间在百度和同学的提醒帮助下完成的,发现了很多细节问题与盲点,比如变量不能轻易改变类型,字符串不能转化成int;时间用Date比较方便;搞懂Java关键字的意思也很重要;String的使用还得继续琢磨;还有学会使用了eclipse的很多快捷键。

第四周总结:

1.这一周学习了String类两种对象实例化方式的区别:一是直接赋值(开一块,自动保存在对象池),二是使用new调用构造方法(开两块,不自动)。
以及String类中的常见几种操作方法,比如public char[ ]toCharArray()...
还学了对象数组(使用时数组中的每一个对象都必须实例化)
系统常见包(打包)以及import语句(导入类)。
2.String类中的方法要求理解记忆,真的很难...还有对象数组初始化的两种形态区别不太理解
3.一个字符串就是一个String类的匿名对象,但是匿名对象上星期学的不太记得了,需要及时复习..

猜你喜欢

转载自www.cnblogs.com/saury/p/11519690.html