第四周总结&实验报告二

时间:2019-09-20
本文章向大家介绍第四周总结&实验报告二,主要包括第四周总结&实验报告二使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

实验报告二

实验目的
掌握类的定义,熟悉属性、构造函数、方法的作用,掌握用类作为类型声明变量和方法返回值;
理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性;
理解static修饰付对类、类成员变量及类方法的影响。
实验内容
1.写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。要求该类具有:

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

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

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

package test;

class Rectangle {
    private double width;
    private double height;
    private String color;
    public Rectangle(double width,double height,String color) {
        this.width = width;
        this.height = height;
        this.color = color;
    }
    public double getWidth(){
        return width;
    }
    public void setwidth(double a){
        width = a;
    }
    public double getheight(){
        return height;
    }
    public void setheight(double b){
        height = b;
    }
    public String getColor(){
        return color;
    }
    public void setcolor(String c){
        color = c;
    }
    public double getLengh(){
        return (height+width)*2;
    }
    public double getArea() {
        return width*height;
    }
}
package test;

public class test {
    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        Rectangle rec = null;
        rec = new Rectangle(10,20,"蓝色");
        System.out.println("宽:" + rec.getWidth());
        System.out.println("高:" + rec.getheight());
        System.out.println("颜色:" + rec.getColor());
        System.out.println("面积:" + rec.getArea());
        System.out.println("周长:  " + rec.getLengh());

    }

}

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

package test1;

class Account {
    private String id;         //账户
    private String name;      //姓名
    private int date;        //开户日期
    private int cipher;      //密码
    private int balance;     //账户余额
    
    
    public Account (String id,String name,int cipher,int balance) {
        this.id=id;
        this.name=name;
        this.cipher=123456;
        this.balance=balance;
    }
    
    public String getId() {
        return id;
    }
    public void setId1(String a) {
        id = a;
    }
    
    
    public String getName() {
        return name;
    }
    public void setName(String b) {
        name = b;
    }
    
    
    public int getDate() {
        return date;
    }
    
    public void setDate(int c) {
        date = c;
    }
    
    
    public int getCipher() {
        return cipher;
    }
    
    public void setCipher(int d) {
        cipher = d;
    }
    
    public int getBalance() {
        return balance;
    }
    
    public void setbalance(int e) {
        balance = e;
    }
    
}
package test1;

public class test {

    public static void main(String[] args) {
        Account acc=null;
        acc=new Account("987456321", "zhangsan",123456,0);
        System.out.println("账户:"+acc.getId());
        System.out.println("姓名:"+acc.getName());
        System.out.println("余额:"+acc.getBalance());
        System.out.println("密码:"+acc.getCipher());
        

    }

}

第三次学习总结
这一周学习了String类,String的赋值
复习了一下之前学的以及讲解了一些习题,老师放慢了一些节奏让我们能够跟上来,还有就是讲了一下数组
包的基本概念和用法
包的定义:package 包名称.子包名称;

原文地址:https://www.cnblogs.com/cxnkkk/p/11560210.html