ICS 211 homework need help ArrayListOfObjects Assignment 4 1
ICS 211 homework need help~
ArrayListOfObjects
Assignment 4:
100 points
due Thurs, Feb 23rd.
Adapt your ArrayOfPokemon code from assignment 2 to use the new Pokemon objects.
When adding a Pokemon, instead of making the user enter all the information, make a menu for them to pick from the nine (or more) implemented species: (Bulbasaur, Ivysaur, Venusaur, Charmander, Charmaleon, Charizard, Squirtle, Wartortle, Blastoise).The only thing the user must choose is the species of Pokemon and if it gets a name.
If they want a name, then the program should ask them to enter one.
Then call the constructor for the chosen Pokemon. For example: Pokemon p = new Bulbasaur(); or Pokemon p = new Squirtle(sName);
Change the Array from assignment 2 to a Java API ArrayList.
For examples on using an ArrayList, see the uploaded sample code ArrayListDemo.java and slide 40 of ICS211_Lecture09_ListsAndContainers.pdf on Laulima -> Resources -> Week 5. Also see the Java API website for ArrayList for available methods.
The allowed number of stored Pokemon should still be limited to 6. Don\'t let the ArrayList keep growing though it can!
Assignment 2 code is here.
| import java.util.*; //scanner | |
| /** | |
| * Solution to Assignment 1b - Array of Pokemon | |
| * @author Lisa Miller | |
| * @since 9/7/2016 | |
| */ | |
| public class PokeArray{ | |
| static final int SIZE = 6; | |
| public static void main(String[] args){ | |
| Scanner userIn = new Scanner(System.in); | |
| String inString = new String(\"\"); | |
| boolean endLoop = false; | |
| Pokemon[] arrPoke = new Pokemon[SIZE]; | |
| int nextIndex = 0; //points to index for next insert | |
| //loop until stopping condition is given | |
| while(endLoop != true){ | |
| //menu text | |
| System.out.println(\"Please enter the number of your choice:\"); | |
| System.out.println(\"1. Add a Pokemon\"); | |
| System.out.println(\"2. Print all Pokemon\"); | |
| System.out.println(\"0. Exit the program\"); | |
| System.out.print(\"What would you like to do? \"); | |
| //read in from user as a String -- much less errors can happen! | |
| inString = userIn.nextLine(); | |
| //take off any spaces on the string | |
| inString = inString.trim(); | |
| //just switch on the String no need to convert to int | |
| switch(inString){ | |
| case \"0\": endLoop = true; | |
| System.out.println(\"Good bye!\"); | |
| break; | |
| case \"1\": //do stuff to make a new Pokemon | |
| arrPoke[nextIndex] = PokeArray.makePokemon(); | |
| nextIndex = (nextIndex + 1)% SIZE; //makes value run from 0-5 always | |
| break; | |
| case \"2\": //print out all the Pokemon | |
| PokeArray.printArray(arrPoke); | |
| break; | |
| default: //not a valid menu entry | |
| System.out.println(\"\ ****Invalid menu choice.****\ Please enter a 0, 1, or 2\ \"); | |
| break; | |
| } | |
| } | |
| }//close main method | |
| /* | |
| * makePokemon asks user for data for a new Pokemon object and instantiates it | |
| * | |
| * @return a Pokemon to insert into the array | |
| */ | |
| public static Pokemon makePokemon(){ | |
| Scanner userIn = new Scanner(System.in); | |
| Pokemon newPoke; | |
| //Data for new Pokemon constructor | |
| String inName = new String(\"\"); | |
| String inNick = new String(\"\"); | |
| String inNum = new String(\"\");//have to convert to an int | |
| String inType1 = new String(\"\"); | |
| String inType2 = new String(\"\"); | |
| int iNum = 0; | |
| boolean dataGood = false; | |
| //keep taking input until user enters valid input | |
| while(dataGood == false){ | |
| //this can be used later when Pokemon exception is made too | |
| try{ | |
| System.out.println(\"Enter the Pokemon\'s name:\"); | |
| inName = userIn.nextLine(); | |
| inName = inName.trim(); | |
| System.out.println(\"Enter a nickname, or just enter if none:\"); | |
| inNick = userIn.nextLine(); | |
| inNick = inNick.trim(); | |
| System.out.println(\"Enter the Pokemon\'s number:\"); | |
| inNum = userIn.nextLine(); | |
| inNum = inNum.trim(); | |
| iNum = Integer.parseInt(inNum); //parse into an int | |
| System.out.println(\"Enter the Pokemon\'s type:\"); | |
| inType1 = userIn.nextLine(); | |
| inType1 = inType1.trim(); | |
| System.out.println(\"Enter the Pokemon\'s second type, if none just enter:\"); | |
| inType2 = userIn.nextLine(); | |
| inType2 = inType2.trim(); | |
| dataGood = true; | |
| }catch(NumberFormatException nfe){ | |
| System.out.println(\"You didn\'t enter a valid number, try again\"); | |
| } | |
| } | |
| if(inNick.length() > 0){//call 5 parameter constructor | |
| newPoke = new Pokemon(inName, inNick, iNum, inType1, inType2); | |
| }else{//call constructor without nickname | |
| newPoke = new Pokemon(inName, iNum, inType1, inType2); | |
| } | |
| return newPoke; | |
| } | |
| /* | |
| * printArray prints array of Pokemon | |
| * @param pArray an array of Pokemon Objects | |
| */ | |
| public static void printArray(Pokemon[] pArray){ | |
| for(int i = 0; i < SIZE; i++){ | |
| if(pArray[i] != null){//don\'t print the indicies that are not filled | |
| System.out.println(\"Pokemon \"+ i + \": \"); | |
| System.out.println(pArray[i].toString()); | |
| } | |
| } | |
| }//end printArray method | |
| } |
Solution
importjava.util.*; //scanner /** * Solution to Assignment 1b - Array of Pokemon * @author Lisa Miller * @since 9/7/2016 */ public class PokeArray{ static final int SIZE = 6; public static void main(String[] args){ Scanner userIn = new Scanner(System.in); String inString = new String(\"\"); boolean endLoop = false; Pokemon[] arrPoke = new Pokemon[SIZE]; int nextIndex = 0; //points to index for next insert //loop until stopping condition is given while(endLoop != true){ //menu text System.out.println(\"Please enter the number of your choice:\"); System.out.println(\"1. Add a Pokemon\"); System.out.println(\"2. Print all Pokemon\"); System.out.println(\"0. Exit the program\"); System.out.print(\"What would you like to do? \"); //read in from user as a String -- much less errors can happen! inString = userIn.nextLine(); //take off any spaces on the string inString = inString.trim(); //just switch on the String no need to convert to int switch(inString){ case \"0\": endLoop = true; System.out.println(\"Good bye!\"); break; case \"1\": //do stuff to make a new Pokemon arrPoke[nextIndex] = PokeArray.makePokemon(); nextIndex = (nextIndex + 1)% SIZE; //makes value run from 0-5 always break; case \"2\": //print out all the Pokemon PokeArray.printArray(arrPoke); break; default: //not a valid menu entry System.out.println(\"\ ****Invalid menu choice.****\ Please enter a 0, 1, or 2\ \"); break; } } }//close main method /* * makePokemon asks user for data for a new Pokemon object and instantiates it * * @return a Pokemon to insert into the array */ public static Pokemon makePokemon(){ Scanner userIn = new Scanner(System.in); Pokemon newPoke; //Data for new Pokemon constructor String inName = new String(\"\"); String inNick = new String(\"\"); String inNum = new String(\"\");//have to convert to an int String inType1 = new String(\"\"); String inType2 = new String(\"\"); int iNum = 0; boolean dataGood = false; //keep taking input until user enters valid input while(dataGood == false){ //this can be used later when Pokemon exception is made too try{ System.out.println(\"Enter the Pokemon\'s name:\"); inName = userIn.nextLine(); inName = inName.trim(); System.out.println(\"Enter a nickname, or just enter if none:\"); inNick = userIn.nextLine(); inNick = inNick.trim(); System.out.println(\"Enter the Pokemon\'s number:\"); inNum = userIn.nextLine(); inNum = inNum.trim(); iNum = Integer.parseInt(inNum); //parse into an int System.out.println(\"Enter the Pokemon\'s type:\"); inType1 = userIn.nextLine(); inType1 = inType1.trim(); System.out.println(\"Enter the Pokemon\'s second type, if none just enter:\"); inType2 = userIn.nextLine(); inType2 = inType2.trim(); dataGood = true; }catch(NumberFormatException nfe){ System.out.println(\"You didn\'t enter a valid number, try again\"); } } if(inNick.length() > 0){//call 5 parameter constructor newPoke = new Pokemon(inName, inNick, iNum, inType1, inType2); }else{//call constructor without nickname newPoke = new Pokemon(inName, iNum, inType1, inType2); } return newPoke; } /* * printArray prints array of Pokemon * @param pArray an array of Pokemon Objects */ public static void printArray(Pokemon[] pArray){ for(int i = 0; i < SIZE; i++){ if(pArray[i] != null){//don\'t print the indicies that are not filled System.out.println(\"Pokemon \"+ i + \": \"); System.out.println(pArray[i].toString()); } } }//end printArray method }


