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,Please help me change it to match Assignment 4\'s requirement.
This is the Pokemon.java code.import java.util.*; //scannerpublic 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 givenwhile(endLoop != true){//menu textSystem.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 stringinString = inString.trim();//just switch on
the String no need to convert to intswitch(inString){case \"0\": endLoop =
true;System.out.println(\"Good bye!\");break;case \"1\": //do stuff to make a new
PokemonarrPoke[nextIndex] = PokeArray.makePokemon();nextIndex = (nextIndex + 1)%
SIZE; //makes value run from 0-5 alwaysbreak;case \"2\": //print out all the
PokemonPokeArray.printArray(arrPoke);break;default: //not a valid menu
entrySystem.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 constructorString inName = new String(\"\");String inNick = new String(\"\");String
inNum = new String(\"\");//have to convert to an intString inType1 = new String(\"\");String
inType2 = new String(\"\");int iNum = 0;boolean dataGood = fals.
ArrayListOfObjectsAssignment 4100 pointsdue Thurs, Feb 23rd..pdf
1. 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,Please help me change it to match Assignment 4's requirement.
This is the Pokemon.java code.import java.util.*; //scannerpublic 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 givenwhile(endLoop != true){//menu textSystem.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 stringinString = inString.trim();//just switch on
the String no need to convert to intswitch(inString){case "0": endLoop =
true;System.out.println("Good bye!");break;case "1": //do stuff to make a new
PokemonarrPoke[nextIndex] = PokeArray.makePokemon();nextIndex = (nextIndex + 1)%
SIZE; //makes value run from 0-5 alwaysbreak;case "2": //print out all the
PokemonPokeArray.printArray(arrPoke);break;default: //not a valid menu
entrySystem.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
2. 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 constructorString inName = new String("");String inNick = new String("");String
inNum = new String("");//have to convert to an intString inType1 = new String("");String
inType2 = new String("");int iNum = 0;boolean dataGood = false;//keep taking input until user
enters valid inputwhile(dataGood == false){//this can be used later when Pokemon exception is
made tootry{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 intSystem.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
constructornewPoke = new Pokemon(inName, inNick, iNum, inType1, inType2);}else{//call
constructor without nicknamenewPoke = 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
filledSystem.out.println("Pokemon "+ i + ": ");
System.out.println(pArray[i].toString());}}}//end printArray method}
Solution
/*The main difference between Assignment2 and Assignment4 is implementation of ArrayList
and Inheritance*/
import java.util.*; //scanner
public class PokeArray{
static final int SIZE = 6;
public static void main(String[] args){
3. Scanner userIn = new Scanner(System.in);
String inString = new String("");
boolean endLoop = false;
ArrayList pokeList=new ArrayList(SIZE);
//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
pokeList.add(PokeArray.makePokemon());
break;
case "2": //print out all the Pokemon
PokeArray.printArray(pokeList);
break;
default: //not a valid menu entry
System.out.println(" ****Invalid menu choice.**** Please enter a 0, 1, or 2 ");
break;
4. }
}
}//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("");
int iNum = 0;
boolean dataGood = false;
//keep taking input until user enters valid input
//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();
if(inNick.length() > 0){//call 5 parameter constructor
if(inName=="Bulbasaur")
newPoke = new Bulbasaur(inNick);
else if(inName=="Ivysaur")
newPoke = new Ivysaur(inNick);
else if(inName=="Venusaur")
5. newPoke = new Venusaur(inNick);
else if(inName=="Charmander")
newPoke = new Charmander(inNick);
else if(inName=="Charmaleon")
newPoke = new Charmaleon(inNick);
else if(inName=="Charizard")
newPoke = new Charizard(inNick);
else if(inName=="Squirtle")
newPoke = new Squirtle(inNick);
else if(inName=="Wartotle")
newPoke = new Wartotle(inNick);
else if(inName=="Blastoise")
newPoke = new Blastoise(inNick);
else throw new Exception ();
}
else{//call constructor without nickname
if(inName=="Bulbasaur")
newPoke = new Bulbasaur();
else if(inName=="Ivysaur")
newPoke = new Ivysaur();
else if(inName=="Venusaur")
newPoke = new Venusaur();
else if(inName=="Charmander")
newPoke = new Charmander();
else if(inName=="Charmaleon")
newPoke = new Charmaleon();
else if(inName=="Charizard")
newPoke = new Charizard();
else if(inName=="Squirtle")
newPoke = new Squirtle();
else if(inName=="Wartotle")
newPoke = new Wartotle();
else if(inName=="Blastoise")
newPoke = new Blastoise();
else throw new Exception ();
}
6. }catch(Exception e){
System.out.println("Pokemon not found!! ");
}
return newPoke;
}
/*
* printArray prints array of Pokemon
* @param pArray an array of Pokemon Objects
*/
public static void printArray(ArrayList pArray){
Iterator itr=pArray.iterator();
//traversing elements of ArrayList object
while(itr.hasNext()){
Pokemon st=(Pokemon)itr.next();
System.out.println(st.toString());
}
}//end printArray method
}
Pokemon.java
import java.util.Random;
import java.text.*; //imports decimal format
public class Pokemon{
/** instance variables **/
/* the actual kind of Pokemon */
private String name;
/* optional user-defined name */
private String nickName;
/* official Pokemon number for Pokedex */
private int number;
7. /* required type of Pokemon */
private String type1;
/* optional second type */
private String type2;
/* interally calculated Hit Points */
private int HP;
/* internally calculated Combat Power */
private int CP;
/* main constructor with nickName and two types
* @param name official Pokemon name for this kind of Pokemon
* @param nickName user-given name or name if none
* @param number official Pokedex number for this Pokemon
* @param type1 the Pokemon type
* @param type2 second type if Pokemon has one, otherwise empty string
*/
public Pokemon(String name, String nickName, int number, String type1, String type2){
this.name = name;
this.nickName = nickName;
this.type1 = type1;
this.type2 = type2;
this.number = number;
initHPCP();
}
/* constructor with no nickname and two types
* calls other constructor with nickName set to name
* @param name the official Pokemon name for this kind of Pokemon
* @param number official Pokedex number for this Pokemon
* @param type1 the Pokemon type
* @param type2 second type if Pokemon has one
*/
/*
8. * private method for initializing HP and CP from constructors
*/
private void initHPCP(){
//generate random numbers
Random randGen = new Random();
int newHP;
int newCP;
double multiplier;
double CPrangeMin = 1.00;
double CPrangeMax = 3.00;
newHP = randGen.nextInt(141) + 10; //integer between 10 and 150 inclusive
this.HP = newHP;
multiplier = CPrangeMin + (CPrangeMax - CPrangeMin)*randGen.nextDouble();
this.CP = (int)(newHP*multiplier);
}
/*
* Returns Pokemon information as a formatted String
* @return String representing Pokemon object data
*/
public String toString( ){
DecimalFormat df = new DecimalFormat("000");
String s="";
s = "Name: " + this.name + " ";
if(this.name.compareTo(this.nickName) != 0){
s = s + "Nickname: " + this.nickName + " ";
}
s = s + "Number: " + df.format(this.number) + " ";
s = s + "Type: " + this.type1;
if(this.type2.length() > 0){
s = s + " | " + this.type2;
}
s = s+ " ";
9. s = s + "HP: " + this.HP + " ";
s = s + "CP: " + this.CP + " ";
return s;
}
/*
* updates a Pokemon's HP and CP
* CP only updated if random multiplier increases it
* otherwise it stays the same
*/
public void powerUp(){
Random randGen = new Random();
int newHP;
double multiplier;
double CPrangeMin = 1.00;
double CPrangeMax = 3.00;
this.HP = (int)(this.HP + 1.15 + (0.2*this.HP));//set HP using formula
multiplier = CPrangeMin + (CPrangeMax - CPrangeMin)*randGen.nextDouble();
if((this.HP * multiplier) > this.CP){
this.CP = (int)(this.HP * multiplier);
}
}
/** Get Methods **/
public String getName(){
return this.name;
}
public String getNickName(){
return this.nickName;
}
public String getType1(){
return this.type1;
}
10. public String getType2(){
return this.type2;
}
public int getNumber(){
return this.number;
}
public int getHP(){
return this.HP;
}
public int getCP() {
return this.CP;
}
/*
* only Nickname is user-settable
* @param String the new nickname for this Pokemon
*/
public void setNickName(String newNickName){
this.nickName = newNickName;
}
}
public class Bulbasaur extends Pokemon{
Bulbasaur()
{
super("Bulbasaur","Bulbasaur",1,"Grass","");
}
Bulbasaur(String sName)
{
super("Bulbasaur",sName,1,"Grass","");
}
}
public class Ivysaur extends Pokemon{
Ivysaur()
{
super("Ivysaur","Ivysaur",2,"Grass","");
}
11. Ivysaur(String sName)
{
super("Ivysaur",sName,2,"Grass","");
}
}
public class Venusaur extends Pokemon{
Venusaur()
{
super("Venusaur","Venusaur",3,"Grass","");
}
Venusaur(String sName)
{
super("Venusaur",sName,3,"Grass","");
}
}
public class Charmander extends Pokemon{
Charmander()
{
super("Charmander","Charmander",4,"Fire","");
}
Charmander(String sName)
{
super("Charmander",sName,4,"Fire","");
}
}
public class Charmaleon extends Pokemon{
Charmaleon()
{
super("Charmaleon","Charmaleon",5,"Fire","");
}
Charmaleon(String sName)
{
super("Charmaleon",sName,5,"Fire","");
}
}
public class Charizard extends Pokemon{
12. Charizard()
{
super("Charizard","Charizard",6,"Fire","");
}
Charizard(String sName)
{
super("Charizard",sName,6,"Fire","");
}
}
public class Squirtle extends Pokemon{
Squirtle()
{
super("Squirtle","Squirtle",7,"Water","");
}
Squirtle(String sName)
{
super("Squirtle",sName,7,"Water","");
}
}
public class Wartotle extends Pokemon{
Wartotle()
{
super("Wartotle","Wartotle",8,"Water","");
}
Wartotle(String sName)
{
super("Wartotle",sName,8,"Water","");
}
}
public class Blastoise extends Pokemon{
Blastoise()
{
super("Blastoise","Blastoise",9,"Water","");
}
Blastoise(String sName)
{