zoukankan      html  css  js  c++  java
  • Java第四周编程总结

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

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

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

    (3) 提供计算面积的getArea()方法和计算周长的getLength()方法
    一,实验代码

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

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

    package TXT2;
    
    import java.text.DecimalFormat; //格式化数字
    import java.util.Scanner; //调用控制台输入信息
    
    class Account {
     private String id,name;
     private int time,key;
     private double balance;
     public Account(String id,String name,int time,int key,int balance){
     this.setId(id);
     this.setName(name);
     this.setTime(time);
     this.setKey(key);
     this.setBalance(balance);
     }
     public /static/ void deposit(){ //此处static
     System.out.println("1.存款 2.取款");
    System.out.print("再次输入你要进行的操作的序号:");
    DecimalFormat df=new DecimalFormat("0.00");
     Scanner sc=new Scanner(System.in); //接受控制台输入,同下所有Scanner sc=new Scanner(......)
     int a1=sc.nextInt();
     if(a1==1){
     System.out.println("当前余额:"+df.format(+getBalance())+"元");
    System.out.print("请输入要存入的钱数:");
    double money1=sc.nextDouble();
     setBalance(getBalance()+money1);
     System.out.println("成功!当前余额:"+df.format(+getBalance())+"元");
     }
    else if(a1==2){
     System.out.println("当前余额:"+df.format(+getBalance())+"元");
    System.out.print("请输入要取出的钱数:");
    double money2=sc.nextDouble();
     setBalance(getBalance()-money2);
     if(getBalance()<0){
     System.out.println("余额不足!");
     }
    else{
     System.out.println("成功!当前余额:"+df.format(+getBalance())+"元");
     }
     }
    }public /*static*/ void change(){               
        System.out.print("请设置新密码:");
        Scanner sc=new Scanner(System.in);
        int key1=sc.nextInt();
        System.out.print("请再次确认密码:");
        int key2=sc.nextInt();
        if(key1==key2){
            setKey(key1);            
            System.out.println("设置成功!你的密码为:"+getKey());
        }
        else{
            System.out.println("两次输入的密码不同!");
        }
        
    }
    public /*static*/void inquire(){              
        System.out.print("输入六位数密码查询信息:");
        Scanner sc=new Scanner(System.in);
        int data=sc.nextInt();
        if(data==getKey()){
        DecimalFormat df=new DecimalFormat("0.00");      
        System.out.println("标识:"+getId());
        System.out.println("姓名:"+getName());
        System.out.println("开户日期:"+getTime());
        System.out.println("余额:"+df.format(+getBalance())+"元");
        }
        else{
            System.out.println("密码错误");
        }
    }
    public String getId(){
        return id;
    }
    public void setId(String i){
        id=i;
    }
    public String getName(){
        return name;
    }
    public void setName(String n){
        name=n;
    }
    public int getTime(){
        return time;
    }
    public void setTime(int t){
        time=t;
    }        
    public int getKey(){
        return key;
    }
    public void setKey(int k){
        key=k;
    }
    public double getBalance(){
        return balance;
    }
    public void setBalance(double b){
        balance=b;
    }
    }
     class classdemo3 {
    public static void main(String[] args) {
        boolean R=false;
        Account ac=new Account("Hazelnut826","陈振国",20190919,101010,0);
        while(!R)
        {
        System.out.println("1.存取款");
        System.out.println("2.修改密码");
        System.out.println("3.查询信息");
        System.out.println("4.退出程序");
        System.out.print("请输入你要进行的操作的序号:");
        Scanner sc=new Scanner(System.in);
        int a=sc.nextInt();
        if(a==1){
            ac.deposit();    
        }
        else if(a==2){
            ac.change();     
            
        }
        else if(a==3){
            ac.inquire();       
        }
        else if(a==4){
            System.out.println(" 感谢使用!");
            break;
        }
      }
    
    }
     }
    

    实验总结:
    总是编译错误

  • 相关阅读:
    ASP.NET的一些概念
    破解金盘gdlisxp系统
    对1,3,4,6,7,8组合显示成“1,3-4,6-8”方法
    C#关于Sort排序问题
    ISBN和标准编码关系以及概念
    sql批量修改插入数据
    C#winform导出数据到Excel的类
    C#winform初试报表
    JS解析XML文件和XML字符串
    java中String,StringBuffer,StringBuilder之间的区别
  • 原文地址:https://www.cnblogs.com/dxl1314520/p/11558173.html
Copyright © 2011-2022 走看看