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 test;
    
    class Rectangle {
      private double width,height;
      private  String color;
      public Rectangle(double width,double height,String color){
          this.setWidth(width);
          this.setHeight(height);
          this.setColor(color);
      }
      public void tell(){
          System.out.println("宽:"+width+"  "+"高:"+height+"  "+"颜色:"+color);
      }
      public double getWidth(){
          return width;
      }
      public double getHeight(){
          return height;
      }
      public String getColor(){
          return color;
      }
      public void setWidth(double width){
          this.width=width;
      }
      public void setHeight(double height){
          this.height=height;
      }
      public void setColor(String color) {
          this.color=color;
      }
      public void getArea(){
          System.out.println("面积为:"+(width*height));
      }
      public void getLength(){
          System.out.println("周长为:"+(width+height)*2);
      }
    
    
    
       public static void main(String[] args){
           Rectangle per=new Rectangle(30,60,"yellow");
           per.tell();
           per.getArea();
           per.getLength();
       }
    }
    
    


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

    
    package test;
    
    
    import java.util.Scanner;
    
    class bank {
      private String id;
      private String name;
      private String createtime;
      private int password;
      private int money;
    
      public bank(String id, String name, String createtime, int password, int money) {
          this.setId(id);
          this.setName(name);
          this.setCreatetime(createtime);
          this.setPassword(password);
          this.setMoney(money);
      }
    
      public bank(String string, String string2, String string3, String string4, String string5) {
          // TODO 自动生成的构造函数存根
      }
    
      public String getId() {
          return id;
      }
    
      public void setId(String id) {
          this.id = id;
      }
    
      public String getName() {
          return name;
      }
    
      public void setName(String name) {
          this.name = name;
      }
    
      public String getCreatetime() {
          return createtime;
      }
    
      public void setCreatetime(String createtime) {
          this.createtime = createtime;
      }
    
      public int getPassword() {
          return password;
      }
    
      public void setPassword(int password) {
          this.password = password;
      }
    
      public int getMoney() {
          return money;
      }
    
      public void setMoney(int money) {
          this.money = money;
      }
    
      public void kaihu() {
          System.out.println("您的账号标识符为:dl626489975");
          System.out.println("您的账户初始密码为123456");
          System.out.println("请输入您的真实姓名");
          Scanner sc = new Scanner(System.in);
          char name = sc.next().charAt(0);
          System.out.println("您的账户余额为0");
      }
    
      public void cunqu() {
          System.out.println("请输入查询账号密码");
          Scanner sc = new Scanner(System.in);
          int num = sc.nextInt();
          if (num == getPassword()) {
              System.out.println("您的当前余额为" + getMoney());
              int a = 0;
              while (a != 6) {
                  System.out.println("1:存钱");
                  System.out.println("2:取钱");
                  System.out.println("3:查询开户时间");
                  System.out.println("4:查询账户标识");
                  System.out.println("5:查询账号姓名");
                  System.out.println("6:退出查询");
                  Scanner sc1 = new Scanner(System.in);
                  a = sc1.nextInt();
    
                  if (a == 1) {
                      System.out.println("请输入您所需存金额");
                      Scanner sc11 = new Scanner(System.in);
                      int q = sc11.nextInt();
                      setMoney(getMoney() + q);
                      System.out.println("您的当前余额为" + getMoney());
                  } else if (a == 2) {
                      System.out.println("请输入您所需取金额");
                      Scanner sc111 = new Scanner(System.in);
                      int w = sc111.nextInt();
                      if (w > getMoney()) {
                          System.out.println("您的当前余额不足");
                      } else {
                          setMoney(getMoney() - w);
                          System.out.println("您的当前余额为" + getMoney());
                      }
                  } else if (a == 3) {
    
                      System.out.println("您的开户日期为" + getCreatetime());
                  } else if (a == 4) {
    
                      System.out.println("您的账户标识为" + getId());
                  } else if (a == 5) {
                      System.out.println("您的账户姓名为" + getName());
                  }
    
              }
          } else {
              System.out.println("您输入的密码有误");
          }
      }
    
      public void change() {
    
          System.out.println("请输入新密码");
          Scanner sc2 = new Scanner(System.in);
          int r = sc2.nextInt();
          System.out.println("请再次输入新密码");
          Scanner sc3 = new Scanner(System.in);
          int t = sc3.nextInt();
          if (t == r) {
              System.out.println("更改成功");
          } else {
              System.out.println("俩次输入密码不相同,请重新输入");
          }
    
      }
    
      public static void main(String[] args) {
          bank per = new bank("dl626489975", "邓林", "20181001", 123456, 1000);
          int y = 0;
          while (y != 4) {
              System.out.println("1.开户");
              System.out.println("2.查询与存取");
              System.out.println("3.更改密码");
              System.out.println("4.退出");
              System.out.println("请输入您下一步操作的数字:");
              Scanner sc5 = new Scanner(System.in);
              y = sc5.nextInt();
              if (y == 1) {
                  per.kaihu();
              } else if (y == 2) {
                  per.cunqu();
              } else if (y == 3) {
                  per.change();
              }
          }
      }
    }
    
    

    总结:
    在此声明第二题自己还不太会,这是请教同学的,本周学习状态没有上周好
    本周学习的内容
    一、学习了两种为String赋值的方法;
    1)
    String str="Alice";

    String str=new String("Alice");

    二,包的定义
    package是在使用多个类或接口时,为了避免名称重复而采用的一种措施,直接在程序中加入package关键字即可。

    三,对象数组
    类 对象数组名称[]=new 类[数组长度]
    数据类型 数组名称[]=null;
    数组名称=new 数据类型[长度];

  • 相关阅读:
    LeetCode 109 Convert Sorted List to Binary Search Tree
    LeetCode 108 Convert Sorted Array to Binary Search Tree
    LeetCode 107. Binary Tree Level Order Traversal II
    LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal
    LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
    LeetCode 103 Binary Tree Zigzag Level Order Traversal
    LeetCode 102. Binary Tree Level Order Traversal
    LeetCode 104. Maximum Depth of Binary Tree
    接口和多态性
    C# 编码规范
  • 原文地址:https://www.cnblogs.com/ll-dl2018/p/11558875.html
Copyright © 2011-2022 走看看