Question
The Comparable interface provides a compareToT) method that allows an class to establish what Java refers to as a natural ordering of object instances of
The Comparable interface provides a compareToT) method that allows an class to establish what Java refers to as a natural ordering of object instances of the class. This collections of objects to be sorted through a number of predefined methods. The Comparator interface serves a similar purpose, but allows the creation of comparator obiects that can be used to sort collections of obiects that have no natural ordering or to sort them in a manner different from the natural order. In this assignment, you will modfly the Account class to implement the Comparable interface and which orders the accounts based on the account owner's name. This will be done in the Account.java file. You'll also create two comparator objects OrderAcct1 and OrderAcct2. OrderAcct1 will order Account objects by their account numbers, while OrderAcct2 will order Account objects by their balance. There are four tests that incrementally check the additions that you make. They are controlled by an integer input with values from O through 3. 1. Test 1: Runs with value O and checks the intial Account Test program. 2. Test 2: Runs with value 1 and checks the implementation of Comparable by the Account. It checks that Collections. sort correctly sorts by account holders name. 3. Test 3: Runs with value 2 and checks the lambda expression for Comparator and its use by the Collections.sort(List, Comparator). Should sort by account ID. 4. Test 4: Runs with value 3 and checks the lambda expression for Comparator and its use by the Collections.sort(List, Comparator). Should sort by account balance.
AccountTest.java
interface public class Account { private int accountNumber; private String accountOwner; protected double balance;
public Account(int an, String ao, double b) { accountNumber = an; accountOwner = ao; balance = b; }
public int getAccountNumber() { return accountNumber; } public String getAccountOwner() { return accountOwner; } public double getBalance() { return balance; }
public void deposit(double val) { balance += val; }
public void withdraw(double val) { if(balance >= val) balance -= val; } public void update() {}
public String toString() { return accountNumber + ", " + accountOwner + ", " + balance; }
/* replace this code with the compareTo() method }
AccountTest.java
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Scanner;
public class AccountTest { public static void main(String [] args) { ArrayList
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started