JavaSE——面向对象练习题

embedded/2024/10/18 6:07:01/

1.对象数组排序

        定义一个Person类{name,age,job},初始化Person对象数组,有3个person对象,并按照age从小到大进行冒泡排序;再按照name的长度从小到大进行选择排序。

java">public class HomeWork01 {public static void main(String[] args) {Person[] persons = new Person[4];persons[0] = new Person("zhangsan", 18, "java工程师");persons[1] = new Person("lisi", 32, "项目经理");persons[2] = new Person("wangwu", 10, "小学生");persons[3] = new Person("tom", 50, "BOSS");System.out.println("原始数组:" + Arrays.toString(persons));for (int i = 0; i < persons.length - 1; i++) {for (int j = 0; j < persons.length - 1 - i; j++) {if (persons[j].getAge() > persons[j + 1].getAge()) {Person temp = persons[j];persons[j] = persons[j + 1];persons[j + 1] = temp;}}}System.out.println("按照年龄[从小到大]进行冒泡排序:" + Arrays.toString(persons));for (int i = 0; i < persons.length - 1; i++) {for (int j = i + 1; j < persons.length; j++) {if (persons[i].getName().length() > persons[j].getName().length()) {Person temp1 = persons[i];persons[i] = persons[j];persons[j] = temp1;}}}System.out.println("按照姓名长度[从小到大]进行选择排序:" +Arrays.toString(persons));}
}class Person {private String name;private int age;private String job;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 String getJob() {return job;}public void setJob(String job) {this.job = job;}public Person(String name, int age, String job) {this.name = name;this.age = age;this.job = job;}@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", age=" + age +", job='" + job + '\'' +'}';}
}

运行结果:

2.superthis区别 

3.判断输出结果(superthis区分)

java">public class Homework07 {public static void main(String[] args) {}
}class Test { //父类String name = "Rose";Test() {System.out.println("Test");//(1)}Test(String name) {//name johnthis.name = name;//这里把父类的 name 修改 john}
}class Demo extends Test {//子类String name = "Jack";Demo() {super();System.out.println("Demo");//(2)}Demo(String s) {super(s);}public void test() {System.out.println(super.name);//(3) Rose (5) johnSystem.out.println(this.name);//(4) Jack (6) Jack}public static void main(String[] args) {//1. new Demo()new Demo().test(); //匿名对象new Demo("john").test();//匿名}
}

运行结果:

4.模拟银行存取款业务

java">class BankAccount {private double balance; // 余额public BankAccount(double initialBalance) {this.balance = initialBalance;}// 存款public void deposit(double amount) {balance += amount;}// 取款public void withdraw(double amount) {balance -= amount;}
}

题目要求:

(1)在上面类的基础上扩展新类CheckingAccount,对每次存款和取款都收取1美元的手续费。

首先,我们对BankAccount类添加get与set方法:

java">class BankAccount {private double balance; // 余额public BankAccount(double initialBalance) {this.balance = initialBalance;}// 存款public void deposit(double amount) {balance += amount;}// 取款public void withdraw(double amount) {balance -= amount;}public double getBalance() {return balance;}public void setBalance(double balance) {this.balance = balance;}
}

创建新类CheckingAccount:

java">class CheckingAccount extends BankAccount {public CheckingAccount(double initialBalance) {super(initialBalance);}/*** 存款收取1美元手续费,相当于少存1美元** @param amount*/@Overridepublic void deposit(double amount) {super.deposit(amount - 1);}/*** 取款收取1美元手续费,相当于再给银行1美元** @param amount*/@Overridepublic void withdraw(double amount) {super.withdraw(amount + 1);}
}

结果检验:

java">public class HomeWork08 {public static void main(String[] args) {CheckingAccount checkingAccount = new CheckingAccount(1000);checkingAccount.deposit(100);System.out.println(checkingAccount.getBalance()); // 1099.0checkingAccount.withdraw(20);System.out.println(checkingAccount.getBalance()); // 1078.0}
}

(2)扩展前一个练习的BankAccount类,新类SavingsAccount每个月都有利息产生(earnMonthlyInterest方法被调用),并且有每月三次免手续费的存款或取款。在earnMonthlyInterest方法中重置交易计数。

SavingsAccount类:

java">class SavingsAccount extends BankAccount {// 定义计费次数private int count = 3;// 定义银行利率private double rate = 0.01;public SavingsAccount(double initialBalance) {super(initialBalance);}/*** 每月重新计算利率*/public void earnMonthlyInterest() {// 重新初始化计费次数this.count = 3;// 每月重新计算计算余额super.deposit(getBalance() * rate);}/*** 重写存款方法,存款前查看存款是否超过3次** @param amount 存入的金额*/@Overridepublic void deposit(double amount) {if (count > 0) {super.deposit(amount);} else {super.deposit(amount - 1);}count--;}/*** 重写取款方法,取款前查看取款是否超过3次** @param amount 取款的金额*/@Overridepublic void withdraw(double amount) {if (count > 0) {super.withdraw(amount);} else {super.withdraw(amount + 1);}count--;}public int getCount() {return count;}public void setCount(int count) {this.count = count;}public double getRate() {return rate;}public void setRate(double rate) {this.rate = rate;}
}

结果检验: 

java">public class HomeWork08 {public static void main(String[] args) {   SavingsAccount savingsAccount = new SavingsAccount(1000);savingsAccount.deposit(100);savingsAccount.deposit(100);savingsAccount.deposit(100);System.out.println(savingsAccount.getBalance()); // 1300.0savingsAccount.withdraw(10); // 第4次存取,需要征收手续费System.out.println(savingsAccount.getBalance()); // 1289.0// 月初初始化计费次数savingsAccount.earnMonthlyInterest();System.out.println(savingsAccount.getBalance()); // 1301.89savingsAccount.deposit(100);System.out.println(savingsAccount.getBalance()); // 1401.89savingsAccount.withdraw(50);savingsAccount.withdraw(50);savingsAccount.withdraw(50); // 征收手续费System.out.println(savingsAccount.getBalance()); // 1250.89}
}

5.重写equals方法判断两个对象是否相等

        编写Doctor类{name,age,job,gender,sal}相应的getter()和setter()方法,5个参数的构造器,重写父类(Object)的equals()方法:public boolean equals(Object obj),并判断测试类中创建的两个对象是否相等。相等就是判断属性是否相同。 

java">public class HomeWork10 {public static void main(String[] args) {Doctor doctor = new Doctor("张三", 32, "主治医师", '男', 25000);Doctor doctor2 = new Doctor("张三", 32, "主治医师", '男', 25000);System.out.println(doctor.equals(doctor2)); // true}
}class Doctor {private String name;private int age;private String job;private char gender;private double sal;@Overridepublic boolean equals(Object obj) {if (this == obj) {return true;}// 判断obj 是否是 Doctor类型或其子类if (!(obj instanceof Doctor)) {return false;}// 向下转型, 因为obj的运行类型是Doctor或者其子类型Doctor doctor = (Doctor) obj;return this.name.equals(doctor.getName()) &&this.age == doctor.getAge() &&this.job.equals(doctor.getJob()) &&this.gender == doctor.getGender() &&this.sal == doctor.getSal();}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 String getJob() {return job;}public void setJob(String job) {this.job = job;}public char getGender() {return gender;}public void setGender(char gender) {this.gender = gender;}public double getSal() {return sal;}public void setSal(double sal) {this.sal = sal;}public Doctor(String name, int age, String job, char gender, double sal) {this.name = name;this.age = age;this.job = job;this.gender = gender;this.sal = sal;}

6.写出向上转型和向下转型代码 

java">public class HomeWork11 {public static void main(String[] args) {// 向上转型代码// 父类的引用指向子类对象Person2 p = new Student();p.run(); // student runp.eat(); // person eat// 向下转型代码// 把指向子类对象的父类引用,转成指向子类对象的子类引用Student student = (Student) p;// 注意动态绑定问题student.run(); // student runstudent.study(); // student study..// 因为Student是Person2的子类,所以可以调用父类eat方法student.eat(); // person eat}
}class Person2 {public void run() {System.out.println("person run");}public void eat() {System.out.println("person eat");}
}class Student extends Person2 {@Overridepublic void run() {System.out.println("student run");}public void study() {System.out.println("student study..");}
}

7.判断输出结果

java">public class HomeWork14 {public static void main(String[] args) {C c = new C();}
}class A {public A() {System.out.println("我是A类");}
}class B extends A {public B() {System.out.println("我是B类的无参构造");}public B(String name) {System.out.println(name + "我是B类的有参构造");}
}class C extends B {public C() {this("hello");System.out.println("我是C类的无参构造");}public C(String name) {super("hahah");System.out.println("我是C类的有参构造");}
}

运行结果:


http://www.ppmy.cn/embedded/123739.html

相关文章

Python办公自动化教程(006):Word添加标题

2.3 word标题 介绍&#xff1a; 在 python-docx 中&#xff0c;您可以使用 add_heading() 方法为文档添加标题。此方法允许您指定标题的文本和级别&#xff08;例如&#xff0c;一级标题、二级标题等&#xff09;。标题级别的范围是从 0 到 9&#xff0c;其中 0 表示文档的主标…

RabbitMQ的各类工作模式介绍

简单模式 P: ⽣产者, 也就是要发送消息的程序 C: 消费者,消息的接收者 Queue: 消息队列, 图中⻩⾊背景部分. 类似⼀个邮箱, 可以缓存消息; ⽣产者向其中投递消息, 消费者从其中取出消息.特点: ⼀个⽣产者P&#xff0c;⼀个消费者C, 消息只能被消费⼀次. 也称为点对点(Point-to-…

Redis SpringBoot项目学习

Redis 是一个高性能的key-value内存数据库。它支持常用的5种数据结构&#xff1a;String字符串、Hash哈希表、List列表、Set集合、Zset有序集合 等数据类型。 Redis它解决了2个问题&#xff1a; 第一个是&#xff1a;性能 通常数据库的读操作&#xff0c;一般都要几十毫秒&…

qt cmake自定义资源目录,手动加载资源(图片, qss文件)

1. 目录创建 因为使用非qtcreator或者自定义工程结构就没法自动加载图标&#xff0c;所以需要手动加载&#xff0c;这里使用vscode和自定义工程结构 vscode qt 环境搭建&#xff1a; https://blog.csdn.net/qq_51355375/article/details/140733495 qt 自定义工程结构参考(因使…

滚雪球学Oracle[2.3讲]:Oracle Listener配置与管理

全文目录&#xff1a; 前言一、Oracle Listener的基础概念1.1 什么是Oracle Listener&#xff1f;Listener的作用&#xff1a; 1.2 Oracle Listener的配置文件示例listener.ora配置文件&#xff1a; 1.3 启动与管理Listener 二、多Listener配置与负载分担2.1 多Listener的应用场…

Linux中配置docker环境

1.基础信息了解 &#xff08;1&#xff09;docker信息了解&#xff0c;docker官网 https://www.docker.com/&#xff0c;docker中文网 https://dockerdocs.cn/&#xff0c;还可以了解下虚拟化和容器化的知识&#xff0c;虚拟化和容器化 &#xff08;2&#xff09;查看当前Lin…

C++学习笔记----8、掌握类与对象(四)---- 不同类型的数据成员(2)

3、引用数据成员 Spreadsheet与SpreadsheetCell是伟大的&#xff0c;但是不是它们自己就能成为有用的应用程序。需要代码去控制整个spreadsheet程序&#xff0c;可以将其打包成一个SpreadsheetApplication类。假定接下来我们要让每个Spreadsheet来保存一个应用程序对象的引用。…

ES6新标准下关于数组的几个实用改进功能介绍

ES5中对数组的操作方法还不完善&#xff0c;对于一些其他语言较常见的功能还需要用间接的方法去实现&#xff0c;因而在ES6有力的进行了改进&#xff0c;主要有以下几个方面&#xff1a; 一、创建数组上的改进 在ES5及以前&#xff0c;创建数组的方式有两种&#xff0c;一种是…