zoukankan      html  css  js  c++  java
  • 第四周实验总结

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

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

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

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

    public class Rectangle {     
            private String color="红色";
            private double width;
            private double height;
            public void setColor(String color) {         //构造法为属性初始化         
                this.color=color;
            }
            public void setWidth(double width) {
                this.width=width;
            }
            public void setHeight(double height) {
                this.height=height;
            }
            public String getColor() {
                return color;
            }
            public double getWidth() {
                return width;
            }
            public double getHeight() {
                return height;
            }
            public  double getArea() {          //计算面积
                return width*height;
            }
            public double getLength() {           //计算周长
                return 2*(width+height);    
            }
                public static void main(String args[]) {
                    Rectangle r=new Rectangle();
                    r.setWidth(29);
                    r.setHeight(30);
                    System.out.println("高 ="+r.getHeight());
                    System.out.println("宽 ="+r.getWidth());
                    System.out.println("颜色为:"+r.getColor());
                    System.out.println("面积为:"+r.getArea());
                    System.out.println("周长为:"+r.getLength());
                    
            }
            
        }

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

    import java.util.Scanner;
    public class Account {
    public int id;
    public int password;
    public String name;
    public int money;
    public Account(int id, int password, String name, int money) {
    this.id = id;
    this.password = password;
    this.name = name;
    this.money = money;
    }
    public void show(){
    System.out.println("账户:" + id);
    System.out.println("姓名:" + name);
    System.out.println("余额:" + money);
    }
    public void takeMoney(){
    while(true){
    Scanner sc = new Scanner(System.in);
    System.out.println("输入密码");
    int pass = sc.nextInt();
    if(pass == password){
    System.out.println("取款金额:");
    int withdrawals = sc.nextInt();
    if(withdrawals <= money) {
    money= money-withdrawals;
    System.out.println("余额为:" + money);

    else {
    System.out.println("当前余额不足" );
    }
    break;
    }
    else{
    System.out.println("密码错误,请重新输入!");
    }
    }
    }
    public void saveMoney(int moneys){ 
    money = money+moneys;
    System.out.println("此次存款为:" + moneys);
    System.out.println("账户余额为:" + money);
    }
    public static void main(String[] args) {
    Account acc = new Account(20188496,123456,"黄楠",10000000);
    Scanner sc = new Scanner(System.in);
    System.out.println("请输入需要执行的操作");
    System.out.println("1银行账户信息");
    System.out.println("2取款操作");
    System.out.println("3存款操作");
    System.out.println("4退出系统");
    int s = sc.nextInt();
    switch(s) {
    case 1:
    System.out.println("银行账户");
    acc.show();
    break;
    case 2:
    System.out.println("取款");
    acc.takeMoney();
    break;
    case 3:
    System.out.println("存款");
    acc.saveMoney(1000);
    break;
    case 4:
    System.exit(0);
    break;
    }}}

  • 相关阅读:
    Execution Context(EC) in ECMAScript
    Prototype Chain
    一次websocket的抓包体验
    nodejs 解析 base64 文本
    curl常用命令行总结
    nodejs stream基础知识
    typedarrays splice
    nodejs stream & buffer 互相转换
    nodejs buffer 总结
    ajax stream 一边下载二进制数据一边处理
  • 原文地址:https://www.cnblogs.com/qq2676311181/p/11559247.html
Copyright © 2011-2022 走看看