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

    实验二 Java简单类与对象

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

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

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

    package hlha;
    
    	public class Rectangle {
    		    private double width=12,height=10;      //设定初始值
    		    private String color="yellow";    
    		    public double getHeight() {  //后面的get...()和set...()是用来更改前面设置的变量的初始值
    		        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 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);
    		    }
    		    public static void main(String args[]) {
    		    	Rectangle rec=new Rectangle();       //创建一个对象
    		        rec.setWidth(10);                             //用点方法调用前面的方法
    		        rec.setHeight(10);
    		        rec.setColor("红色");
    		        rec.getArea();
    		        rec.getLength();
    		        System.out.println("长:"+rec.getWidth()+"
    高:"+rec.getHeight()+"
    颜色:"+rec.getColor());         //输出
    		    }
    		}
    
    
    

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

    实验代码:

    
    package hlha;
    
    import java.util.Scanner;
    public class Yinhang{
        private String name,date="2019.9.19";                     
        private int cipher=123456,money=0;   
        private String ccount="dp123";
            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 getCipher() {
            return cipher;
        }
        public void setCipher(int cipher) {
            this.cipher = cipher;
        }
        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();
                System.out.print("请输入开户时间:");
                Scanner userdate = new Scanner(System.in);
                String date = userdate.nextLine();
                setName(name);                            
                setDate(date);                                                                     
                System.out.print("开户成功!
    初始密码:123456
    开户时间:"+date+"
    ");
            }
            public void ChangePWD(){                                     
                System.out.print("请输入更改密码:");
                Scanner usercipher = new Scanner(System.in);
                int cipher = usercipher.nextInt();
                setCipher(cipher);                              
                System.out.print("更改密码成功!
    新密码:"+cipher+"
    ");
            }
            public void all(){                         
                System.out.print("用户名:"+name+"    开户日期:"+date+"
    账号:"+ccount+"    密码:"+cipher+"
    余额:"+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;
                setMoney(money);
                }
                else {
                System.out.print("存款数:");
                Scanner usermoney = new Scanner(System.in);
                int money = usermoney.nextInt();
                money=this.money+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;
                Yinhang user = new Yinhang();         
                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("已安全退出");
            }   
    }
    
    

    学习总结:
    这周主要学习了String类string类中常用的方法。还有字符串内容不可变。
    学习了 == 与 equals()之间的区别。"=="用来进行地址值比较,"equals()"是进行内容的比较的数组之间的转换。
    还有学习的对象数组,对象数组的初始化,对象数组也分为静态初始化和动态初始化。

  • 相关阅读:
    Java多线程运行机制的基本原理
    JAVA IO中的设计模式
    C++ 内存泄露处理方法 (转)
    使用PageHeap.EXE或GFlags.EXE检查内存越界错误 (转)
    32位Windows7上8G内存使用感受+xp 32位下使用8G内存 (转)
    windows 32位系统中进程最大可用内存空间为3GB (转)
    OpenGL 4.0 GLSL 实现 投影纹理映射(Projective Texture Mapping) (转)
    DebugView 调试工具
    剑桥中国史:“571”——林彪的未遂政变(转)
    邱会作与儿子对话:林彪的莫须有之罪(转)
  • 原文地址:https://www.cnblogs.com/dapeng1234/p/11545269.html
Copyright © 2011-2022 走看看