Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The List ADT is defined in the code provided below. The code gives the List ADT interface and the implementation in Java. The List ADT

The List ADT is defined in the code provided below. The code gives the List ADT interface and the implementation in Java.

The List ADT functions very much like an array. Since it is an ADT it has methods that are used to interact with it. Methods can be added to the List ADT in its interface and implementation, and these additions give it more capabilities than a standard array.

You are to modify the ADT interface and implementation by writing a recursive method named recursiveMin() that returns the smallest integer in a List of integers.

The approach you can take is as follows: If you divide the List ADT contents into two pieces halves, for exampleand find the smallest integer in each of the two pieces, the smallest integer in the entire List ADT will be the smaller of the these two integers. Since you will be searching a portion of the List ADTfor example, the elements [first] through [last] these will be convenient for your method to have three parameters: the List ADT and two indices, first and last. As an example of using this recursive method approach, look at the following method that recursively displays an array:

public static void displayArray(int array[], int first, int last) { if (first == last) System.out.print(array[first] + ""); else { int mid = (first + last)/ 2; displayArray(array, first, mid); displayArray(array, mid + 1, last); }// end if-else }// end displayArray

Notice how mid is used to divide the array contents into two pieces. Notice also how the base case plays a role in deciding what value should be displayed.

Once you have modified the List ADT to have the recursiveMin() method, write a Java driver program that makes a List ADT, takes integers as input from a user, and displays the minimum value of the integers using the recursiveMin() method. 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 The minimum value found by recursiveMin is: 5. Do you want to continue (y): y Input a list of integers: 0 5 9 -48 -7 101 183 The minimum value found by recursiveMin is: -48. Do you want to continue (y): y Input a list of integers: 14 The minimum value found by recursiveMin is: 14. Do you want to continue (y): y Input a list of integers: The minimum value found by recursiveMin is: nothing. 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 recursiveMin(), 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.

image text in transcribedimage text in transcribed

image text in transcribedimage text in transcribedimage text in transcribed

image text in transcribedimage text in transcribed

/** An interface for the ADT 1list. Entries in a list have positions that begin with 1. Gauthor Frank M. Carrano Cauthor Timothy M. Henry @version 4.0 public interface ListInterface /**Adds a new entry to the end of this list. Entries currently in the list are unaffected. The list's size is increased by 1 eparam newEntry The object to be added as a new entry. / public void add (T newEntry) /**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. eparam newPosition An integer that specifies the desired position of the new entry. The object to be added as a new entry eparam newEntry throws IndexOutofBoundsException if either newPosition getLength)+ 1. */ public void add (int newPosition, T newEntry); /**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 1. @param givenPosition An integer that indicates the position of the entry to be removed @return A reference to the removed entry ethrows IndexOutofBoundsException if either givenPosition getLength).* public T remove(int givenPosition); /**Removes all entries from this 1ist. public void clear /Replaces the entry at a given position in this list eparam givenPosition An integer that indicates the position of eparam newEntry The object that will replace the entry at the @return 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 TI toArrayO /** 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.*/ I end ListInterface import java.util.Arraysi 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 @author Frank M. Carrano author 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_CAPACITY-25; private static final int MAXCAPACITY = 10000; - public AList() this (DEFAULT CAPACITY) / end default constructor public AList (int initialCapacity) // Is initialCapacity too smal1? if (initialCapacity -1) && (newPosition -1) &&(givenPosition -1) &&(givenPosition = if ((give n Position 1) && (givenPosition = 1) 66 (new Position = newlndex; index-_) list[index 1] -1istlindex]; // end makeRoom // Shifts entries that are beyond the entry to be removed to the // next lower position. // Precondition : 1 1 &&(givenPosition MAX CAPACITY) throw new IllegalStateException ("Attempt to create a list"+ "whose capacity exceeds "+ "allowed maximum."); ) 1/ end checkCapacity II end AList /** An interface for the ADT 1list. Entries in a list have positions that begin with 1. Gauthor Frank M. Carrano Cauthor Timothy M. Henry @version 4.0 public interface ListInterface /**Adds a new entry to the end of this list. Entries currently in the list are unaffected. The list's size is increased by 1 eparam newEntry The object to be added as a new entry. / public void add (T newEntry) /**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. eparam newPosition An integer that specifies the desired position of the new entry. The object to be added as a new entry eparam newEntry throws IndexOutofBoundsException if either newPosition getLength)+ 1. */ public void add (int newPosition, T newEntry); /**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 1. @param givenPosition An integer that indicates the position of the entry to be removed @return A reference to the removed entry ethrows IndexOutofBoundsException if either givenPosition getLength).* public T remove(int givenPosition); /**Removes all entries from this 1ist. public void clear /Replaces the entry at a given position in this list eparam givenPosition An integer that indicates the position of eparam newEntry The object that will replace the entry at the @return 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 TI toArrayO /** 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.*/ I end ListInterface import java.util.Arraysi 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 @author Frank M. Carrano author 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_CAPACITY-25; private static final int MAXCAPACITY = 10000; - public AList() this (DEFAULT CAPACITY) / end default constructor public AList (int initialCapacity) // Is initialCapacity too smal1? if (initialCapacity -1) && (newPosition -1) &&(givenPosition -1) &&(givenPosition = if ((give n Position 1) && (givenPosition = 1) 66 (new Position = newlndex; index-_) list[index 1] -1istlindex]; // end makeRoom // Shifts entries that are beyond the entry to be removed to the // next lower position. // Precondition : 1 1 &&(givenPosition MAX CAPACITY) throw new IllegalStateException ("Attempt to create a list"+ "whose capacity exceeds "+ "allowed maximum."); ) 1/ end checkCapacity II 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_2

Step: 3

blur-text-image_3

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

Database Management Systems Designing And Building Business Applications

Authors: Gerald V. Post

1st Edition

0072898933, 978-0072898934

More Books

Students also viewed these Databases questions