Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 alist = new ArrayList<>(); Scanner scnr = new Scanner(System.in); int stage = scnr.nextInt(); alist.add(new Account(1000, "Brown, John", 100)); alist.add(new Account(1001, "Mae, Sally", 2000)); alist.add(new Account(1002, "Bunker, Edith", 1000)); alist.add(new Account(1010, "Smith, Richard", 10000)); alist.add(new Account(1008, "Dent, Bill", 3000)); alist.add(new Account(1007, "Fields, Ben", 7000)); alist.add(new Account(1009, "Townsend, Kathy", 5000)); alist.add(new Account(1006, "Jackson, Linda", 15000)); alist.add(new Account(1005, "Linton, Sue", 3000)); alist.add(new Account(1004, "Smith, Tom", 4000)); alist.add(new Account(1003, "Pilton, Ernie", 1000)); if (stage >= 0) { for(Account a : alist) System.out.println(a); System.out.println(); } if(stage >= 1) { /* Do this AFTER you have implemented the Comparable * interface in the Account class. * Replace this comment with code that uses Collections.sort(List) * to sort the ArrayList of Accounts in alist and a for each * loop that displays alist */ System.out.println(); // LEAVE this statement; adds blank line between displays } if(stage >= 2) { /* Replace this comment with code that sorts alist by account ID. * Create a Comparator object using a lambda expression * and use Collections.sort(List, Comparator) to * sort alist. Then display alist using a for each loop. */ System.out.println(); // LEAVE THIS STATEMENT; adds blank line between displays } if(stage >= 3) { /* Replace this comment with code that sorts alist by account balance. * Create a Comparator object using a lambda expression * and use Collections.sort(List, Comparator) to * sort alist. Then display alist using a for each loop. */ } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Management System MCQs Multiple Choice Questions And Answers

Authors: Arshad Iqbal

1st Edition

1073328554, 978-1073328550

More Books

Students also viewed these Databases questions

Question

7. Identify six intercultural communication dialectics.

Answered: 1 week ago