Question

In: Computer Science

Purpose: This lab will give you experience modifying an existing ADT. Lab Main Task 1: Modify...

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.

Solutions

Expert Solution

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.


Related Solutions

The Calendar Program The purpose of this lab is to give you a chance to use...
The Calendar Program The purpose of this lab is to give you a chance to use some of the data stream tools we have been discussing in a simple application. The assignment is to write a calendar application which allows the user to select a date, and either retrieve a previously stored calendar entry, or save a calendar entry. Your program should present a GUI interface which allows the user to specify the month, day, and year of the calendar...
Project 3 Details The purpose of this project is to give you experience with creating and...
Project 3 Details The purpose of this project is to give you experience with creating and using custom objects. In it, you will make a couple custom classes that work in tandem with each other, and call them in your main function. For this project we'll be making something kind of like the Chatbot java file we made in class. You will create a Chatbot class that will contain a number of variables and functions. As you can imagine, a...
CVP Modeling project The purpose of this project is to give you experience creating a profitability...
CVP Modeling project The purpose of this project is to give you experience creating a profitability analysis that can be used to determine the effects of changing business conditions on a client's financial position. Managers often want to know the production level where profits earned from a product cover the cost of resources used to create it. Break-even analysis is how we determine this level. The point at which total sales revenues covers the costs of committed resources is called...
The purpose of this exercise is to give you experience in writing condition-controlled loops. Create a...
The purpose of this exercise is to give you experience in writing condition-controlled loops. Create a code in Python that will print monthly loan amortization schedule given the loan amount (P), APR i.e. Annual Percentage Rate (r=APR in %/12), loan terms in number of months (n). Please use these formulas: Monthly Payment (A) = P * ((r * (1 + r)**n)/(((1+r)**n)-1))            Monthly Interest Pmt (mIP)= r * Total Remaining Balance Monthly Principal Pmt (mPP) = A –...
Purpose This project is meant to give you experience with sorting, binary searching, and Big-Oh complexity....
Purpose This project is meant to give you experience with sorting, binary searching, and Big-Oh complexity. Objective "Write in Java please" Your goal is to take a book, as a large text file, and build a digital “concordance”. A concordance is like an index for a book in that it contains entries for words in the book, but it also includes passages from the book containing that word. For example, a query for the word Dormouse in Alice in Wonderland...
The task: You are working in a partnership position in a company and An existing Provider...
The task: You are working in a partnership position in a company and An existing Provider is looking for 10X growth with your company in 2020. you have scheduled a call with the provider's manager. Your CEO is also invited to this call. 1- Please write down a structure for the call and draft 3-5 potential solutions that you supposed to discuss with the partner. 2- prefer/prioritize these solutions in a way you expect to achieve the best result from...
The task: You are working in a partnership position in a company and An existing Provider...
The task: You are working in a partnership position in a company and An existing Provider is looking for 10X growth with your company in 2020. you have scheduled a call with the provider's manager. Your CEO is also invited to this call. 1- Please write down a structure for the call and draft 3-5 potential solutions that you supposed to discuss with the partner. 2- prefer/prioritize these solutions in a way you expect to achieve the best result from...
Purpose This assignment should give you experience in using file descriptors, open(), close(), write(), stat() and...
Purpose This assignment should give you experience in using file descriptors, open(), close(), write(), stat() and chmod(), perror(), and command line arguments. Program Write a C++ program that will allow you to add messages to a file that has NO permissions for any user. A Unix system has many files that have sensitive information in them. Permissions help keep these files secure. Some files can be publicly read, but can not be altered by a regular user (ex.: /etc/passwd). Other...
Data Manipulation In this lab, you will be manipulating the database to add, delete and modify...
Data Manipulation In this lab, you will be manipulating the database to add, delete and modify the values in the database. Please use a "select * from..." after each query to show the effects of your data manipulation query. 1. The title 'Time Flies' now has a new track, the 11th track 'Spring', which is 150 seconds long and has only a MP3 file. Insert the new track into Tracks table (Don’t hand-code any data for insert that can be...
Must be written in C++ in Visual studios community In this lab, you will modify the...
Must be written in C++ in Visual studios community In this lab, you will modify the Student class you created in a previous lab. You will modify one new data member which will be a static integer data member. Call that data member count. You also add a static method to the Student class that will display the value of count with a message indicating what the value represents, meaning I do not want to just see a value printed...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT