- 假设银行 Bank 已经有按整年计算利息的一般方法,其中 year
只能取整数,比如按整年计算方法为:year0.35savedMoncy。 建设银行 ConstructionBank 和大连银行
BankOfDalian 是 Bank 的子类,隐藏继承的成员变量 year,并重写计算利息的方法,即声明一个 double 型的变量
year。比如,当 year 取值为5.216 时,表示计算 5 年 216 天的利息。建设银行或大连银行把 5.216
的整数部分赋给隐藏的yea,并用 super 调用 Bank 的计算利息的方法,求出 5 年的利息,再按自己的方法计算 216
天的利息。建设银行计算日利息的方法:
day0.0001savedMoney,大连银行计算日利息的方法:day0.00012savedMoney。 测试类,求
8000 元存 5 年 236 天两银行的利息差额。Bank 类属性:
savedMoney、year、interest、interestRate=0.35
class Bank {
protected double savedMoney;
protected int year;
protected double interest;
protected double interestRate = 0.35;
public Bank(double savedMoney, int year) {
this.savedMoney = savedMoney;
this.year = year;
}
public double computerInterest() {
interest = year * interestRate * savedMoney;
return interest;
}
}
class ConstructionBank extends Bank {
private double year;
public ConstructionBank(double savedMoney, double year) {
super(savedMoney, (int) year);
this.year = year;
}
public double computerInterest() {
double intYears = super.computerInterest();
double intDays = year - (int) year;
interest = intYears + intDays * 0.0001 * savedMoney;
return interest;
}
}
class BankOfDalian extends Bank {
private double year;
public BankOfDalian(double savedMoney, double year) {
super(savedMoney, (int) year);
this.year = year;
}
public double computerInterest() {
double intYears = super.computerInterest();
double intDays = year - (int) year;
interest = intYears + intDays * 0.00012 * savedMoney;
return interest;
}
}
class TestBank {
public static void main(String[] args) {
Bank bank1 = new ConstructionBank(8000, 5.236);
Bank bank2 = new BankOfDalian(8000, 5.236);
double interest1 = bank1.computerInterest();
double interest2 = bank2.computerInterest();
System.out.println("Construction Bank Interest: " + interest1);
System.out.println("Bank of Dalian Interest: " + interest2);
System.out.println("Difference: " + (interest2 - interest1));
}
- (1)定义动物Animal,私有属性 sex,表示性别,数据类型为 boolean。
私有属性age,表示年龄,数据类型为 int。
属性的 getters 和setters 方法。
在类的构造方法中设置性别初始值为 false。
公有方法Introduce0,用于介绍动物。
若字段 sex 属性的值为 true,则方法返回一个字符串"This
is a male Animal!“.
若字段 sex 属性的值为 false,则方法返回一个字符串”This is a female
Animal!” (2)由基类Animal创建派生类 Dog,在派生类中实现方法重写。
创建公有类Dog,它是从基类 Animal中派生出的。
在类 Dog 的构造方法中,设置属性 sex 的初始值为 true。 》在类Dog中重写基类Animal的方法Introduce。
若属性 Sex 的值为 true,则方法返回一个字符串"This is a male
Dog!”。2若属性 Sex 的值为 false,则方法返回一个字符串“This is a female Dog!”。(3)定义测试类
AnimalTest,在程序主方法中实例化类的对象,调用方法输出介绍动物的字符串。
实例化Animal的一个对象ani,调用类的方法Introduce0,并输出方法返回的字符串。 实例化 Dog 的一个对象
dog,调用类的方法Introduce0,并输出方法返回的字符串。
class Animal {
private boolean sex;
private int age;
// Define getters and setters for the properties.
public boolean getSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
// Define a constructor that sets the initial value of sex to false.
public Animal() {
this.sex = false;
}
// Define a public method Introduce0 that introduces the animal.
public String Introduce0() {
// If the sex property is true, return "This is a male Animal!".
if (this.sex) {
return "This is a male Animal!";
}
// If the sex property is false, return "This is a female Animal!".
else {
return "This is a female Animal!";
}
}
}
// Create a public class Dog that is derived from the Animal base class.
class Dog extends Animal {
// Define a constructor that sets the initial value of sex to true.
public Dog() {
this.setSex(true);
}
// Override the Introduce0 method from the base class.
@Override
public String Introduce0() {
// If the sex property is true, return "This is a male Dog!".
if (this.getSex()) {
return "This is a male Dog!";
}
// If the sex property is false, return "This is a female Dog!".
else {
return "This is a female Dog!";
}
}
}
// Define a test class AnimalTest with a main method.
class AnimalTest {
public static void main(String[] args) {
// Instantiate an Animal object ani and call the Introduce0 method, then print the returned string.
Animal ani = new Animal();
System.out.println(ani.Introduce0());
// Instantiate a Dog object dog and call the Introduce0 method, then print the returned string.
Dog dog = new Dog();
System.out.println(dog.Introduce0());
}
3.要求设计abstract类,类名为 Employe,类内有抽象方法earning0,功能是计算工资Employee 的子类有 YearWorker、MonthWorker、WeekWorker。YearWorker 对象按年领取薪水,每年年薪 6 万元,MonthWorker 按月领取薪水,每月 3000 元,WeekWorker 按周领取薪水,每周 500元。有一个 Company 类,该类有两个属性,一个是用 Employee 数组作为属性,存放所有的员工,另一个是 salaries.属性,存放该公司每年该支付的总薪水,paySalaries 方法计算一年需支付的薪水总额。测试类定义 29 名员工,员工编号为 0~28,其中员工编号能被整除的员工为 WeekWorker,员工编号除 3 余数为1的是 MonthWorker,员工编号除3 余数为2的是 YearWorker,测试类输出该公司支付总金额。
abstract class Employee {
public abstract double earning0();
}
class YearWorker extends Employee {
public double earning0() {
return 60000;
}
}
class MonthWorker extends Employee {
public double earning0() {
return 3000;
}
}
class WeekWorker extends Employee {
public double earning0() {
return 500;
}
}
class Company {
private Employee[] employees;
private double salaries;
public Company(Employee[] employees) {
this.employees = employees;
}
public double paySalaries() {
for (Employee employee : employees) {
salaries += employee.earning0();
}
return salaries;
}
}
class Main {
public static void main(String[] args) {
Employee[] employees = new Employee[29];
double totalSalaries;
for (int i = 0; i < 29; i++) {
if (i % 3 == 0) {
employees[i] = new WeekWorker();
} else if (i % 3 == 1) {
employees[i] = new MonthWorker();
} else {
employees[i] = new YearWorker();
}
}
Company company = new Company(employees);
totalSalaries = company.paySalaries();
System.out.println("Total salaries: " + totalSalaries);
}
4.(1)编写 DogState 接口,接口中有 showState 方法。(2)编写若干个实现 DogState 接口的类,负责刻画小狗的各种状态。meetEnemyStat(狂叫)、meetEFriendState (晃动尾巴)、meetAnotherDog (嬉戏)、SoflyState (听主人命令)。(3)编写 Dog类,有 DogState 定义的属性 state,有 show 方法,在该方法中回调 showState
(4)编写测试类,测试小狗的各种状态。
// 1)编写 DogState 接口,接口中有 showState 方法。
interface DogState {
void showState();
}
// 2)编写若干个实现 DogState 接口的类,负责刻画小狗的各种状态。
class MeetEnemyState implements DogState {
public void showState() {
System.out.println("狂叫");
}
}
class MeetFriendState implements DogState {
public void showState() {
System.out.println("晃动尾巴");
}
}
class MeetAnotherDogState implements DogState {
public void showState() {
System.out.println("嬉戏");
}
}
class SoflyState implements DogState {
public void showState() {
System.out.println("听主人命令");
}
}
// 3)编写 Dog类,有 DogState 定义的属性 state,有 show 方法,在该方法中回调 showState方法
class Dog {
private DogState state;
public void setState(DogState state) {
this.state = state;
}
public void show() {
state.showState();
}
}
// 4)编写测试类,测试小狗的各种状态。
class Test {
public static void main(String[] args) {
Dog dog = new Dog();
dog.setState(new MeetEnemyState());
dog.show();
dog.setState(new MeetFriendState());
dog.show();
dog.setState(new MeetAnotherDogState());
dog.show();
dog.setState(new SoflyState());
dog.show();
}