加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

第四周课程总结&试验报告(二)

发布时间:2020-12-15 07:41:29 所属栏目:Java 来源:网络整理
导读:第四周课程总结试验报告(二) 实验二?Java简单类与对象 实验目的 掌握类的定义,熟悉属性、构造函数、方法的作用,掌握用类作为类型声明变量和方法返回值; 理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性; 理解static修饰

第四周课程总结&试验报告(二)

实验二?Java简单类与对象

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

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

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

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

实验代码:

package main;

public class Rectangle {
    private  double  width,height;
    private String color;
    public Rectangle(double width,double height,String color){
        this.setHeight(height);
        this.setWidth(width);
        this.setColor(color);
    }
    public  void tell() {
        System.out.println("宽=" +getWidth()+"  "+"长="+getHeight()+"  "+"颜色:"+getColor());
    }

    public  double getWidth() {
        return width;
    }
    public void setWidth(double width) {
        this.width = width;
    }
    public  double getHeight() {
        return height;
    }
    public void setHeight(double height) {
        this.height = height;
    }
    public  String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    } 
    public void getArea() {
        double area=0;
        area=this.width*this.height;
        System.out.println("面积="+area);
    }
     public void getLength() {
            double length=0;
            length=2*(this.width+this.height);
            System.out.println("周长="+length);
        }
     public static class Main{
         public static void main(String[] args) {
                Rectangle per=new Rectangle(10,20,"blue");
                per.tell();
                per.getArea();
                per.getLength();
            }
         }
}

实验过程:

这一道题要比第二题相对简单些,首先定义构造方法为矩形的几个属性初始化,然后可以用eclipse软件自带功能使用get…()和set…()的形式完成属性的访问及修改,然后写出计算面积的getArea()方法和计算周长的getLength()方法,最后调用构造方法输出矩形的信息。在对照书上写完代码后,还是出现了以下错误,改变其他变量的静态修饰符并不能解决,后来只有根据提示将静态修饰符添加到父类型才解决了问题。

?

?

?实验结果:

?

?

?

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

实验代码:

package main;

import java.util.Scanner;

public class Main {
    private String name;
    String date;
    private int password;
    int money;
    private String Account;
        public Main(String name,String date,String Account,int password,int money) {
            this.setName(name);
            this.setDate(date);
            this.setAccount(Account);
            this.setPassword(password);
            this.setMoney(money);
    }
        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 getPassword() {
        return password;
    }
    public void setPassword(int password) {
        this.password = password;
    }
    public int getMoney() {
        return money;
    }
    public void setMoney(int money2) {
        this.money = money2;
    }
    public String getAccount() {
        return Account;
    }
    public void setAccount(String account2) {
        this.Account = account2;
    }
        public void username(){                  
            System.out.print("用户名称:");
            Scanner username = new Scanner(System.in);
            String name = username.nextLine();
            setName(name);
        }
        public void Changepassword(){                    
            System.out.print("更改密码:");
            Scanner userpassword = new Scanner(System.in);
            int password = userpassword.nextInt();
            setPassword(password);
        }
        public void tell(){                                    
            System.out.print("用户名:"+name+"  开户日期:"+date+"n账号:"+Account+"    密码:"+password+"n余额:"+money+"n");
        }
  public static void main(String[] args) {
    System.out.print("欢迎!n");
    System.out.print("进入系统请按1n");
    System.out.print("请选择:");
    Scanner x = new Scanner(System.in);
    int X = x.nextInt();
    int Y=0;
    Main per = new Main("张三","2019.9.20","6666666666",123456,0);
    for(int i=1;i>0;i++){
        if(X==1||Y==1) {
            System.out.print("开户:1     "+"更改密码:2n"+"查询用户信息:3     ");
            System.out.print("请选择:");
            Scanner p = new Scanner(System.in);
            int A = p.nextInt();
            if(A==1) 
            {
                per.username();
            }
            else if(A==2)
            {
                per.Changepassword();
            }
            else if(A==3)
            {
                per.tell();
                System.out.print("返回主页面:1n"+"退出:0n");
                System.out.print("请选择:");
                Scanner x1 = new Scanner(System.in);
                X = x1.nextInt();
                Y=0;
            }
        }
      }
   } 
}

实验过程:

这一题从刚开始的茫然到读懂题意都花了不少的时间,后来看了同学的代码才明白题目要实现什么,就开始照着第一题那样写,首先声明属性并定义构造方法为用户名、开户日期、账号、密码等初始化,然后定义一个开户(没能实现)和改密码的方法,然后根据输入来输出所需要的信息(调用构造方法)。但是那样只能将用户的姓名,开户日期,账户密码,当前的余额输出来,并不能对数据进行改变,再看同学的代码后对自己的代码进行了改进,也只实现了对密码进行了修改,并不能进行开户和存款、取款,但这也花了一下午的时间,今天没时间了,也只能后面在改进了。

实验结果:

?

?学习&自我总结

这周课堂上老师主要讲了string类的常用方法,对一些常用的方法进行了使用讲解,还有string可以采用直接赋值的方式进行操作,也可以通过new方法实例化,而自己在学习上的进步主要还是通过写作业,在做题过程中发现了自己的许多问题,一些在课堂上听懂的写起题目来却写不出,列如Scanner userpassword = new Scanner(System.in)在做题前并不能很好的理解它的意义,但通过查找资料也学到了不少知识,虽然这周作业没有完成,但也有了不少收获,看来学习要多实践才能提高自己,继续努力。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读