SlideShare a Scribd company logo
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.
HERE IS I.a java code:
import java.util.Random;
public abstract class I_a {
protected final String name;
protected final String englishName;
protected final String scientificName;
protected final double maxLength;
protected final double minLength;
protected final String[] diet;
protected double length;
protected double weight;
protected String bodyColor;
protected String finColor;
protected String sex;
protected double growthRate = 0.8;
/**
* constructor.
**/
public I_a(String nName, String nEnglishName, String nScientificName,
double nMaxLength, double nMinLength, double nLength, double nWeight,
String[] nDiet, String nBodyColor, String nFinColor, String nSex)
throws FishSizeException {
//set instance variables
this.name = nName;
this.englishName = nEnglishName;
this.scientificName = nScientificName;
this.maxLength = nMaxLength;
this.minLength = nMinLength;
if (nLength >= this.minLength && nLength < this.maxLength) {
this.length = nLength;
} else {
throw new FishSizeException(name + " length must be within "
+ this.minLength + " and " + this.maxLength
+ " inches long. But was sent " + nLength);
}
this.weight = nWeight;
this.diet = nDiet;
this.bodyColor = nBodyColor;
this.finColor = nFinColor;
this.sex = nSex;
}
/*** abstract methods to be implemented in the sub-classes ****/
public abstract void setLength(double newLength);
public abstract void setWeight(double newWeight);
public abstract void eat(String food);
protected abstract void grow();
protected abstract I_a levelUp();
//########### Protected class method ################
/** initialize random fish sex
* choose randomly male or female
* used by some subclasses.
*/
protected void initSex() {
//randomize sex
Random ran = new Random();
int flip = ran.nextInt(2);
if (flip == 0) {
this.sex = "male";
} else {
this.sex = "female";
}
}
/*** public class methods - methods used by all sub-classes ***/
/**
* Returns I_a information as a formatted String.
* @return String representing I_a object data.
*/
public String toString() {
String s = "Name: " + this.name + "n";
s = s + "English name: " + this.englishName + "n";
s = s + "Scientific name: " + this.scientificName + "n";
s = s + "Length: " + this.length + "n";
s = s + "Weight: " + this.weight + "n";
s = s + "Body color: " + this.bodyColor + "n";
s = s + "Fin color: " + this.finColor + "n";
s = s + "Sex: " + this.sex;
return s;
}
/** Get Methods **/
public String getName() {
return this.name;
}
public String getEnglishName() {
return this.englishName;
}
public String getScientificName() {
return this.scientificName;
}
public double getMaxLength() {
return this.maxLength;
}
public double getMinLength() {
return this.minLength;
}
public double getLength() {
return this.length;
}
public double getWeight() {
return this.weight;
}
public String[] getDiet() {
return this.diet;
}
public String getBodyColor() {
return this.bodyColor;
}
public String getFinColor() {
return this.finColor;
}
public String getSex() {
return this.sex;
}
}
Code from A2: PokemonArray.java
import java.util.Scanner;
public class PokeArray {
/**set the size for array.*/
static final int SIZE = 6;
/**Sanner.*/
static final Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
Pokemon[] pokemonArray = new Pokemon[SIZE];
int curIndex = 0;
boolean choice = false;
int inNumber = -1;
//loop
while (!choice) {
System.out.println("1. Add a Pokemon");
System.out.println("2. Print all Pokemon");
System.out.println("0. Exit the Program");
System.out.println("What would you like to do?");
inNumber = sc.nextInt();
sc.nextLine();
switch (inNumber) {
case 1:
Pokemon p = makePokemon();
pokemonArray[curIndex % SIZE] = p;
curIndex++;
break;
case 2:
printArray(pokemonArray);
break;
case 0: choice = true;
System.out.print("Goodbye!!");
break;
default:
System.out.println("Invalid option, please make a selection");
break;
}
}
sc.close();
}
public static Pokemon makePokemon() {
System.out.print("What is the Pokemon's species: ");
String species = sc.nextLine();
System.out.println("Does the Pokemon have a name Y/N: ");
String hasName = sc.nextLine();
String name = "";
if (hasName.equalsIgnoreCase("Y")) {
System.out.println("What is the Pokemon's name? ");
name = sc.nextLine();
} else if (hasName.equalsIgnoreCase("N")) {
System.out.println("Name is empty");
}
System.out.println("What is the Pokemon's Pokedex number? ");
int number = -1;
while (!sc.hasNextInt()) {
System.out.println("Please enter the valid number: ");
sc.nextLine();
}
number = sc.nextInt();
sc.nextLine();
System.out.println("What is the Pokemon's Type? ");
String type1 = sc.nextLine();
System.out.println("What is the Pokemon's Second Type (if it has one)? ");
String type2 = sc.nextLine();
return new Pokemon(species, name, number, type1, type2);
}
public static void printArray(Pokemon[] pokemonArray) {
for (int i = 0; i < SIZE; i++) {
Pokemon p = pokemonArray[i];
if (p != null) {
System.out.println("Pokemon " + (i + 1) + ":");
System.out.print(p);
}
}
}
}

More Related Content

PPTX
Icom4015 lecture15-f16
PDF
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
PDF
Step 1 The Pair Class Many times in writing software we come across p.pdf
PDF
In this lab, we will write an application to store a deck of cards i.pdf
DOCX
Java execise
PDF
DOES NOT NEED TO BE ANSWERED UNTIL NOV 13thWords AssignmentRober.pdf
PPT
Chapter2pp
PPT
ARRAYS.ppt
Icom4015 lecture15-f16
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Step 1 The Pair Class Many times in writing software we come across p.pdf
In this lab, we will write an application to store a deck of cards i.pdf
Java execise
DOES NOT NEED TO BE ANSWERED UNTIL NOV 13thWords AssignmentRober.pdf
Chapter2pp
ARRAYS.ppt

Similar to Adapt your code from Assignment 2 to use both a Java API ArrayList a.pdf (20)

DOCX
package lists; This class represents different animal ch.docx
PPTX
130717666736980000
PPT
Chap 2 Arrays and Structures.ppt
PPTX
Chap 2 Arrays and Structures.pptx
PDF
Everything needs to be according to the instructions- thank you! SUPPO.pdf
PDF
public class Person { private String name; private int age;.pdf
PDF
Main issues with the following code-Im not sure if its reading the fil.pdf
DOCX
SeriesTester.classpathSeriesTester.project SeriesT.docx
PDF
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
PPT
Introduction to objects and inputoutput
PDF
E6
PDF
In this assignment you will implement insert() method for a singly l.pdf
PDF
Please the following is the currency class of perious one- class Curre.pdf
PPTX
Data Type In Python.pptx
PDF
I need help with this program for java.The program you are given t.pdf
DOCX
PPS 4.4ARRAYS ARRAY DECLARATION & INITIALIZATION, BOUND CHECKING ARRAYS (1-D...
PPTX
DOC-20240812-WA0000 array string and.pptx
PDF
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
PPTX
Computer programming 2 Lesson 13
DOCX
Provide copy constructor- destructor- and assignment operator for the.docx
package lists; This class represents different animal ch.docx
130717666736980000
Chap 2 Arrays and Structures.ppt
Chap 2 Arrays and Structures.pptx
Everything needs to be according to the instructions- thank you! SUPPO.pdf
public class Person { private String name; private int age;.pdf
Main issues with the following code-Im not sure if its reading the fil.pdf
SeriesTester.classpathSeriesTester.project SeriesT.docx
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
Introduction to objects and inputoutput
E6
In this assignment you will implement insert() method for a singly l.pdf
Please the following is the currency class of perious one- class Curre.pdf
Data Type In Python.pptx
I need help with this program for java.The program you are given t.pdf
PPS 4.4ARRAYS ARRAY DECLARATION & INITIALIZATION, BOUND CHECKING ARRAYS (1-D...
DOC-20240812-WA0000 array string and.pptx
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Computer programming 2 Lesson 13
Provide copy constructor- destructor- and assignment operator for the.docx
Ad

More from albarefqc (20)

PDF
AONE writes about discovery of potential the ability to search for .pdf
PDF
Answer the following questions1. Where on Earth is the Prime Meri.pdf
PDF
Anwar El-Ibrahimi (Omar Metwally) boards a flight in South Africa bu.pdf
PDF
Antes de liquidar su sociedad, Callie y Russo ten�an cuentas de capi.pdf
PDF
Answer the question with the following1. Procedure2. Facts3. .pdf
PDF
Answer choicesUpwelling zonesCyanobacteriaChemical precipitat.pdf
PDF
Answer 1-4 Answer question 1-4 options A.condyloid joint, B..pdf
PDF
answer about any of the following financial crisisOpec Oil Shock.pdf
PDF
Answer ChoicesNutrient enrichmentCyanobacteriaA compound foun.pdf
PDF
Another research team has measured the allele frequencies in this po.pdf
PDF
Annisa, que tiene 28 a�os y es soltera, tiene un ingreso bruto ajust.pdf
PDF
Annie is concerned over a report that a woman over age 40 has a bet.pdf
PDF
android studio (java)how can i get the number values from one rad.pdf
PDF
Analysts are expecting the government to announce GDP growth for the.pdf
PDF
Anders discovered an old pay statement from 11 years ago. His monthl.pdf
PDF
ANALISIS DE CASOMars Inc.fusi�n de las divisiones europeas de ali.pdf
PDF
An overnight express company must include two cities on its route. H.pdf
PDF
An unusual problem has occurred for a small multinational company P.pdf
PDF
An investor purchases Fannie Mae residential mortgage-backed securit.pdf
PDF
An experiment is given together with an event. Find the (modeled) pr.pdf
AONE writes about discovery of potential the ability to search for .pdf
Answer the following questions1. Where on Earth is the Prime Meri.pdf
Anwar El-Ibrahimi (Omar Metwally) boards a flight in South Africa bu.pdf
Antes de liquidar su sociedad, Callie y Russo ten�an cuentas de capi.pdf
Answer the question with the following1. Procedure2. Facts3. .pdf
Answer choicesUpwelling zonesCyanobacteriaChemical precipitat.pdf
Answer 1-4 Answer question 1-4 options A.condyloid joint, B..pdf
answer about any of the following financial crisisOpec Oil Shock.pdf
Answer ChoicesNutrient enrichmentCyanobacteriaA compound foun.pdf
Another research team has measured the allele frequencies in this po.pdf
Annisa, que tiene 28 a�os y es soltera, tiene un ingreso bruto ajust.pdf
Annie is concerned over a report that a woman over age 40 has a bet.pdf
android studio (java)how can i get the number values from one rad.pdf
Analysts are expecting the government to announce GDP growth for the.pdf
Anders discovered an old pay statement from 11 years ago. His monthl.pdf
ANALISIS DE CASOMars Inc.fusi�n de las divisiones europeas de ali.pdf
An overnight express company must include two cities on its route. H.pdf
An unusual problem has occurred for a small multinational company P.pdf
An investor purchases Fannie Mae residential mortgage-backed securit.pdf
An experiment is given together with an event. Find the (modeled) pr.pdf
Ad

Recently uploaded (20)

PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Cell Types and Its function , kingdom of life
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Pharma ospi slides which help in ospi learning
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Computing-Curriculum for Schools in Ghana
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
Complications of Minimal Access Surgery at WLH
human mycosis Human fungal infections are called human mycosis..pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
A systematic review of self-coping strategies used by university students to ...
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Cell Types and Its function , kingdom of life
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
01-Introduction-to-Information-Management.pdf
Pharma ospi slides which help in ospi learning
Chinmaya Tiranga quiz Grand Finale.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
STATICS OF THE RIGID BODIES Hibbelers.pdf
Computing-Curriculum for Schools in Ghana
102 student loan defaulters named and shamed – Is someone you know on the list?
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Anesthesia in Laparoscopic Surgery in India
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Complications of Minimal Access Surgery at WLH

Adapt your code from Assignment 2 to use both a Java API ArrayList a.pdf

  • 1. 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. HERE IS I.a java code: import java.util.Random;
  • 2. public abstract class I_a { protected final String name; protected final String englishName; protected final String scientificName; protected final double maxLength; protected final double minLength; protected final String[] diet; protected double length; protected double weight; protected String bodyColor; protected String finColor; protected String sex; protected double growthRate = 0.8; /** * constructor. **/ public I_a(String nName, String nEnglishName, String nScientificName, double nMaxLength, double nMinLength, double nLength, double nWeight, String[] nDiet, String nBodyColor, String nFinColor, String nSex) throws FishSizeException { //set instance variables this.name = nName; this.englishName = nEnglishName; this.scientificName = nScientificName; this.maxLength = nMaxLength; this.minLength = nMinLength; if (nLength >= this.minLength && nLength < this.maxLength) { this.length = nLength;
  • 3. } else { throw new FishSizeException(name + " length must be within " + this.minLength + " and " + this.maxLength + " inches long. But was sent " + nLength); } this.weight = nWeight; this.diet = nDiet; this.bodyColor = nBodyColor; this.finColor = nFinColor; this.sex = nSex; } /*** abstract methods to be implemented in the sub-classes ****/ public abstract void setLength(double newLength); public abstract void setWeight(double newWeight); public abstract void eat(String food); protected abstract void grow(); protected abstract I_a levelUp(); //########### Protected class method ################ /** initialize random fish sex * choose randomly male or female * used by some subclasses. */ protected void initSex() { //randomize sex Random ran = new Random(); int flip = ran.nextInt(2); if (flip == 0) { this.sex = "male";
  • 4. } else { this.sex = "female"; } } /*** public class methods - methods used by all sub-classes ***/ /** * Returns I_a information as a formatted String. * @return String representing I_a object data. */ public String toString() { String s = "Name: " + this.name + "n"; s = s + "English name: " + this.englishName + "n"; s = s + "Scientific name: " + this.scientificName + "n"; s = s + "Length: " + this.length + "n"; s = s + "Weight: " + this.weight + "n"; s = s + "Body color: " + this.bodyColor + "n"; s = s + "Fin color: " + this.finColor + "n"; s = s + "Sex: " + this.sex; return s; } /** Get Methods **/ public String getName() { return this.name; } public String getEnglishName() { return this.englishName; } public String getScientificName() { return this.scientificName; }
  • 5. public double getMaxLength() { return this.maxLength; } public double getMinLength() { return this.minLength; } public double getLength() { return this.length; } public double getWeight() { return this.weight; } public String[] getDiet() { return this.diet; } public String getBodyColor() { return this.bodyColor; } public String getFinColor() { return this.finColor; } public String getSex() { return this.sex; } } Code from A2: PokemonArray.java import java.util.Scanner;
  • 6. public class PokeArray { /**set the size for array.*/ static final int SIZE = 6; /**Sanner.*/ static final Scanner sc = new Scanner(System.in); public static void main(String[] args) { Pokemon[] pokemonArray = new Pokemon[SIZE]; int curIndex = 0; boolean choice = false; int inNumber = -1; //loop while (!choice) { System.out.println("1. Add a Pokemon"); System.out.println("2. Print all Pokemon"); System.out.println("0. Exit the Program"); System.out.println("What would you like to do?"); inNumber = sc.nextInt(); sc.nextLine(); switch (inNumber) { case 1: Pokemon p = makePokemon(); pokemonArray[curIndex % SIZE] = p; curIndex++; break; case 2: printArray(pokemonArray); break; case 0: choice = true; System.out.print("Goodbye!!"); break; default: System.out.println("Invalid option, please make a selection");
  • 7. break; } } sc.close(); } public static Pokemon makePokemon() { System.out.print("What is the Pokemon's species: "); String species = sc.nextLine(); System.out.println("Does the Pokemon have a name Y/N: "); String hasName = sc.nextLine(); String name = ""; if (hasName.equalsIgnoreCase("Y")) { System.out.println("What is the Pokemon's name? "); name = sc.nextLine(); } else if (hasName.equalsIgnoreCase("N")) { System.out.println("Name is empty"); } System.out.println("What is the Pokemon's Pokedex number? "); int number = -1; while (!sc.hasNextInt()) { System.out.println("Please enter the valid number: "); sc.nextLine(); } number = sc.nextInt(); sc.nextLine(); System.out.println("What is the Pokemon's Type? "); String type1 = sc.nextLine(); System.out.println("What is the Pokemon's Second Type (if it has one)? "); String type2 = sc.nextLine(); return new Pokemon(species, name, number, type1, type2);
  • 8. } public static void printArray(Pokemon[] pokemonArray) { for (int i = 0; i < SIZE; i++) { Pokemon p = pokemonArray[i]; if (p != null) { System.out.println("Pokemon " + (i + 1) + ":"); System.out.print(p); } } } }