Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

a. Define a Java interface named SortableInterface. A class that implements this

ID: 3752036 • Letter: A

Question

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 "bob jim michael" and "1000 2000 5000."

public class Sample { public static void main (String[] args) {
SortableInterface a<String> = new StringSort <String> ();

a.add("bob");

a.add("jim");

a.add("michael");

a.sort();

System.out.print(a.toString());


SortableInterface b<BankAccount> = 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());

}

}

b. Create a class called StringSort that implements the SortableInterface interface. This class should use an array to hold String objects.

c. Create a class called BankAccountSort that implements the SortableInterface interface. This class should use an array to hold BankAccount objects. We assume that the BankAccount class provides a constructor that accepts as an argument the initial balance.

Explanation / Answer

ScreenShot

Program

BankAccount class

//Bank account class
public class BankAccount {
   //Variable
   int balance;
   //Parameterized constructor
   public BankAccount(int i) {
       balance=i;
   }
   //sort
   public int compareTo(BankAccount ba) {
       int bal=((BankAccount)ba).getBalance();
        return balance-bal;
    }
   //Accessor
   int getBalance() {
       return balance;
   }

}


Interface class(Sample.java)

//Packages for I/O,list and array list
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
//Interface with metods
interface SortableInterface<T>{
   void add(T element);
   void sort();
   String toString();
  
}
//Class to implement interface string
class StringSort implements SortableInterface<String>{
   //Array list for store data
   private List<String> arr=new ArrayList<String>();
   //Methods of interface implementation
   @Override
   public void add(String s) {
       arr.add(s);
   }
   @Override
   public void sort() {
       Collections.sort(arr);
   }
   @Override
   public String toString()
   {
       int i=0;
       String str="";
       while(i<arr.size()) {
           str+=arr.get(i)+" ";
           i++;
       }
       str+=" ";
       return str;
   }
  
}
//Bank account object inplementation interface class
class BankAccountSort implements SortableInterface<BankAccount>{
   //Array list for object storage
   private List<Integer> arr=new ArrayList<Integer>();
   //Implementation
   @Override
   public void add(BankAccount b) {
       arr.add(b.getBalance());
   }
   @Override
   public void sort() {
       Collections.sort(arr);
   }
   @Override
   public String toString()
   {
       int i=0;
       String str="";
       while(i<arr.size()) {
           str+=Integer.toString(arr.get(i))+" ";
           i++;
       }
       str+=" ";
       return str;
   }
  
   }

public class Sample {

   public static void main(String[] args) {
       SortableInterface<String> a = new StringSort ();

       a.add("bob");

       a.add("jim");

       a.add("michael");

       a.sort();

       System.out.print(a.toString());
       SortableInterface<BankAccount> b= new BankAccountSort();

       b.add(new BankAccount(1000));

       b.add(new BankAccount(5000));

       b.add(new BankAccount(2000));

       b.sort();

       System.out.print(b.toString());

   }

}

-------------------------------------

Output

bob jim michael
1000 2000 5000

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote