For this problem you must define a simple generic interface SortableInterface, a
ID: 3752033 • Letter: F
Question
For this problem you must define a simple generic interface SortableInterface, and two implementations of the interface, StringSort and BankAccountSort.
a. Define a Java interface named SortableInterface. A class that implements this interface allows creation of an object that holds an array of objects of a specified type. The SortableInterface interface should require the add method to add an object to the array and the sort method to sort the objects in the array. The actual type of the objects in the array is specified when the SortableInterface object is instantiated. Therefore, both the SortableInterface interface and the classes that implement it should be generic. Suppose a class named StringSort implements the SortableInterface interface. A test driver application is shown here. Its output would be "chopper nami luffy" and "1000 2000 5000."
Explanation / Answer
This project consists of multiple files.
1. BankAccount.java
2. BankAccountSort.java
3. SortableInterface.java
4. StringSort.java
5. TestDriverApp.java
-----------------------
BankAccount.java
-----------------------
package assignment;
public class BankAccount implements Comparable<BankAccount> {
private Integer accountNumber;
public BankAccount(Integer accountNumber) {
this.accountNumber = accountNumber;
}
public int getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(Integer accountNumber) {
this.accountNumber = accountNumber;
}
public int compareTo(BankAccount o) {
return accountNumber.compareTo(o.accountNumber);
}
@Override
public String toString() {
return String.valueOf(accountNumber);
}
}
--------------------
BankAccountSort.java
--------------------
package assignment;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class BankAccountSort<T extends Comparable<T>> implements SortableInterface<T> {
List<T> items = new ArrayList<T>();
public void add(T item) {
items.add(item);
}
public void sort() {
Collections.sort(items);
}
@Override
public String toString() {
return items.toString();
}
}
-----------------------
SortableInterface.java
-----------------------
package assignment;
/*
* This is a sort-able interface, which takes any comparable items like String, Integer, BankAccount objects.
*/
public interface SortableInterface<T extends Comparable<T>> {
public void add(T item);
public void sort();
public String toString();
}
--------------------
StringSort.java
--------------------
package assignment;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class StringSort<T extends Comparable<T>> implements SortableInterface<T> {
List<T> items = new ArrayList<T>();
public void add(T item) {
items.add(item);
}
public void sort() {
Collections.sort(items);
}
@Override
public String toString() {
return items.toString();
}
}
------------------
TestDriverApp.java
------------------
package assignment;
public class TestDriverApp {
public static void main(String[] args) {
SortableInterface<String> a = new StringSort<String>();
a.add("nami");
a.add("chopper");
a.add("luffy");
a.sort();
System.out.print(a.toString());
SortableInterface<BankAccount> b = new BankAccountSort<BankAccount>();
b.add(new BankAccount(1000));
b.add(new BankAccount(5000));
b.add(new BankAccount(2000));
b.sort();
System.out.print(b.toString());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.