第七题
答案
package com.hspedu.homework.homework07;
public class HomeWork07 {
public static void main(String[] args) {
}
}
class Test{ //父类
String name = "Rose";
Test(){
System.out.println("Test");
}
Test(String name){
this.name = name;
}
}
class Demo extends Test{
String name= "Jack";
Demo() {
super();
System.out.println("Demo");
}
Demo(String s){
super(s);
}
public void test(){
System.out.println(super.name);
System.out.println(this.name);
}
public static void main(String[] args) {
new Demo().test();
new Demo("john").test();
}
}
第八题
题目
BankAccount类
package com.hspedu.homework.homework08;
class BankAccount {
private double balance;
private double charge;
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;
}
public double getCharge() {
return charge;
}
public void setCharge(double charge) {
this.charge = charge;
}
}
因为在CheckingAccount类和SavingsAccount类中都需要用到手续费,将charge在父类中以属性的形式定义,并且不放在构造器中,这样可以灵活应用
CheckingAccount类
package com.hspedu.homework.homework08;
public class CheckingAccount extends BankAccount {
// (1)在上面类的基础上扩展 新类CheckingAccount
// 对每次存款和取款都收取1美元的手续费用
public CheckingAccount(double initialbalance) {
super(initialbalance);
}
@Override
public void deposit(double amount) {
//按照下面这种写法,先计算balanace = balanace + amount
//然后得到(balanace-1)的值,在调用方法时,返回这个值并输出
//比如原来余额有1000,存进去100,最后输出的余额是1099,
// 但是JVM中balance还是1100,出现了偏差
//return super.deposite(amount) - charge;
//如果要存100,直接传(100-1)进去
super.deposit(amount - getCharge());
}
@Override
public void withdraw(double amount) {
// //如果要取100,直接传(100+1)进去
super.withdraw(amount + getCharge());
}
}
重点在于重写的父类的存款和付款方法内的方法体,存款就是把扣掉手续费之后的金额传参给父类的存款方法,取款就是把加上手续费之后的金额传参给父类的存款方法,
main类运行
package com.hspedu.homework.homework08;
public class HomeWork08 {
public static void main(String[] args) {
CheckingAccount checkingAccount = new CheckingAccount(100);
checkingAccount.setCharge(1);
checkingAccount.deposit(100);
//查看余额
System.out.println(checkingAccount.getBalance());//199
checkingAccount.withdraw(100);
System.out.println(checkingAccount.getBalance());//98
}
}
运行结果
SavingsAccount类
题目解读
1、需要创建一个新类-SavingsAccount类,继承BankAccount类,在这个类中有一个方法earnMonthlylInterest计算每个月的利息
2、并且在这个类中,用户每个月有三次存取款免手续费的机会,机会用完之后,在earnMonthlylInterest中重置机会的数值
解决思路
1、定义一个变量count,并赋初始值3,用if-else分支重写父类存取款方法,控制免手续费的次数
2、定义变量rate,计算利息,并且通过super关键字调用父类的存款方法,将利息存到银行的账户中(是调用而不是重写,也就是不收取手续费,也不会用掉count的次数)
package com.hspedu.homework.homework08;
public class SavingsAccount extends BankAccount{
// (2)扩展前一个练习的BankAccount类,
// 新类SavingsAccount每个月都有利息产生
// (earnMonthlyInterest方法被调用),
// 并且有每月三次免手续费的存款或取款。
// 在earnMonthlylInterest方法中重置交易计数
private int count = 3;//次数
private double rate = 0.01;//利率
public SavingsAccount(double initialbalance) {
super(initialbalance);
}
@Override
//原来写的是public double deposit,在方法的最后一句必定要return一个值
//而此方法的最后一句是count--,系统一直报错
//存款
public void deposit(double amount) {
//判断是否还可以免手续费
if(count > 0){
super.deposit(amount);
}else{
super.deposit(amount-getCharge());
}
count--;
}
//取款
@Override
public void withdraw(double amount) {
if(count > 0){
super.withdraw(amount);
}else{
super.withdraw(amount + getCharge());
}
count--;
}
//计算利息
public void earnMonthlylInterest(){
//每个月月初统计上个月的利息,同时重置免收手续费的次数
count = 3;
//计算利息,调用父类的存款方法,把利息存进余额,因为此时调用的是父类的存款方法
//上述有3次机会的是子类重写父类的存款方法,所以是不会用掉3次免手续费的机会的
super.deposit(getBalance() * rate);
}
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;
}
}
main类
package com.hspedu.homework.homework08;
public class HomeWork08 {
public static void main(String[] args) {
//测试SavingsAccount
SavingsAccount savingsAccount = new SavingsAccount(1000);
//存取款的手续费是1元
savingsAccount.setCharge(1);
savingsAccount.deposit(100);
savingsAccount.deposit(100);
savingsAccount.withdraw(100);
System.out.println(savingsAccount.getBalance());//1100
savingsAccount.withdraw(100);//存取三次之后就开始收手续费
System.out.println(savingsAccount.getBalance());//999
//给定一个计时器,到月初的时候执行earnMonthlylInterest方法
savingsAccount.earnMonthlylInterest();
System.out.println(savingsAccount.getBalance());//999 + 999*0.01=1008.99
savingsAccount.withdraw(100);
savingsAccount.withdraw(100);
savingsAccount.withdraw(100);
System.out.println(savingsAccount.getBalance());//708.99
savingsAccount.deposit(100);
System.out.println(savingsAccount.getBalance());//807.99
}
}
运行结果
第九题
Point类
package com.hspedu.homework.homework09;
public class Point {
private double x;
private double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
}
LabeledPoint类
package com.hspedu.homework.homework09;
public class LabeledPoint extends Point{
private String label;
public LabeledPoint(String label,double x, double y) {
super(x, y);
this.label = label;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
}
main类
package com.hspedu.homework.homework09;
public class HomeWork09 {
public static void main(String[] args) {
LabeledPoint thursday = new LabeledPoint("Black Thursday", 1929, 230.07);
}
}
第十题
题目
Doctor类
package com.hspedu.homework.homework10;
public class Doctor {
// 10.编写Poctor类{name, age,job, gender, sal}
// 5个参数的构造器,重写父类的equals0方法:
// public相应的getter0和setter0方法,
// 并判断测试类中创建的两个对象是否相等。
// 相等就是判断boolean equals(Object obj)属性是否相同
private String name;
private int age;
private String job;
private char gender;
private double sal;
//重写Object类的equals方法,系统自动重写
// @Override
// public boolean equals(Object obj) {
// if (this == obj) return true;
// if (obj == null || getClass() != obj.getClass()) return false;
//
// Doctor doctor = (Doctor) obj;
//
// if (age != doctor.age) return false;
// if (gender != doctor.gender) return false;
// if (Double.compare(doctor.sal, sal) != 0) return false;
// if (name != null ? !name.equals(doctor.name) : doctor.name != null) return false;
// return job != null ? job.equals(doctor.job) : doctor.job == null;
// }
@Override
public boolean equals(Object obj) {
if(this == obj){
return true;
}
if(obj instanceof Doctor){
Doctor doctor = (Doctor) obj;
//name, age,job, gender, sal
return this.name.equals(doctor.name) && this.age == doctor.age &&
this.job.equals(doctor.job) && this.gender == doctor.gender && this.sal == doctor.sal;
}
return false;
}
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;
}
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;
}
}
main类
package com.hspedu.homework.homework10;
public class HomeWork10 {
public static void main(String[] args) {
// 并判断测试类中创建的两个对象是否相等。
//相等就是判断boolean equals(Object obj)属性是否相同
Doctor doctor1 = new Doctor("jay", 24, "住院医师", '男', 7000);
Doctor doctor2 = new Doctor("jay", 24, "住院医师", '男', 7000);
System.out.println(doctor1==doctor2);//输出false
System.out.println(doctor1.equals(doctor2));//输出true
}
}
运行结果
第十一题
题目
Person类
package com.hspedu.homework.homework11;
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void run(){
System.out.println("person run");
}
public void eat(){
System.out.println("person eat");
}
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;
}
}
Student类
package com.hspedu.homework.homework11;
class Student extends Person{
public Student(String name, int age) {
super(name, age);
}
@Override
public void run() {
System.out.println("student run");
}
public void study(){
System.out.println("student study..");
}
}
main类
package com.hspedu.homework.homework11;
public class HomeWork11 {
public static void main(String[] args) {
//向上转型
Person person = new Student("emma",30);
//在编译阶段,是看编译类型的,编译类型是Person,
//可以调用Person类的run和eat方法
// 因为子类-Student重写了父类的run方法,根据动态绑定机制,首先查找并调用Student类的run方法
person.run();//输出 student run
person.eat();//输出 person eat
//向下转型
Student student = (Student) person;
//调用Student类的run和study
student.run();//输出student run
student.study();//输出student study...
student.eat();//漏掉了这一点 person eat
}
}