In: Computer Science
Purpose: This lab will give you experience modifying an existing ADT.
Lab
Main Task 1: Modify the ListInterface Java interface source code
given below.
Change the name of ListInterface to ComparableListInterface, and
have ComparableListInterface inherit from the built-in Java
interface Comparable.
Note that Comparable has a method compareTo(). compareTo()
must be used in programming logic you will write
in a method called isInAscendingOrder() (more about
isInAscendingOrder() is mentioned further down in the lab
description).
You can find a brief summary on compareTo() at these web pages:
http://chortle.ccsu.edu/java5/notes/chap53b/ch53B_2.html (Links to an external site.)
https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html#compareTo(T) (Links to an external site.)
and in other sources you can find by performing a search for the Java Comparable interface using a web search engine.
Main Task 2: Modify the AList class source code given below by
performing the following:
1) change the name of the class from AList to ComparableAList
2) change ListInterface to ComparableListInterface
3) add a method corresponding to the following UML:
+isInAscendingOrder() : boolean
isInAscendingOrder() returns true if the list is in ascending
sorted order, else returns false isInAscendingOrder() should use
the compareTo() method that was inherited from
ComparableListInterface in order to perform the logic it needs to
return true or false.
Note: You MUST use the List ADT method's to
perform the logic for isInAscendingOrder(). That is, you must use
the methods declared in the interface for the List ADT and defined
in the implementation class for the List ADT in the programming
logic for isInAscendingOrder(). You will not receive
credit for the lab if you try to directly access the list array
member and determine from the list array member whether the List
ADT is in ascending order in the programming logic for
isInAscendingOrder().
Test your solution by writing a driver program. The driver program
will have a main() method and is the program that is run at the
command line. You may give your driver program any name you like.
The driver program should perform a loop, ask for input, and
display output in the following way (note that the output in bold
is what the user inputs):
Input a list of integers: 5 9 101 183
4893
Your list of integers is in ascending order.
Do you want to continue (y/n): y
Input a list of integers: 5 9 101 183 48
Your list of integers is not in ascending order.
Do you want to continue (y/n): y
Input a list of integers: 5 4 100 101 183
4893
Your list of integers is not in ascending order.
Do you want to continue (y/n): y
Input a list of integers: 5 9 101 101 183
4893
Your list of integers is in ascending order.
Do you want to continue (y/n): y
Input a list of integers: -48 -7 0 5 9 101
183
Your list of integers is in ascending order.
Do you want to continue (y/n): y
Input a list of integers: 14
Your list of integers is in ascending order.
Do you want to continue (y/n): y
Input a list of integers:
Your list of integers is in ascending order.
Do you want to continue (y/n): n
What to submit for the lab:
• your program source code for the List ADT you modified to have
inheritance from the Comparable interface, the isInAscendingOrder()
method, and the driver program
• a capture the results of the program runs as shown above. You may
submit captures of the program run as either a text file that has
all of the output of the driver program, or as an image file that
has all of the output of the driver program.
import java.util.*;
public class ToRunLab2 {
public static void main(String[] args) throws ListIndexOutOfBoundsException, ListException {
ListComparableListArrayBased<Integer> aList = new ListComparableListArrayBased<Integer>();
String[] numList;
char ans = 'y';
Scanner input = new Scanner(System.in);
while ( ans == 'y'){//while ans is y, go into below loop
do{//ask for user input
System.out.print("\n"+"Input a list of integers: ");
String numbers = input.nextLine();
numList = numbers.split(" "); //separates input with spaces
for(int i=0; i<numList.length;i++)//adds the separated input to the array
aList.add(i,numList[i]);
} while(!input.nextLine().isEmpty());//while input is empty, execute do loop
if(aList.isInAscendingOrder())//if the input is in ascending order, print out below
System.out.println("Your list of integers is in ascending order.");
else // else print out below
System.out.println("Your list of integers is not ascending order.");
System.out.print("Do you want to continue (y/n): " );//ask user to continue or not
ans = input.nextLine().charAt(0);
aList.removeAll();//remove all the input before going into the next input
}
}
}
//**Classes******************************************************************************************************************
public class ListComparableListArrayBased<T extends Comparable>
implements ComparableListInterface {
private static final int MAX_LIST = 50;
private Comparable items[];
private int numItems;
//constructor
public ListComparableListArrayBased(){
items = new Comparable[MAX_LIST];
numItems = 0;
}
public boolean isEmpty(){//check if value is empty or not
return (numItems == 0);
}
public int size(){
return numItems;
}
public void removeAll(){//remove all the values
items = new Comparable[MAX_LIST];
numItems = 0;
}
public void add(int index, Comparable item)//add all the values imputed
throws ListIndexOutOfBoundsException,
ListException{
if (numItems >= MAX_LIST){ // >= instead of > insert -1 after numItems
throw new ListException("ListException on add");
}//end if
if(index >=0 && index <=numItems){
for (int pos = numItems-1; pos >= index; pos--){
items[pos+1] = items[pos];
}//end for
items[index] = item;
numItems++;
}// end for
else{
throw new ListIndexOutOfBoundsException(
"ListIndexOutOfBoundsException on add");
}//end else
}
public T get(int index) throws ListIndexOutOfBoundsException{
if(index >= 0 && index < numItems){
return (T)items[index];
}//end if
else{
throw new ListIndexOutOfBoundsException(
"ListIndexOutOfBoundsException on get");
}//end else
}//end get method
public void remove(int index) throws ListIndexOutOfBoundsException{
if(index >= 0&& index <numItems){
//correction on textbook code: < numItems instead of <= size done
for (int pos = index+1; pos <numItems; pos++){
items[pos-1] = items[pos];
}//end for
numItems--;
}//end if
else {//index out of range
throw new ListIndexOutOfBoundsException(
"ListIndexOutOfBoundsException on remove");
}//end else
}//end remove
//compare if array is in ascending order by compare to
public boolean isInAscendingOrder(){
for(int i = 0; i < size()-1; i++)//check until i = size-1
{
if(items[i].toString().length() ==0 || items[i+1].toString().length()==0)
continue;
Integer i1 = new Integer(items[i].toString());
Integer i2 = new Integer(items[i+1].toString());
if(i1.compareTo(i2) > 0)
return false;
}
return true;
}
public int compareTo(Object obj){
return this.compareTo(obj);
}
@Override
public void compare() {
// TODO Auto-generated method stub
}
@Override
public void add(int index, Object item) throws ListIndexOutOfBoundsException, ListException {
// TODO Auto-generated method stub
}
//end class
}
******************************************************************************************************
public interface ComparableListInterface {
public boolean isEmpty();
public int size();
public void add(int index, Object item) throws ListIndexOutOfBoundsException, ListException;
public Object get(int index) throws ListIndexOutOfBoundsException;
public void remove(int index) throws ListIndexOutOfBoundsException;
public void removeAll();
public void compare();
} // end ListInterface
class ListIndexOutOfBoundsException extends Exception {
public ListIndexOutOfBoundsException(String string) {
System.out.println(string);
}
}
******************************************************************************************************************
public class ListException extends Exception {
public ListException(String string) {
System.out.println(string);
}
}
******************************************************************************************************************
SEE OUTPUT
Thanks, PLEASE COMMENT if there is any concern.