有一类BankAccount(模拟银行账户),包括存款deposit,取款withdraw等操作,根据balance(存款)进行比较,有一类SavingAccount,继承了BankAccount,用于设置利率和增加利息,建立以数组,包含一系列SavingAccount的对象,挑选出该数组中最小、最大的两个,赋给一个Pair。
部分代码如下,还是很简单的,请哪位给出PairUtil.minmax函数的代码。
class BankAccount implements Comparable<BankAccount>
{
private double balance;
public BankAccount(double initialBalance)
{
balance = initialBalance;
}

public void deposit(double amount)
{
double newBalance=balance+amount;
balance = newBalance;
}

public void withdraw(double amount)
{
double newBalance = balance-amount;
balance = newBalance;
}

public double getBalance()
{
return balance;
}

public int compareTo(BankAccount otherAccount)
{
if(balance<otherAccount.getBalance())
{
return -1;
}
else if(balance==otherAccount.getBalance())
{
return 0;
}
else
{
return 1;
}
}
}class SavingAccount extends BankAccount
{
private double interestRate;
public SavingAccount(double rate)
{
interestRate = rate;
}

public void addInterest()
{
double interest = getBalance()*interestRate/100;
deposit(interest);
}
}用于检测该操作的主类和主函数如下:
public class MinMaxTester
{
public static void main(String[] args)
{
SavingAccount[] accounts =
{
new SavingAccount(10),
new SavingAccount(10),
new SavingAccount(5)
};
accounts[0].deposit(1000);
accounts[1].deposit(10000);
accounts[2].deposit(10000);

for(SavingAccount a:accounts)
{
a.addInterest();
}

Pair<SavingAccount,SavingAccount>mm
     = PairUtil.minmax(accounts);
     
System.out.println(mm.getFirst().getBalance());
System.out.println("Expected: 1100");
System.out.println(mm.getSecond().getBalance());
System.out.println("Expected:11000");
}
}

解决方案 »

  1.   


    class Pair<T1, T2> {
    private T1 first;
    private T2 second;
    public T1 getFirst() {
    return first;
    }
    public T2 getSecond() {
    return second;
    }
    public Pair(T1 first, T2 second) {
    this.first = first;
    this.second = second;
    }
    }
    class PairUtil {
    public static <T1, T2> Pair<T2, T1> swap(Pair<T1, T2> pair) {
    T1 first = pair.getFirst();
    T2 second = pair.getSecond();
    Pair<T2, T1> ret = new Pair<T2, T1>(second, first);
    return ret;
    }
    public static Pair<SavingAccount,SavingAccount> minmax(SavingAccount[] accounts){
    SavingAccount max = accounts[0];
    SavingAccount min = accounts[0];
    for(int i=0;i<accounts.length;i++){
    SavingAccount account = accounts[i];
    if(max.getBalance()<account.getBalance()){
    max = account;
    }
    if(min.getBalance() > account.getBalance()){
    min = account;
    }
    }
    return new Pair<SavingAccount,SavingAccount>(min,max);
    }
    }
    class BankAccount implements Comparable<BankAccount> {
    private double balance;
    public BankAccount(double initialBalance) {
    balance = initialBalance;
    }
    public void deposit(double amount) {
    double newBalance = balance + amount;
    balance = newBalance;
    }
    public void withdraw(double amount) {
    double newBalance = balance - amount;
    balance = newBalance;
    }
    public double getBalance() {
    return balance;
    }
    public int compareTo(BankAccount otherAccount) {
    if (balance < otherAccount.getBalance()) {
    return -1;
    } else if (balance == otherAccount.getBalance()) {
    return 0;
    } else {
    return 1;
    }
    }
    }
    class SavingAccount extends BankAccount {
    private double interestRate;
    public SavingAccount(double initialBalance,double rate) {
    super(initialBalance);
    interestRate = rate;
    }
    public void addInterest() {
    double interest = getBalance() * interestRate / 100;
    deposit(interest);
    }
    }
    public class MinMaxTester {
    public static void main(String[] args) {
    SavingAccount[] accounts = { 
    new SavingAccount(1000,10),
    new SavingAccount(10000,10), 
    new SavingAccount(10000,5) };

    for (SavingAccount a : accounts) {
    a.addInterest();
    }
    Pair<SavingAccount, SavingAccount> mm = PairUtil.minmax(accounts);
    System.out.println(mm.getFirst().getBalance());
    System.out.println("Expected: 1100");
    System.out.println(mm.getSecond().getBalance());
    System.out.println("Expected:11000");
    }}
      

  2.   

    LS的程序写得挺好,可我想是否可以把minmax函数写成诸如:
    public static <E extends Comparable<? super E>> <T,S> Pair<T,S> minmax(E[] aAccounts)
    这样的形式啊,调用时E应该是SavingAccount,但是这样写编译时报错啊。
      

  3.   


    public static <E extends Comparable<? super E>> Pair<E,E> minmax(E[] accounts){
    E max = accounts[0];
    E min = accounts[0];
    for(int i=0;i<accounts.length;i++){
    E account = accounts[i];
    if(max.compareTo(account) < 0 ){
    max = account;
    }
    if(min.compareTo(account)>0){
    min = account;
    }
    }
    return new Pair<E,E>(min,max);
    }
      

  4.   

    如果用raw types 呢,这道题目又该怎么写