代码改变世界

第四周实验报告

2019-09-20 17:15  借过  阅读(163)  评论(0编辑  收藏  举报

第四周实验报告

实验二 Java简单类与对象

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

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

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

1、实验源代码

package test3;

public class number {
	public static void main(String args[]) {
		number rec=new number();
        rec.setWidth(4);
        rec.setHeight(6);
        rec.setColor("绿色");
        rec.getArea();
        rec.getLength();
        System.out.println("长:"+rec.getWidth()+"\n高:"+rec.getHeight()+"\n颜色:"+rec.getColor());
    }
    
    double width,height;
    String color="red";        
    public double getHeight() {
        return height;                
    }
    public double getWidth() {
        return width;
    }
    public String getColor() {
        return color;
    }
    public void setHeight(double height) {
        this.height = height;        
    }
    public void setWidth(double width) {
        this.width = width;
    }
    public void setColor(String color) {
        this.color = color;
    }
    
    public void getArea() {
        double area=0;
        area=this.height*this.width;
        System.out.println("面积为"+area);
    }
    public void getLength() {
        double length=0;
        length=(this.height+this.width)*2;
        System.out.println("周长为"+length);
    }
}

2、实验结果截图

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

1、实验源代码

package banksystem;

import java.util.Scanner;
public class banksystem{
    private String name,date="2019.9.20";
    private int key=123456,money=0;
    private String ccount="zzaf";
        public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDate() {
        return date;
    }
    public void setDate(String date) {
        this.date = date;
    }
    public int getKey() {
        return key;
    }
    public void setCipher(int key) {
        this.key = key;
    }
    public int getMoney() {
        return money;
    }
    public void setMoney(int money) {
        this.money = money;
    }
    public String getCcount() {
        return ccount;
    }
    public void setCcount(String ccount) {
        this.ccount = ccount;
    }
    public void username(){                   
        System.out.print("用户名称:");
        Scanner username = new Scanner(System.in);
        String name = username.nextLine();
        setName(name);
    }
    public void ChangePWD(){                    
        System.out.print("请输入您要更改的密码:");
        Scanner userkey = new Scanner(System.in);
        int key = userkey.nextInt();
        setCipher(key);
    }
    public void all(){                                    
        System.out.print("用户名:"+name+"\n开户日期:"+date+"\n账号:"+ccount+"\n密码:"+key+"\n余额:"+money+"\n");
    }
    public void Depositsandwithdrawals(){
        System.out.print("存款:1\n"+"取款:0\n");
        System.out.print("请选择:");
        Scanner j = new Scanner(System.in);
        int i = j.nextInt();
        if(i==0) {
        System.out.print("取款数:");
        Scanner usermoney = new Scanner(System.in);
        int money = usermoney.nextInt();
        money=this.money-money;
        System.out.print("操作成功!当前余额:"+money);
        setMoney(money);
        }
        else {
        System.out.print("存款数:");
        Scanner usermoney = new Scanner(System.in);
        int money = usermoney.nextInt();
        money=this.money+money;
        System.out.print("操作成功!当前余额:"+money);
        setMoney(money);
        }
    }
    public static void main(String[] args) {
        System.out.print(" Welcome\n");
        System.out.print("进入系统请按:1\n");
        System.out.print("请输入你要操作的序号:");
        Scanner j = new Scanner(System.in);
        int J = j.nextInt();
        int I=0;
        banksystem user = new banksystem();
        for(int k=1;k>0;) {
            if(J==1||I==1) {
                System.out.print("开通用户:1\n"+"更改密码:2\n"+"查询信息:3\n"+"存款取款:4\n");
                System.out.print("请输入你要操作的序号:");
                Scanner b = new Scanner(System.in);
                int B = b.nextInt();
                if(B==1) {
                    user.username();
                }else if(B==2){
                    user.ChangePWD();
                }else if(B==3){
                    user.all();
                }else if(B==4){
                    user.Depositsandwithdrawals();
                }
                System.out.print("返回主页:1\n"+"退出系统:0\n");
                System.out.print("请输入你要操作的序号:");
                Scanner i = new Scanner(System.in);
                I = i.nextInt();
                J=0;
            }else{
                break;
        }
      }
        System.out.print("已安全退出,感谢使用!");
    }   
}

2、实验结果截图

学习总结
这次的两个题比上周显然要难,第一题的话使用构造函数会比较方便的做出来,老师也在课上讲过了,第二题的话就存在一些问题了,一开始不知道怎么做起,可能是要用到刚学的一些语句方法那些的,这里的封存就有用了,不能像C一样直接进行赋值,同时,本周的get和set语句方法也是能够解决问题的,这题还是问了黄才知道,知道方法就还好点了,不过代码还是有蛮长,不过还可。