107k views
3 votes
Adapt your code from Assignment 2 to use both a Java API ArrayList and a Java API LinkedList and your new I_a (fish) objects from Assignment 3. The new program should have a main user menu like in A2 with an added choice to print the list backwards. There should be a new makeFish static method and a printing method similar to that in A2 renamed to printForwards and one new method printBackwards.

Making Fish When making an I_a object in the makeFish method, instead of asking the user to enter all the information as you did for Pokemon in A2, make a sub-menu for them to pick from the 14 kinds of I_a implemented in A3
The only thing the user must choose is the I_a type and if they want to enter the length (or get a random length) You should ask, Do you want to enter a length for your I'a, Y/N HINT: use equalsIgnoreCase to simplify user input If they choose to enter the length then you will need to ask for the length and convert the input to a double for the constructor.
You will need to try/catch for non-number and FishSizeException. This will need to loop until you get a valid length. Finally use a switch to call the constructor for the chosen I_a. For example: case 1: I_a ia = new Pua_ama(); or case 7: I_a ia = new PalaMoi(14.25); or case 9:I_a ia = new Ohua(3.3));
Lists of Fish The program should have both a Java API ArrayList and LinkedList. You must correctly declare both generic types to hold I_a objects. They should be local to the main method (like the array in A2). Use the List interface to declare both list variables, then initialize as the two different kinds of lists: List List1 = new ArrayList<>(); List List2 = new LinkedList<>(); Add each I_a the user makes to both lists. To add the first six I_a you can use the List Interface add method: List.add(E element) This will add to the end (higher index) of both kinds of lists. The allowed number of stored I_a should be limited to 6 like in the array in A2.
Don't let either List keep growing even though they can! The lists should work the same as in A2, the 7th I_a should "wrap around" and overwrite the first I_a made (in index 0), and so on. To overwrite existing elements using the List.set(int index, E element) method. Printing Lists The printArray method from A2 should be renamed to printForwards, it should still be void, and should take one parameter of type List instead of an array. The new printBackwards method will be nearly identical, but will print a List in reverse index order: 5 4 3 2 1 0 Both methods must use ListIterators to print the List contents. The printForwards method should get the ArrayList sent to it from the menu choice to print the list and should print out just like the old array did, forwards. The printBackwards method should have the LinkedList sent to it from the menu choice to print the list backwards and should print out in REVERSE order.

1 Answer

6 votes

Final answer:

The question is about extending Java code to incorporate ArrayList and LinkedList, utilize I_a (fish) objects, and handle user inputs, with particular focus on list operations such as adding, setting, and printing elements in both natural and reverse order.

Step-by-step explanation:

The student's question pertains to adapting existing Java code to make use of Java API ArrayList and LinkedList in addition to incorporating an I_a (fish) object. The instructions detail how to modify a program to execute specified functionalities like making fish objects using sub-menu selections, handling user input, employing try/catch blocks for input validation, limiting the number of objects stored, and overriding elements in a collection. Moreover, methods for printing the collections both forwards and backwards are to be implemented, utilizing the ListIterator for traversal.The makeFish method will facilitate the creation of I_a objects while allowing user interaction limited to selecting the type of I_a and optionally entering its length.

In cases where the length is not provided by the user, the program should assign a random value. Exception handling needs to be present to deal with potential input errors and FishSizeExceptions.For managing collections of fish objects, the program should declare and instantiate both an ArrayList and a LinkedList of I_a objects using the List interface. The printForwards method will print the list in natural order, while the printBackwards method will do so in reverse.

User Sanganabasu
by
7.9k points