用Java编写在1.为Person类增加以下方法: (1)int getAge(int year) //返回在year年的年龄 (2)int getAge() //返回今年的年龄,重载 (3)int olderThen(Person per) //返回this与per //的年龄差值,按年龄比较对象大小 (4)boolean equals(Person per) //比较this与 // per引用实例对应成员变量值是否相等 2. 实现银行账户类构造方法成成员方法。 public class Account //账户类 { private String name; //储户姓名 private double balance; //账户余额 public Account(String name) //构造方法 public String getName() //返回账户名 public double balance() //查看账户余额 public boolean put(double value)//存款,存入金额 public double get(double value) //取款,参数为取款金额,返回实际取到金额 }的基础上,声明实现Comparble<T>接口,提供按照账户余额比较账户大小的方法;同时,使用Comparator<T>接口声明比较器类CompareName,按照账户名比较账户大小;编写main方法测试相应的接口实现方法。
时间: 2024-02-14 08:12:30 浏览: 75
下面是Java代码实现:
1. Person类:
```java
public class Person implements Comparable<Person> {
private String name;
private int birthYear;
public Person(String name, int birthYear) {
this.name = name;
this.birthYear = birthYear;
}
public int getAge(int year) {
return year - birthYear;
}
public int getAge() {
return getAge(Calendar.getInstance().get(Calendar.YEAR));
}
public int olderThan(Person per) {
return this.birthYear - per.birthYear;
}
public boolean equals(Person per) {
if (this == per) {
return true;
}
if (per == null) {
return false;
}
return this.name.equals(per.name) && this.birthYear == per.birthYear;
}
@Override
public int compareTo(Person per) {
return Integer.compare(this.birthYear, per.birthYear);
}
}
```
2. Account类:
```java
import java.util.Comparator;
public class Account implements Comparable<Account> {
private String name;
private double balance;
public Account(String name) {
this.name = name;
this.balance = 0;
}
public String getName() {
return name;
}
public double getBalance() {
return balance;
}
public boolean put(double value) {
if (value <= 0) {
return false;
}
balance += value;
return true;
}
public double get(double value) {
if (value <= 0 || balance < value) {
return 0;
}
balance -= value;
return value;
}
@Override
public int compareTo(Account acc) {
return Double.compare(this.balance, acc.balance);
}
public static class CompareName implements Comparator<Account> {
@Override
public int compare(Account acc1, Account acc2) {
return acc1.name.compareTo(acc2.name);
}
}
}
```
3. 测试代码:
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args) {
// 测试Person类
Person p1 = new Person("Tom", 1990);
Person p2 = new Person("Jerry", 1995);
System.out.println("Tom's age in 2021: " + p1.getAge(2021));
System.out.println("Jerry's age in 2021: " + p2.getAge(2021));
System.out.println("Tom's age: " + p1.getAge());
System.out.println("Jerry's age: " + p2.getAge());
System.out.println("Tom older than Jerry: " + p1.olderThan(p2));
System.out.println("Tom equals Jerry: " + p1.equals(p2));
// 测试Account类
List<Account> accounts = new ArrayList<>();
Account acc1 = new Account("Tom");
acc1.put(1000);
accounts.add(acc1);
Account acc2 = new Account("Jerry");
acc2.put(2000);
accounts.add(acc2);
Account acc3 = new Account("Alice");
acc3.put(1500);
accounts.add(acc3);
// 按照余额排序
System.out.println("Sort by balance:");
Collections.sort(accounts);
for (Account acc : accounts) {
System.out.println(acc.getName() + " " + acc.getBalance());
}
// 按照账户名排序
System.out.println("Sort by name:");
Collections.sort(accounts, new Account.CompareName());
for (Account acc : accounts) {
System.out.println(acc.getName() + " " + acc.getBalance());
}
}
}
```
阅读全文