Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Lab 2b: Enforcing the ADT Model's Concept of Interface Main Task 1: Modify the ListInterface Java interface source code given below. Change the name of

image text in transcribedLab 2b: Enforcing the ADT Model's Concept of Interface

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).

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):

Output:

Input a list of integers: 5 9 101 183 4893 Your list of integers is in ascending order. Do you want to continue (y): 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): 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): 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): 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): y Input a list of integers: 14 Your list of integers is in ascending order. Do you want to continue (y): y Input a list of integers: Your list of integers is in ascending order. Do you want to continue (y): 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.

PROGRAM CODE FOR ListInterface.java: I couldn't provide the code because the system says, it's too long. So I attached images of it bellow. Please let me know if you need anything else.

image text in transcribed image text in transcribed

PROGRAM CODE for AList.java:

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

/An interface for the ADT list. Entries in a list have positions that begin with 1. @author Frank M. Carrano @author Timothy M. Henry @version 4.0 public interface ListInterface /**Adds a new entry to the end of this 1ist. Entries currently in the list are unaffected The list's size is increased by 1. @param newEntry The object to be added as a new entry. */ public void add (T newEntry)i *Adds a new entry at a specified position within this list. Entries originally at and above the specified position are at the next higher position within the list. The list's size is increased by 1. @param newPosition An integer that specifies the desired position of the new entry The object to be added as a new entry. @param newEntry @throws IndexOutofBoundsException if either newPosition getLength)+1. */ public void add (int newPosition, T newEntry)i /**Removes the entry at a given position from this list Entries originally at positions higher than the given position are at the next lower position within the list, and the list's size is decreased by param givenPosition An integer that indicates the position of the entry to be removed return A reference to the removed entry @throws IndexOutofBoundsException if either givenPosition getLength).*/ public T remove (int givenPosition); /**Removes all entries from this list.*/ public void clear *Replaces the entry at a given position in this list. Cparam givenPosition An integer that indicates the position of @param newEntry The object that will replace the entry at the areturn The original entry that was replaced the entry to be replaced position givenPosition. throws IndexOutofBoundsException if either givenPosition getLength). / public T replace (int givenPosition, T newEntry); /**Retrieves the entry at a given position in this list param givenPosition An integer that indicates the position of the desired entry. return A reference to the indicated entry. /An interface for the ADT list. Entries in a list have positions that begin with 1. @author Frank M. Carrano @author Timothy M. Henry @version 4.0 public interface ListInterface /**Adds a new entry to the end of this 1ist. Entries currently in the list are unaffected The list's size is increased by 1. @param newEntry The object to be added as a new entry. */ public void add (T newEntry)i *Adds a new entry at a specified position within this list. Entries originally at and above the specified position are at the next higher position within the list. The list's size is increased by 1. @param newPosition An integer that specifies the desired position of the new entry The object to be added as a new entry. @param newEntry @throws IndexOutofBoundsException if either newPosition getLength)+1. */ public void add (int newPosition, T newEntry)i /**Removes the entry at a given position from this list Entries originally at positions higher than the given position are at the next lower position within the list, and the list's size is decreased by param givenPosition An integer that indicates the position of the entry to be removed return A reference to the removed entry @throws IndexOutofBoundsException if either givenPosition getLength).*/ public T remove (int givenPosition); /**Removes all entries from this list.*/ public void clear *Replaces the entry at a given position in this list. Cparam givenPosition An integer that indicates the position of @param newEntry The object that will replace the entry at the areturn The original entry that was replaced the entry to be replaced position givenPosition. throws IndexOutofBoundsException if either givenPosition getLength). / public T replace (int givenPosition, T newEntry); /**Retrieves the entry at a given position in this list param givenPosition An integer that indicates the position of the desired entry. return A reference to the indicated entry. ethrows IndexOutofBoundsException if either givenPosition getLength).* public T getEntry (int givenPosition) /**Retrieves all entries that are in this list in the order in which they occur in the list. @return A newly allocated array of all the entries in the list. If the list is empty, the returned array is empty. */ public T[] toArray /** Sees whether this list contains a given entry @param anEntry The object that is the desired entry. @return True if the list contains anEntry, or false if not./ public boolean contains (T anEntry); /**Gets the length of this list. public int getLength ) /**Sees whether this list is empty public boolean isEmpty); @return The integer number of entries currently in the list.* @return True if the list is empty, or false if not./ ) // end ListInterface import java.util.Arrays; A class that implements the ADT list by using a resizable array Entries in a list have positions that begin with 1 Duplicate entries are allowed Gauthor Frank M. Carrano Cauthor Timothy M. Henry @version 4.0 public class AList implements ListInterface private T list Array of list entries ignore list[0] private int numberOfEntries; private boolean initialized false; private static final int DEFAULT_CAPACITY25; private static final int MAX_CAPACITY10000; public AList ) this (DEFAULT_CAPACITY) ) / end default constructor public AList(int initialCapacity) // Is initialCapacity too small? if (initialCapacity = 1) && (newPosition -1) if ((give nPosition && (givenPosition 1) && (givenPosition (give nPosition capacity) int newCapacity2 * capacity; checkCapacity (newCapacity) / Is capacity too big? listArrays.copyOf (list, newCapacity+1); // end if // end ensureCapacity // Makes room for a new entry at newPosition // Precondition : 1 1) && (newPosition = newlndex; index--) list[index 1list[index]; end makeRoom // Shifts entries that are beyond the entry to be removed to the // next lower position. // Pre condition : 1 -1) &&(givenPosition MAX_CAPACITY) throw new IllegalStateException ("Attempt to create a list " "whose capacity exceeds"+ "allowed maximum.") 1 II end checkCapacity ) // end AList /An interface for the ADT list. Entries in a list have positions that begin with 1. @author Frank M. Carrano @author Timothy M. Henry @version 4.0 public interface ListInterface /**Adds a new entry to the end of this 1ist. Entries currently in the list are unaffected The list's size is increased by 1. @param newEntry The object to be added as a new entry. */ public void add (T newEntry)i *Adds a new entry at a specified position within this list. Entries originally at and above the specified position are at the next higher position within the list. The list's size is increased by 1. @param newPosition An integer that specifies the desired position of the new entry The object to be added as a new entry. @param newEntry @throws IndexOutofBoundsException if either newPosition getLength)+1. */ public void add (int newPosition, T newEntry)i /**Removes the entry at a given position from this list Entries originally at positions higher than the given position are at the next lower position within the list, and the list's size is decreased by param givenPosition An integer that indicates the position of the entry to be removed return A reference to the removed entry @throws IndexOutofBoundsException if either givenPosition getLength).*/ public T remove (int givenPosition); /**Removes all entries from this list.*/ public void clear *Replaces the entry at a given position in this list. Cparam givenPosition An integer that indicates the position of @param newEntry The object that will replace the entry at the areturn The original entry that was replaced the entry to be replaced position givenPosition. throws IndexOutofBoundsException if either givenPosition getLength). / public T replace (int givenPosition, T newEntry); /**Retrieves the entry at a given position in this list param givenPosition An integer that indicates the position of the desired entry. return A reference to the indicated entry. /An interface for the ADT list. Entries in a list have positions that begin with 1. @author Frank M. Carrano @author Timothy M. Henry @version 4.0 public interface ListInterface /**Adds a new entry to the end of this 1ist. Entries currently in the list are unaffected The list's size is increased by 1. @param newEntry The object to be added as a new entry. */ public void add (T newEntry)i *Adds a new entry at a specified position within this list. Entries originally at and above the specified position are at the next higher position within the list. The list's size is increased by 1. @param newPosition An integer that specifies the desired position of the new entry The object to be added as a new entry. @param newEntry @throws IndexOutofBoundsException if either newPosition getLength)+1. */ public void add (int newPosition, T newEntry)i /**Removes the entry at a given position from this list Entries originally at positions higher than the given position are at the next lower position within the list, and the list's size is decreased by param givenPosition An integer that indicates the position of the entry to be removed return A reference to the removed entry @throws IndexOutofBoundsException if either givenPosition getLength).*/ public T remove (int givenPosition); /**Removes all entries from this list.*/ public void clear *Replaces the entry at a given position in this list. Cparam givenPosition An integer that indicates the position of @param newEntry The object that will replace the entry at the areturn The original entry that was replaced the entry to be replaced position givenPosition. throws IndexOutofBoundsException if either givenPosition getLength). / public T replace (int givenPosition, T newEntry); /**Retrieves the entry at a given position in this list param givenPosition An integer that indicates the position of the desired entry. return A reference to the indicated entry. ethrows IndexOutofBoundsException if either givenPosition getLength).* public T getEntry (int givenPosition) /**Retrieves all entries that are in this list in the order in which they occur in the list. @return A newly allocated array of all the entries in the list. If the list is empty, the returned array is empty. */ public T[] toArray /** Sees whether this list contains a given entry @param anEntry The object that is the desired entry. @return True if the list contains anEntry, or false if not./ public boolean contains (T anEntry); /**Gets the length of this list. public int getLength ) /**Sees whether this list is empty public boolean isEmpty); @return The integer number of entries currently in the list.* @return True if the list is empty, or false if not./ ) // end ListInterface import java.util.Arrays; A class that implements the ADT list by using a resizable array Entries in a list have positions that begin with 1 Duplicate entries are allowed Gauthor Frank M. Carrano Cauthor Timothy M. Henry @version 4.0 public class AList implements ListInterface private T list Array of list entries ignore list[0] private int numberOfEntries; private boolean initialized false; private static final int DEFAULT_CAPACITY25; private static final int MAX_CAPACITY10000; public AList ) this (DEFAULT_CAPACITY) ) / end default constructor public AList(int initialCapacity) // Is initialCapacity too small? if (initialCapacity = 1) && (newPosition -1) if ((give nPosition && (givenPosition 1) && (givenPosition (give nPosition capacity) int newCapacity2 * capacity; checkCapacity (newCapacity) / Is capacity too big? listArrays.copyOf (list, newCapacity+1); // end if // end ensureCapacity // Makes room for a new entry at newPosition // Precondition : 1 1) && (newPosition = newlndex; index--) list[index 1list[index]; end makeRoom // Shifts entries that are beyond the entry to be removed to the // next lower position. // Pre condition : 1 -1) &&(givenPosition MAX_CAPACITY) throw new IllegalStateException ("Attempt to create a list " "whose capacity exceeds"+ "allowed maximum.") 1 II end checkCapacity ) // end AList

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

Modern Database Management

Authors: Heikki Topi, Jeffrey A Hoffer, Ramesh Venkataraman

13th Edition

0134773659, 978-0134773650

More Books

Students also viewed these Databases questions