实验六
上机实践5 实验2 银行计算利息
实验要求:
建设银行是ConstructionBank是Bank的子类,准备隐藏继承的成员变量year,并重写计算利息的方法,即自己声明一个double型的year变量。
程序代码:
Bank.java
public class Bank{
int savedMoney;
int year;
double interest;
double interestRate=0.29;
茉莉花的寓意
public double computerInterest(){
interest=year*interestRate*savedMoney;
return interest;
}
public void tInterestRate(double rate){
interestRate=rate;
}
}
Construction.java
public class ConstructionBank extends Bank{
double year;
public double computerInterest(){
double r=year-(int)year;
int day=(int)(r*1000);
double puterInterest();
double dayInterest=day*0.0001*savedMoney;
interest=yearInterest+dayInterest;
System.out.printf("%d元存在建设银行%d年零%d天的利息:%f元\n",ar,day,interest);腐乳的制作
return interest;
}
}
BankOfDalian.java
public class BankOfDalian extends Bank{
double year;
public double computerInterest(){
double r=year-(int)year;
int day=(int)(r*1000);
double puterInterest();
double dayInterest=day*0.00012*savedMoney;
interest=yearInterest+dayInterest;
System.out.printf("%d元存在大连银行%d年零%d天的利息:%f元\n",ar,day,interest);
return interest;
}
}
SaveMoney.java
public class SaveMoney{
public static void main(String args[]){
int amount=8000;
ConstructionBank bank1=new ConstructionBank();
bank1.savedMoney=amount;
bank1.tInterestRate(0.035);
拖地日记
double puterInterest();
BankOfDalian bank2=new BankOfDalian();
bank2.savedMoney=amount;
bank2.tInterestRate(0.035);
double puterInterest();
System.out.printf("两个银行利息相差%f元\n",interest2-interest1);
}
}
实验结果:
实验后的练习:
参照建设银行或大连银行,再编写一个商业银行,让程序输出8000元存在商业银行8年零236天的利息。
BankOfChina.java
public class BankOfChina extends Bank{
鲷鱼的做法double year;
public double computerInterest(){
double r=year-(int)year;
int day=(int)(r*1000);
double puterInterest();
double dayInterest=day*0.00018*savedMoney;
interest=yearInterest+dayInterest;
System.out.printf("%d元存在中国银行%d年零%d天的利息:%f元\n",ar,day,interest);
幼儿围棋return interest;
}
}
上机实践5 实验3 公司支出的总薪水
实验要求:
在讲述继承与多态时,通过子类对象的上转型体现了继承的多态性,即把子类创造的对象的引用放到一个父类的对象中时,得到该对象的一个上转型对象,那么这个上转型对象在调用方法是就可能具有多种形态,不同对象的上转型对象调用同一方法可能产生不同的行为。
程序代码:
CompanySalary.java
abstract class Employee{
public abstract double earnings();
}
class Y earWorker extends Employee{
public double earnings(){
return 12000;
}
}
class MonthWorker extends Employee{
public double earnings(){
return 12*2300;
}
}
class WeekWorker extends Employee{
public double earnings(){
return 52*780;
}
}
class Company{
Employee[] employee;
double salaries=0;
Company(Employee[] employee){
}
public double salariesPay(){
salaries=0;包子馅配方
for(int i=0;i<employee.length;i++){
salaries=salaries+employee[i].earnings();
}
return salaries;若明若暗
}
}
public class CompanySalary{
public static void main(String args[]){
Employee[] employee=new Employee[29];
for(int i=0;i<employee.length;i++){产品经理面试问题
if(i%3==0)
employee[i]=new WeekWorker();
el if(i%3==1)
employee[i]=new MonthWorker();
el if(i%3==2)
employee[i]=new Y earWorker();
}
Company company=new Company(employee);
System.out.println("公司薪水总额:"+company.salariesPay()+"元");
}
}
实验结果: