有一类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");
}
}
部分代码如下,还是很简单的,请哪位给出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");
}
}
解决方案 »
- Log4j疑问
- java 强制类型转换问题 请高手指点!
- Exception in thread "main" java.lang.NoClassDefFoundError是什么意思?
- 取得当天日期最简单方法&&字符串默认值设置
- 如何在JTable的单元格里加入控件
- textarea事件处理,麻烦帮个忙!
- 有人用过Junit对类进行测试,我的用的时候,怎么说类找不到
- 如何判断输入的是中文还是非中文?
- Java中如何运行系统命令?
- 我已经买了java2核心技术,卷1和卷2,可是怎么看不太懂呀。是不是应该上个补习班呢?
- 看视频,遇到这么一句话,关于equals方法
- RandomAccessFile类向文件写汉字字符串问题
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");
}}
public static <E extends Comparable<? super E>> <T,S> Pair<T,S> minmax(E[] aAccounts)
这样的形式啊,调用时E应该是SavingAccount,但是这样写编译时报错啊。
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);
}