zoukankan      html  css  js  c++  java
  • 第四周课程总结&实验报告

    实验二 Java简单类与对象

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

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

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

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

    实验代码:

    package 实验2;
    
    
    public class Rectangle {
        public static void main(String args[]) {
            Rectangle rec=new Rectangle();
            rec.setWidth(3);
            rec.setHeight(4);
            rec.setColor("红色");
            rec.getArea();
            rec.getLength();
            System.out.println("长:"+rec.getWidth()+"
    高:"+rec.getHeight()+"
    颜色:"+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);
        }
    }

    实验结果:

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

     实验代码:

    package 实验2;
    import java.util.Scanner;
    public class Banksystem{
        private String name,date="2019.9.20";
        private int key=123456,money=0;
        private String ccount="Barcelona";
            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+"
    开户日期:"+date+"
    账号:"+ccount+"
    密码:"+key+"
    余额:"+money+"
    ");
        }
        public void Depositsandwithdrawals(){
            System.out.print("存款:1
    "+"取款:0
    ");
            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
    ");
            System.out.print("进入系统请按:1
    ");
            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
    "+"更改密码:2
    "+"查询信息:3
    "+"存款取款:4
    ");
                    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
    "+"退出系统:0
    ");
                    System.out.print("请输入你要操作的序号:");
                    Scanner i = new Scanner(System.in);
                    I = i.nextInt();
                    J=0;
                }else{
                    break;
            }
          }
            System.out.print("已安全退出,感谢使用!");
        }   
    }

    实验结果:

     

     

    学习总结:

    这次的实验主要是让我们掌握类的定义,掌握用类作为类型声明变量和方法返回值,理解类和对象的区别,其中,构造方法的名称必须与类名称一致;构造方法的声明处不能有任何返回值类型的声明;不能在构造方法中使用return返回一个值。还有就是熟悉属性、构造函数、方法的作用;掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性;理解static修饰付对类、类成员变量及类方法的影响。在上课时,老师讲的课很有激情,跟着老师来的话还是很好理解的,所以听老师说的那些类的用法倒是蛮轻松的,下课后用起来却是蛮费劲,感觉要消化的东西有点多,代码还是要多打打。

    第一个题目难度不大,老师上课的时候基本就讲了,所以做起来也没什么问题。

    第二题的话难度相比于第一题要大点,因为是做一个类似于银行账户的东西,当我们进行一步操作后,它里面储存的东西就会改变,我们需要将改变的东西保存下来,这个点对之前的我们来说是有难度的,还好老师上课讲了get和set,加上第一题写了类似的题,还是顺利解决了,但是目前的这个账户还比较低级,比如密码开头不能为0,账号那里也没有做什么操作等等,这些问题还需要进一步解决,完善。生命在于创造,还要努力!

  • 相关阅读:
    [转载]DBA的特质第一部分:技术
    DPA/Ignite由于DNS问题导致连接不上被监控的数据库服务器
    ORACLE计算表引占用空间大小
    Reporting Services 错误案例一则
    SQL SERVER Transactional Replication中添加新表如何不初始化整个快照
    TNS-12540: TNS:internal limit restriction exceeded
    Error: 9001, Severity: 21, State: 5 The log for database 'xxxx' is not available
    ORA-01012: not logged on
    -bash: .bash_profile: command not found
    -bash: ulimit: pipe size: cannot modify limit: Invalid argument
  • 原文地址:https://www.cnblogs.com/Jay-h/p/11556076.html
Copyright © 2011-2022 走看看