this练习题

练习1:添加必要的构造器,综合应用构造器的重载,this关键字。

package chapter07_oop2_teacher.src.com.atguigu01._this.exer1;

/**
 * ClassName: Boy
 * Description:
 *
 * @Author 尚硅谷-宋红康
 * @Create 14:03
 * @Version 1.0
 */
public class Boy {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Boy() {
    }

    public Boy(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void marry(Girl girl){
        System.out.println("我想娶" + girl.getName());
    }

    public void shout(){
        if(this.age >= 22){
            System.out.println("我终于可以结婚了!");
        }else{
            System.out.println("我只能多谈恋爱了");
        }
    }

}

 

 

package chapter07_oop2_teacher.src.com.atguigu01._this.exer1;

/**
 * ClassName: BoyGirlTest
 * Description:
 *
 * @Author 尚硅谷-宋红康
 * @Create 14:16
 * @Version 1.0
 */
public class BoyGirlTest {
    public static void main(String[] args) {

        Boy boy1 = new Boy("杰克",24);
        Girl girl1 = new Girl("朱丽叶",20);

        girl1.marry(boy1);

        boy1.shout();

        Girl girl2 = new Girl("肉丝",18);
        int compare = girl1.compare(girl2);
        if(compare > 0){
            System.out.println(girl1.getName() + "大");
        }else if(compare < 0){
            System.out.println(girl2.getName() + "大");
        }else{
            System.out.println("一样大");
        }


    }
}

 

 

package chapter07_oop2_teacher.src.com.atguigu01._this.exer1;

/**
 * ClassName: Girl
 * Description:
 *
 * @Author 尚硅谷-宋红康
 * @Create 14:03
 * @Version 1.0
 */
public class Girl {
    private String name;
    private int age;

    public Girl() {

    }

    public Girl(String name, int age) {
        this();
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void marry(Boy boy){
        System.out.println("我想嫁给" + boy.getName());

        boy.marry(this);
    }

    /**
     * 比较两个Girl对象的大小。
     * @param girl
     * @return 正数:当前对象大 ; 负数:当前对象小(或形参girl大) ; 0:相等
     */
    public int compare(Girl girl){
        if(this.age > girl.age){
            return 1;
        }else if(this.age < girl.age){
            return -1;
        }else{
            return 0;
        }

    }
}

练习2:

(1)按照如下的UML类图,创建相应的类,提供必要的结构:

 

在提款方法withdraw()中,需要判断用户余额是否能够满足提款数额的要求,如果不能,应给出提示。deposit()方法表示存款。

(2)按照如下的UML类图,创建相应的类,提供必要的结构

 

 

(3)按照如下的UML类图,创建相应的类,提供必要的结构  

 

  • addCustomer 方法必须依照参数(姓,名)构造一个新的 Customer对象,然后把它放到 customer 数组中。还必须把 numberOfCustomer 属性的值加 1。

  • getNumOfCustomers 方法返回 numberofCustomers 属性值。

  • getCustomer方法返回与给出的index参数相关的客户。

(4)创建BankTest类,进行测试。

内存解析图:

 

代码:

package chapter07_oop2_teacher.src.com.atguigu01._this.exer2;

/**
 * ClassName: Account
 * Description:
 *      账户类
 * @Author 尚硅谷-宋红康
 * @Create 14:22
 * @Version 1.0
 */
public class Account {
    private double balance;//余额

    public Account(double init_balance) {
        this.balance = init_balance;
    }

    public double getBalance() {
        return balance;
    }

    //存钱
    public void deposit(double amt){
        if(amt > 0){
            balance += amt;
            System.out.println("成功存入:" + amt);
        }
    }

    //取钱
    public void withdraw(double amt){
        if(balance >= amt && amt > 0){
            balance -= amt;
            System.out.println("成功取出:" + amt);
        }else{
            System.out.println("取款数额有误或余额不足");
        }
    }
}

package chapter07_oop2_teacher.src.com.atguigu01._this.exer2;

/**
 * ClassName: Bank
 * Description:
 *      银行类
 * @Author 尚硅谷-宋红康
 * @Create 14:32
 * @Version 1.0
 */
public class Bank {
    private Customer[] customers;//用于保存多个客户
    private int numberOfCustomer;//用于记录存储的客户的个数

    public Bank(){
        customers = new Customer[10];
    }

    /**
     * 将指定姓名的客户保存在银行的客户列表中
     * @param f
     * @param l
     */
    public void addCustomer(String f,String l){
        Customer cust = new Customer(f,l);
//        customers[numberOfCustomer] = cust;
//        numberOfCustomer++;
        //或
        customers[numberOfCustomer++] = cust;
    }

    /**
     * 获取客户列表中存储的客户的个数
     * @return
     */
    public int getNumOfCustomers(){
        return numberOfCustomer;
    }

    /**
     * 获取指定索引位置上的客户
     * @param index
     * @return
     */
    public Customer getCustomer(int index){
        if(index < 0 || index >= numberOfCustomer){
            return null;
        }

        return customers[index];

    }
}

 

package chapter07_oop2_teacher.src.com.atguigu01._this.exer2;

/**
 * ClassName: BankTest
 * Description:
 *
 * @Author 尚硅谷-宋红康
 * @Create 14:35
 * @Version 1.0
 */
public class BankTest {
    public static void main(String[] args) {
        Bank bank = new Bank();

        bank.addCustomer("操","曹");
        bank.addCustomer("备","刘");

        bank.getCustomer(0).setAccount(new Account(1000));
        bank.getCustomer(0).getAccount().withdraw(50);

        System.out.println("账户余额为:" + bank.getCustomer(0).getAccount().getBalance());
    }
}

 

package chapter07_oop2_teacher.src.com.atguigu01._this.exer2;

/**
 * ClassName: Customer
 * Description:
 *      客户类
 * @Author 尚硅谷-宋红康
 * @Create 14:25
 * @Version 1.0
 */
public class Customer {
    private String firstName;//名
    private String lastName;//姓

    private Account account; //账户

    public Customer(String f, String l) {
        this.firstName = f;
        this.lastName = l;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public Account getAccount() {
        return account;
    }

    public void setAccount(Account account) {
        this.account = account;
    }
}

相关推荐

  1. MIT-Missing Semester_Topic 1: The Shell 练习题

    2024-03-30 13:36:03       29 阅读
  2. this::onCheckedChanged

    2024-03-30 13:36:03       13 阅读
  3. vue this.$set()、this.$delete使用方法

    2024-03-30 13:36:03       15 阅读
  4. this的指向

    2024-03-30 13:36:03       33 阅读
  5. C++ this 指针

    2024-03-30 13:36:03       29 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-30 13:36:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-30 13:36:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-30 13:36:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-30 13:36:03       20 阅读

热门阅读

  1. 在ROS2-foxy环境中配置nooploop-linktrack

    2024-03-30 13:36:03       17 阅读
  2. Android JNI---入门了解

    2024-03-30 13:36:03       14 阅读
  3. 信号量或互斥锁

    2024-03-30 13:36:03       19 阅读
  4. VSCode中6个AI顶级插件

    2024-03-30 13:36:03       29 阅读
  5. Openreview公式不能正常显示

    2024-03-30 13:36:03       12 阅读
  6. linux终端介绍

    2024-03-30 13:36:03       18 阅读
  7. ChatGPT助力学术写作:快速提升论文质量

    2024-03-30 13:36:03       19 阅读
  8. React中使用antDesign框架

    2024-03-30 13:36:03       17 阅读
  9. 使用 React Router v6.22 进行导航

    2024-03-30 13:36:03       17 阅读