• 第四周课程总结与第二次实验报告(Java简单类与对象)


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

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

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

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

    实验代码

    public class Rectangle {
      private double height;
      private double width;
      private String color;
    public double getHeight(){
    	return height;
    }
    public void setHeight(double height){
    	this.height = height;	
    }
    public double getWidth(){
    	return width;
    }
    public void setWidth(double width){
    	this.width = width;
    }
    public String getColor(){
    	return color;
    }
    public void setColor(String color){
    	this.color = color;
    }
    public Rectangle(double height,double width,String color ){
    	this.setHeight(height);
    	this.setWidth(width);
    	this.setColor(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("矩形的周长" + girth);
    }
    public String toString(){
    	String recStr = "矩形的高度:" + this.getHeight() + "矩形的宽度:" + this.getWidth() + "矩形的颜色:" + this.getColor();
    	return recStr;
    }
    public static void main(String args[]){
    	Rectangle rec = new Rectangle(8,9,"黑色");
    	rec.getArea();
    	rec.getLength();
     System.out.println(rec.toString());
    }
    }
    

    思考:用private对矩形的属性长宽,高,颜色进行封装,使用get(),set(),的形式完成属性的访问与修改,用方法对矩形的周长和面积进行计算,利用to String方法返回字符串,使用构造函数完成对各属性的初始赋值。

    出现的问题:因为对构造函数以及对构造方法还不是很熟练,所以出现了一些问题,经过自己查阅书籍以及查找资料解决了问题

    结果截图:

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

    实验代码:

    import java.util.Scanner;
    
    class Zl{
        private String id;
        private String name;
        private String begindate;
        private String password;
        private float menoy;
        public Zl() {
        
         }
        public Zl(String id,String name,String begindate,float menoy) {
            this.setId(id);
            this.setName(name);
            this.setBegindate(begindate);
            this.password = "123456";
            this.setMenoy(menoy);
            
        }
        
        public void setId(String i) {
            id = i;
        }
        public void setName(String n) {
            name = n;
        }
    
        public void setBegindate(String b) {
            begindate = b;
        }
        public void setMenoy(float m) {
            menoy=m;
        }
       
        public String getId() {
            return id;
        }
        public String getName() {
            return name;
        }
        public String getBegindate() {
            return begindate;
        }
        public float getMenoy() {
            return menoy;
        }
        public float cqian() {           
            return menoy+cqian();  
        }
        public float qqian() {               
            return menoy-qqian();
        }
        public void changepassword() {      
            System.out.println("输入新密码 ");
            Scanner in = new Scanner(System.in);
            int password = in.nextInt();
        }
    }
    public class Bank {
    
        public static void main(String args[]) {
            Zl z = null;
            z = new Zl("CN-12345678","无殇","2008.8.18",8888888.0f);
            System.out.println("账号 "+z.getId());
            System.out.println("姓名 "+z.getName());
            System.out.println("开户日期 "+z.getBegindate());
            System.out.println("余额 "+z.getMenoy());
            
            
        }
    }
    

    思考:先前看这个题目第一反应以为和第一题差不多,然后就按照写第一题的方法写了第二题,然后用Eclipise去编译发现有很多报错了自己也进行了更改,发现还是不对,自己也询问了同学也查找了一些资料,后来解决了问题,发现是方法用错了,我还是采用了和上一题差不多的思路,但感觉其实自己没有做完.


    结果截图

    总结:感觉自己对知识点还不是很熟悉,解决问题的能力还有待提高,希望自己能利用课余时间做一些练习题,提高自己解决问题的能力,道阻且长,唯有一直向上.

    第四周课程总结

    1.String类的常用方法,起先对此还没有太多了解,通过PTA上面的六道题的练习,对String有了一定的了解.

    2.学习了包的相关知识点,在包中完整的类名称应该是包.类名称.

    3.还学习了import语句,如果几个了类存放在不同的包中,则使用类必须通过import语句导入,以及一些系统常见包.

    4.不忘初心,方得始终.

  • 相关阅读:
    java学习笔记(二)分布式框架Dubbo+zookeeper搭建
    java学习笔记(一) 服务器的认识
    用slf4j+logback实现多功能日志解决方案 --- 转
    9.3.2 The force and release procedural statements
    3.7.4 Tri0 and tri1 nets
    9.3.1 The assign and deassign procedural statements
    10. Tasks and functions
    6.1.2 The continuous assignment statement
    df 查看磁盘使用情况
    信息学竞赛知识点整理
  • 原文地址:https://www.cnblogs.com/buxiu888/p/11536002.html
Copyright © 2020-2023  润新知