SlideShare a Scribd company logo
1 of 24
Download to read offline
import java.util.ArrayList;
import java.util.Iterator;
/**
* A PokemonSpecies entry in the Pokedex. Maintains the number of candies associated
* with the Pokemon species as well as the Trainer's inventory of Pokemon of this
* species.
*/
public class PokemonSpecies {
private int pokedexNumber;
private String speciesName;
private int candies;
/**
* Maintains the list of Pokemon of this species in the Trainer's inventory.
*/
private ArrayList caughtPokemon;
/**
* Constructor suitable for a newly encountered Pokemon species during the course of the
* game and for loading species data from a save file.
*
* @param pokedexNumber the Pokedex Number for the species
* @param speciesName the name of the species
* @param candies the number of candies the player has obtained by catching
* or transferring Pokemon of this species
*/
public PokemonSpecies(int pokedexNumber, String speciesName, int candies) {
this.pokedexNumber = pokedexNumber;
this.speciesName = speciesName;
this.candies = candies;
// construct caughtPokemon
caughtPokemon = new ArrayList();
}
/**
* Getter methods
*/
public Integer getPokedexNumber() {
return pokedexNumber;
}
public String getSpeciesName() {
return speciesName;
}
public int getCandies() {
return candies;
}
/**
* Add a newly caught Pokemon to the player's inventory and
* increase the number of candies for this PokemonSpecies
*
* @param pokemon the newly caught Pokemon
*/
public void addNewPokemon(Pokemon pokemon) {
// TODO
caughtPokemon.add(pokemon);
this.candies++;
}
/**
* Helper function to load Pokemon from a save file into the player's inventory for this
* Pokemon Species
*
* @param pokemon the pokemon to add to this species
*/
public void loadPokemon(Pokemon pokemon) {
caughtPokemon.add(pokemon);
}
/**
* Find a Pokemon of the given combatPower in the player's inventory for this species.
*
* @param cp the combatPower of the Pokemon to find
* @throws PokedexException [Config.POKEMON_NOT_FOUND] if there is no Pokemon
with the
* given combatPower in the player's inventory.
* @return the first Pokemon with the provided combatPower
*/
public Pokemon findPokemon(int cp) throws PokedexException {
// TODO
for(int i=0;i 0){
return true;
}
return false;
}
/**
* Increment candies when a new pokemon is caught
*/
private void addNewPokemonCandies() {
this.candies += PokemonGO.NEW_POKEMON_CANDIES;
}
/**
* Increment candies when a pokemon is transferred to the professor
*/
private void addTransferCandies() {
this.candies += PokemonGO.TRANSFER_POKEMON_CANDIES;
}
/**
* Prepare a listing of all the Pokemon of this species that are currently in the
* player's inventory.
*
* @return a String of the form
* ...
*/
public String caughtPokemonToString() {
// TODO
return pokedexNumber+" Name: "+speciesName+" Candies"+candies;
}
/**
* Prepare a String representing this entire PokemonSpecies. This is used to
* save the PokemonSpecies (one part of the Pokedex) to a file to
* save the player's game.
*
* @return a String of the form
* [, , ...]
*/
public String toString() {
// TODO
return pokedexNumber+" Name: "+speciesName+" Candies"+candies;
}
}
--------------------------------------------------------------------------------------------------------------------
-------------------------------------
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.InputMismatchException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
/**
* The main class. Provides the main method which is responsible for game play.
* Also provides assistance for gameplay with functions to generate Pokemon, and
* save a player's progress.
*/
public class PokemonGO {
public static final int NEW_POKEMON_CANDIES = 3;
public static final int TRANSFER_POKEMON_CANDIES = 1;
/**
* The game begins here! The set of Pokemon that the player will encounter
* must be provided as the first and only command-line argument which will
* be loaded into the {@link PokemonDB}
*
* Players are prompted to enter their name which will be used to save their
* progress. Players are then prompted with a set of menu options which can
* be selected by entering a single character e.g. 's' for "search". Options
* include search for Pokemon, display encountered Pokemon, display caught Pokemon,
* transfer caught Pokemon, and quit.
*
* @param args the command line arguments. args[0] must be a file which is loaded
* by {@link PokemonDB}
*/
public static void main(String[] args) throws FileNotFoundException {
// Interpret command-line arguments and use them to load PokemonDB
if(args.length != 1) {
System.err.println("Invalid arguments");
System.exit(1);
}
PokemonDB db = new PokemonDB(args[0]);
// Start
System.out.println(Config.WELCOME);
// Prompt user to enter player name
System.out.println(Config.USER_PROMPT);
// Take name of Pokemon trainer
String playerName = Config.getNextLine();
// Provide a name for a txt file which will be used to save the player's progress
String playerFileName = playerName + ".txt";
Pokedex pokedex = new Pokedex();
try {
pokedex.loadFromFile(playerFileName);
} catch (FileNotFoundException e) {
// then the player has not saved any progress yet, start a new game!
}
PokemonTrainer pokemonTrainer = new PokemonTrainer(playerName, pokedex);
System.out.println("Hi " + playerName);
// main menu for the game. accept commands until the player enters 'q' to quit
String option = null;
while ((!"Q".equals(option)) && (!"q".equals(option))) {
// Ask user to choose
// [C] display caught Pokemon
// [D]isplay encountered Pokemon
// [S]earch for Pokemon
// [T]ransfer Pokemon to the Professor
// [Q]uit
System.out.println(Config.MENU_PROMPT);
option = Config.getNextLine();
switch(option) {
case "C":
case "c":
System.out.println(pokemonTrainer.getPokedex().caughtPokemonMenu());
break;
case "D":
case "d":
System.out.println(pokemonTrainer.getPokedex().seenPokemonMenu());
break;
case "S":
case "s":
Pokemon wildPokemon = encounterPokemon(db);
// Provide alternative guessing options
int[] pokedexNumbers = new int[Config.DIFFICULTY];
pokedexNumbers[0] = wildPokemon.getPokedexNumber();
for(int i = 1; i < pokedexNumbers.length; i++) {
pokedexNumbers[i] = db.generatePokedexNumber();
}
Arrays.sort(pokedexNumbers);
// Prompt user for input
System.out.println(String.format(
Config.ENCOUNTERED_POKEMON,
wildPokemon.getSpecies(),
wildPokemon.getCombatPower(),
Arrays.toString(pokedexNumbers)));
int guessedId = 0;
while(guessedId < 1 || guessedId > 151) {
// then prompt is invalid
try {
guessedId = Config.getNextInteger();
if(guessedId < 1 || guessedId > 151) {
throw new RuntimeException(Config.INVALID_INT_INPUT);
}
} catch (InputMismatchException e) {
System.out.println(Config.INVALID_INT_INPUT);
Config.getNextLine();
} catch (RuntimeException e) {
System.out.println(Config.INVALID_INT_INPUT);
}
}
if (guessedId == wildPokemon.getPokedexNumber()) {
// guessed correctly, pokemon is captured
pokemonTrainer.capturePokemon(wildPokemon);
System.out.println(String.format(
Config.SUCCESSFUL_CAPTURE,
playerName,
wildPokemon.getSpecies(),
wildPokemon.getCombatPower()));
}
else {
// guessed incorrectly, pokemon escapes
pokemonTrainer.seePokemon(wildPokemon);
System.out.println(String.format(
Config.FAILED_CAPTURE,
playerName,
wildPokemon.getSpecies()));
}
break;
case "T":
case "t":
// Select Pokemon species to transfer
System.out.println(Config.TRANSFER_PROMPT);
pokedex = pokemonTrainer.getPokedex();
String speciesName = Config.getNextLine();
if(speciesName.toLowerCase().equals("cancel")) {
break;
}
try {
db.lookupPokedexNumber(speciesName);
} catch (PokedexException e) {
System.out.println(e.toString());
break;
}
// Begin transfer of selected species
PokemonSpecies species = null;
try {
species = pokedex.findCaughtSpeciesData(speciesName);
} catch (PokedexException e) {
System.out.println(e.toString());
break;
}
String transferPokemonName = species.getSpeciesName();
// Select Pokemon of that species to transfer
System.out.println(String.format(
Config.TRANSFER_CP_PROMPT,
transferPokemonName,
species.caughtPokemonToString()));
int transferPokemonCp = -1;
while(transferPokemonCp == -1) {
try {
transferPokemonCp = Config.getNextInteger();
if(transferPokemonCp < 0) {
System.out.println(Config.INVALID_CP_INPUT);
transferPokemonCp = -1;
}
} catch (InputMismatchException e) {
System.out.println(Config.INVALID_CP_INPUT);
transferPokemonCp = -1;
Config.getNextLine();
} catch (RuntimeException e) {
System.out.println(Config.INVALID_CP_INPUT);
transferPokemonCp = -1;
}
}
if(transferPokemonCp == 0) {
break;
}
try {
// Call transfer function; should throw exceptions within transfer but are to be caught here
pokemonTrainer.transferPokemon(transferPokemonName, transferPokemonCp);
System.out.println(String.format(
Config.SUCCESSFUL_TRANSFER,
transferPokemonName,
transferPokemonCp));
}
catch (PokedexException pokedexException) {
System.out.println (pokedexException.toString());
}
break;
case "Q":
case "q":
break;
default:
System.out.println(Config.INVALID_RESPONSE);
}
}
// Save the game when the player quits
File outFile = new File(playerFileName);
saveGame(outFile, pokemonTrainer);
System.out.println(String.format(
Config.QUIT_MESSAGE,
playerName));
}
/**
* A wild has appeared! ~*~ battle music plays ~*~
*
* @param db the PokemonDB to generate a Pokemon from
* @return a Pokemon for the encounter
*/
public static Pokemon encounterPokemon(PokemonDB db) {
// random number to pick pokemon
int pokedexNumber = db.generatePokedexNumber();
String species = db.lookupSpeciesName(pokedexNumber);
// random number to decide CP
int cp = Config.CP_GENERATOR.nextInt(Config.MAX_CP-1)+1; // adjustments for origin 0/1
Pokemon wildPokemon = new Pokemon(pokedexNumber, species, cp);
return wildPokemon;
}
/**
* Save the game by writing the Pokedex into a file. The game can be loaded again
* by functions provided by the {@link Pokedex} class
*
* @param outFile the file handle to write the game progress to
* @param pokemonTrainer the player whose game we are saving
*/
public static void saveGame(File outFile, PokemonTrainer pokemonTrainer) throws
FileNotFoundException {
// TODO
outFile.println(pokemonTrainer);
}
}
--------------------------------------------------------------------------------------------------------------------
-----------------------------------------
import java.io.File;
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Iterator;
/**
* This class provides functions for interacting with the set of Pokemon that the player
* will encounter during the course of the game.
*/
public class PokemonDB {
public static final int NUM_POKEMON = 151;
public static final int WILD_CANDIES = 3;
private static ArrayList pokemonSpeciesNames;
/**
* Create a PokemonDB by parsing the Pokemon file in @path@
*
* @param path the file path that contains the Pokemon used for this game
*/
public PokemonDB(String path) throws FileNotFoundException {
pokemonSpeciesNames = new ArrayList();
File infile = new File(path);
loadFile(infile);
}
/**
* Parse Pokemon file with format
* 
* File lines must be ordered from PokedexNumber and start from 1
*
* @param infile the file with all the Pokemon that will be used for this game
*/
public void loadFile(File infile) throws FileNotFoundException {
Scanner sc = new Scanner(infile);
while(sc.hasNextLine()) {
String line = sc.nextLine();
String[] tokens = line.split("s+");
String name = tokens[1];
name = name.toLowerCase(); // to make sure all cases are acceptable
pokemonSpeciesNames.add(name);
}
sc.close();
}
/**
* Generate a Pokedex Number to (1) provide a Pokemon for an encounter and (2)
* provide a set of alternative options for the Pokedex Number guessing game to
* capture the encountered Pokemon
*
* @return the Pokedex Number generated
*/
public int generatePokedexNumber() {
int pokedexNumber =
Config.POKEDEX_NUMBER_GENERATOR.nextInt(PokemonDB.NUM_POKEMON-1)+1; //
adjustments for origin 0/1
return pokedexNumber;
}
/**
* Lookup a Pokemon species name using its Pokedex Number (the index+1 of the
* string in pokemonSpeciesNames which matches the parameter pokedexNumber)
*
* @param pokedexNumber find the Pokemon species name associated with this number
* @throws PokedexException thrown if the pokedexNumber is not in the range 1 to
* NUM_POKEMON (inclusive)
*/
public String lookupSpeciesName(int pokedexNumber) throws PokedexException {
// TODO
for(int i=0;i
Solution
import java.util.ArrayList;
import java.util.Iterator;
/**
* A PokemonSpecies entry in the Pokedex. Maintains the number of candies associated
* with the Pokemon species as well as the Trainer's inventory of Pokemon of this
* species.
*/
public class PokemonSpecies {
private int pokedexNumber;
private String speciesName;
private int candies;
/**
* Maintains the list of Pokemon of this species in the Trainer's inventory.
*/
private ArrayList caughtPokemon;
/**
* Constructor suitable for a newly encountered Pokemon species during the course of the
* game and for loading species data from a save file.
*
* @param pokedexNumber the Pokedex Number for the species
* @param speciesName the name of the species
* @param candies the number of candies the player has obtained by catching
* or transferring Pokemon of this species
*/
public PokemonSpecies(int pokedexNumber, String speciesName, int candies) {
this.pokedexNumber = pokedexNumber;
this.speciesName = speciesName;
this.candies = candies;
// construct caughtPokemon
caughtPokemon = new ArrayList();
}
/**
* Getter methods
*/
public Integer getPokedexNumber() {
return pokedexNumber;
}
public String getSpeciesName() {
return speciesName;
}
public int getCandies() {
return candies;
}
/**
* Add a newly caught Pokemon to the player's inventory and
* increase the number of candies for this PokemonSpecies
*
* @param pokemon the newly caught Pokemon
*/
public void addNewPokemon(Pokemon pokemon) {
// TODO
caughtPokemon.add(pokemon);
this.candies++;
}
/**
* Helper function to load Pokemon from a save file into the player's inventory for this
* Pokemon Species
*
* @param pokemon the pokemon to add to this species
*/
public void loadPokemon(Pokemon pokemon) {
caughtPokemon.add(pokemon);
}
/**
* Find a Pokemon of the given combatPower in the player's inventory for this species.
*
* @param cp the combatPower of the Pokemon to find
* @throws PokedexException [Config.POKEMON_NOT_FOUND] if there is no Pokemon
with the
* given combatPower in the player's inventory.
* @return the first Pokemon with the provided combatPower
*/
public Pokemon findPokemon(int cp) throws PokedexException {
// TODO
for(int i=0;i 0){
return true;
}
return false;
}
/**
* Increment candies when a new pokemon is caught
*/
private void addNewPokemonCandies() {
this.candies += PokemonGO.NEW_POKEMON_CANDIES;
}
/**
* Increment candies when a pokemon is transferred to the professor
*/
private void addTransferCandies() {
this.candies += PokemonGO.TRANSFER_POKEMON_CANDIES;
}
/**
* Prepare a listing of all the Pokemon of this species that are currently in the
* player's inventory.
*
* @return a String of the form
* ...
*/
public String caughtPokemonToString() {
// TODO
return pokedexNumber+" Name: "+speciesName+" Candies"+candies;
}
/**
* Prepare a String representing this entire PokemonSpecies. This is used to
* save the PokemonSpecies (one part of the Pokedex) to a file to
* save the player's game.
*
* @return a String of the form
* [, , ...]
*/
public String toString() {
// TODO
return pokedexNumber+" Name: "+speciesName+" Candies"+candies;
}
}
--------------------------------------------------------------------------------------------------------------------
-------------------------------------
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.InputMismatchException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
/**
* The main class. Provides the main method which is responsible for game play.
* Also provides assistance for gameplay with functions to generate Pokemon, and
* save a player's progress.
*/
public class PokemonGO {
public static final int NEW_POKEMON_CANDIES = 3;
public static final int TRANSFER_POKEMON_CANDIES = 1;
/**
* The game begins here! The set of Pokemon that the player will encounter
* must be provided as the first and only command-line argument which will
* be loaded into the {@link PokemonDB}
*
* Players are prompted to enter their name which will be used to save their
* progress. Players are then prompted with a set of menu options which can
* be selected by entering a single character e.g. 's' for "search". Options
* include search for Pokemon, display encountered Pokemon, display caught Pokemon,
* transfer caught Pokemon, and quit.
*
* @param args the command line arguments. args[0] must be a file which is loaded
* by {@link PokemonDB}
*/
public static void main(String[] args) throws FileNotFoundException {
// Interpret command-line arguments and use them to load PokemonDB
if(args.length != 1) {
System.err.println("Invalid arguments");
System.exit(1);
}
PokemonDB db = new PokemonDB(args[0]);
// Start
System.out.println(Config.WELCOME);
// Prompt user to enter player name
System.out.println(Config.USER_PROMPT);
// Take name of Pokemon trainer
String playerName = Config.getNextLine();
// Provide a name for a txt file which will be used to save the player's progress
String playerFileName = playerName + ".txt";
Pokedex pokedex = new Pokedex();
try {
pokedex.loadFromFile(playerFileName);
} catch (FileNotFoundException e) {
// then the player has not saved any progress yet, start a new game!
}
PokemonTrainer pokemonTrainer = new PokemonTrainer(playerName, pokedex);
System.out.println("Hi " + playerName);
// main menu for the game. accept commands until the player enters 'q' to quit
String option = null;
while ((!"Q".equals(option)) && (!"q".equals(option))) {
// Ask user to choose
// [C] display caught Pokemon
// [D]isplay encountered Pokemon
// [S]earch for Pokemon
// [T]ransfer Pokemon to the Professor
// [Q]uit
System.out.println(Config.MENU_PROMPT);
option = Config.getNextLine();
switch(option) {
case "C":
case "c":
System.out.println(pokemonTrainer.getPokedex().caughtPokemonMenu());
break;
case "D":
case "d":
System.out.println(pokemonTrainer.getPokedex().seenPokemonMenu());
break;
case "S":
case "s":
Pokemon wildPokemon = encounterPokemon(db);
// Provide alternative guessing options
int[] pokedexNumbers = new int[Config.DIFFICULTY];
pokedexNumbers[0] = wildPokemon.getPokedexNumber();
for(int i = 1; i < pokedexNumbers.length; i++) {
pokedexNumbers[i] = db.generatePokedexNumber();
}
Arrays.sort(pokedexNumbers);
// Prompt user for input
System.out.println(String.format(
Config.ENCOUNTERED_POKEMON,
wildPokemon.getSpecies(),
wildPokemon.getCombatPower(),
Arrays.toString(pokedexNumbers)));
int guessedId = 0;
while(guessedId < 1 || guessedId > 151) {
// then prompt is invalid
try {
guessedId = Config.getNextInteger();
if(guessedId < 1 || guessedId > 151) {
throw new RuntimeException(Config.INVALID_INT_INPUT);
}
} catch (InputMismatchException e) {
System.out.println(Config.INVALID_INT_INPUT);
Config.getNextLine();
} catch (RuntimeException e) {
System.out.println(Config.INVALID_INT_INPUT);
}
}
if (guessedId == wildPokemon.getPokedexNumber()) {
// guessed correctly, pokemon is captured
pokemonTrainer.capturePokemon(wildPokemon);
System.out.println(String.format(
Config.SUCCESSFUL_CAPTURE,
playerName,
wildPokemon.getSpecies(),
wildPokemon.getCombatPower()));
}
else {
// guessed incorrectly, pokemon escapes
pokemonTrainer.seePokemon(wildPokemon);
System.out.println(String.format(
Config.FAILED_CAPTURE,
playerName,
wildPokemon.getSpecies()));
}
break;
case "T":
case "t":
// Select Pokemon species to transfer
System.out.println(Config.TRANSFER_PROMPT);
pokedex = pokemonTrainer.getPokedex();
String speciesName = Config.getNextLine();
if(speciesName.toLowerCase().equals("cancel")) {
break;
}
try {
db.lookupPokedexNumber(speciesName);
} catch (PokedexException e) {
System.out.println(e.toString());
break;
}
// Begin transfer of selected species
PokemonSpecies species = null;
try {
species = pokedex.findCaughtSpeciesData(speciesName);
} catch (PokedexException e) {
System.out.println(e.toString());
break;
}
String transferPokemonName = species.getSpeciesName();
// Select Pokemon of that species to transfer
System.out.println(String.format(
Config.TRANSFER_CP_PROMPT,
transferPokemonName,
species.caughtPokemonToString()));
int transferPokemonCp = -1;
while(transferPokemonCp == -1) {
try {
transferPokemonCp = Config.getNextInteger();
if(transferPokemonCp < 0) {
System.out.println(Config.INVALID_CP_INPUT);
transferPokemonCp = -1;
}
} catch (InputMismatchException e) {
System.out.println(Config.INVALID_CP_INPUT);
transferPokemonCp = -1;
Config.getNextLine();
} catch (RuntimeException e) {
System.out.println(Config.INVALID_CP_INPUT);
transferPokemonCp = -1;
}
}
if(transferPokemonCp == 0) {
break;
}
try {
// Call transfer function; should throw exceptions within transfer but are to be caught here
pokemonTrainer.transferPokemon(transferPokemonName, transferPokemonCp);
System.out.println(String.format(
Config.SUCCESSFUL_TRANSFER,
transferPokemonName,
transferPokemonCp));
}
catch (PokedexException pokedexException) {
System.out.println (pokedexException.toString());
}
break;
case "Q":
case "q":
break;
default:
System.out.println(Config.INVALID_RESPONSE);
}
}
// Save the game when the player quits
File outFile = new File(playerFileName);
saveGame(outFile, pokemonTrainer);
System.out.println(String.format(
Config.QUIT_MESSAGE,
playerName));
}
/**
* A wild has appeared! ~*~ battle music plays ~*~
*
* @param db the PokemonDB to generate a Pokemon from
* @return a Pokemon for the encounter
*/
public static Pokemon encounterPokemon(PokemonDB db) {
// random number to pick pokemon
int pokedexNumber = db.generatePokedexNumber();
String species = db.lookupSpeciesName(pokedexNumber);
// random number to decide CP
int cp = Config.CP_GENERATOR.nextInt(Config.MAX_CP-1)+1; // adjustments for origin 0/1
Pokemon wildPokemon = new Pokemon(pokedexNumber, species, cp);
return wildPokemon;
}
/**
* Save the game by writing the Pokedex into a file. The game can be loaded again
* by functions provided by the {@link Pokedex} class
*
* @param outFile the file handle to write the game progress to
* @param pokemonTrainer the player whose game we are saving
*/
public static void saveGame(File outFile, PokemonTrainer pokemonTrainer) throws
FileNotFoundException {
// TODO
outFile.println(pokemonTrainer);
}
}
--------------------------------------------------------------------------------------------------------------------
-----------------------------------------
import java.io.File;
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Iterator;
/**
* This class provides functions for interacting with the set of Pokemon that the player
* will encounter during the course of the game.
*/
public class PokemonDB {
public static final int NUM_POKEMON = 151;
public static final int WILD_CANDIES = 3;
private static ArrayList pokemonSpeciesNames;
/**
* Create a PokemonDB by parsing the Pokemon file in @path@
*
* @param path the file path that contains the Pokemon used for this game
*/
public PokemonDB(String path) throws FileNotFoundException {
pokemonSpeciesNames = new ArrayList();
File infile = new File(path);
loadFile(infile);
}
/**
* Parse Pokemon file with format
* 
* File lines must be ordered from PokedexNumber and start from 1
*
* @param infile the file with all the Pokemon that will be used for this game
*/
public void loadFile(File infile) throws FileNotFoundException {
Scanner sc = new Scanner(infile);
while(sc.hasNextLine()) {
String line = sc.nextLine();
String[] tokens = line.split("s+");
String name = tokens[1];
name = name.toLowerCase(); // to make sure all cases are acceptable
pokemonSpeciesNames.add(name);
}
sc.close();
}
/**
* Generate a Pokedex Number to (1) provide a Pokemon for an encounter and (2)
* provide a set of alternative options for the Pokedex Number guessing game to
* capture the encountered Pokemon
*
* @return the Pokedex Number generated
*/
public int generatePokedexNumber() {
int pokedexNumber =
Config.POKEDEX_NUMBER_GENERATOR.nextInt(PokemonDB.NUM_POKEMON-1)+1; //
adjustments for origin 0/1
return pokedexNumber;
}
/**
* Lookup a Pokemon species name using its Pokedex Number (the index+1 of the
* string in pokemonSpeciesNames which matches the parameter pokedexNumber)
*
* @param pokedexNumber find the Pokemon species name associated with this number
* @throws PokedexException thrown if the pokedexNumber is not in the range 1 to
* NUM_POKEMON (inclusive)
*/
public String lookupSpeciesName(int pokedexNumber) throws PokedexException {
// TODO
for(int i=0;i

More Related Content

Similar to import java.util.ArrayList; import java.util.Iterator; A.pdf

Here are the instructions and then the code in a sec. Please R.pdf
Here are the instructions and then the code in a sec. Please R.pdfHere are the instructions and then the code in a sec. Please R.pdf
Here are the instructions and then the code in a sec. Please R.pdf
aggarwalshoppe14
 
Thanks so much for your help. Review the GameService class. Noti.pdf
Thanks so much for your help. Review the GameService class. Noti.pdfThanks so much for your help. Review the GameService class. Noti.pdf
Thanks so much for your help. Review the GameService class. Noti.pdf
adwitanokiastore
 
CS117-S18-AlharbiMohammed-master.gitignore.class.ctxt..docx
CS117-S18-AlharbiMohammed-master.gitignore.class.ctxt..docxCS117-S18-AlharbiMohammed-master.gitignore.class.ctxt..docx
CS117-S18-AlharbiMohammed-master.gitignore.class.ctxt..docx
annettsparrow
 
C346_PA3_W12srccommonBaseThread.javaC346_PA3_W12srccommonB.docx
C346_PA3_W12srccommonBaseThread.javaC346_PA3_W12srccommonB.docxC346_PA3_W12srccommonBaseThread.javaC346_PA3_W12srccommonB.docx
C346_PA3_W12srccommonBaseThread.javaC346_PA3_W12srccommonB.docx
humphrieskalyn
 
BasicPizza.javapublic class BasicPizza { Declaring instance .pdf
BasicPizza.javapublic class BasicPizza { Declaring instance .pdfBasicPizza.javapublic class BasicPizza { Declaring instance .pdf
BasicPizza.javapublic class BasicPizza { Declaring instance .pdf
ankitmobileshop235
 
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docxNew folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
curwenmichaela
 
Frequency .java Word frequency counter package frequ.pdf
Frequency .java  Word frequency counter  package frequ.pdfFrequency .java  Word frequency counter  package frequ.pdf
Frequency .java Word frequency counter package frequ.pdf
arshiartpalace
 
We will be making 4 classes Main - for testing the code Hi.pdf
 We will be making 4 classes Main - for testing the code Hi.pdf We will be making 4 classes Main - for testing the code Hi.pdf
We will be making 4 classes Main - for testing the code Hi.pdf
anithareadymade
 
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdfWorking with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
udit652068
 
ikh331-06-distributed-programming
ikh331-06-distributed-programmingikh331-06-distributed-programming
ikh331-06-distributed-programming
Anung Ariwibowo
 
Exercícios Netbeans - Vera Cymbron
Exercícios Netbeans - Vera CymbronExercícios Netbeans - Vera Cymbron
Exercícios Netbeans - Vera Cymbron
cymbron
 
Write a class (BasketballTeam) encapsulating the concept of a tea.pdf
Write a class (BasketballTeam) encapsulating the concept of a tea.pdfWrite a class (BasketballTeam) encapsulating the concept of a tea.pdf
Write a class (BasketballTeam) encapsulating the concept of a tea.pdf
rozakashif85
 
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdfPLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
mallik3000
 
package com.tictactoe; public class Main {public void play() {.pdf
package com.tictactoe; public class Main {public void play() {.pdfpackage com.tictactoe; public class Main {public void play() {.pdf
package com.tictactoe; public class Main {public void play() {.pdf
info430661
 
Listing.javaimport java.util.Scanner;public class Listing {   .pdf
Listing.javaimport java.util.Scanner;public class Listing {   .pdfListing.javaimport java.util.Scanner;public class Listing {   .pdf
Listing.javaimport java.util.Scanner;public class Listing {   .pdf
Ankitchhabra28
 

Similar to import java.util.ArrayList; import java.util.Iterator; A.pdf (20)

Here are the instructions and then the code in a sec. Please R.pdf
Here are the instructions and then the code in a sec. Please R.pdfHere are the instructions and then the code in a sec. Please R.pdf
Here are the instructions and then the code in a sec. Please R.pdf
 
Thanks so much for your help. Review the GameService class. Noti.pdf
Thanks so much for your help. Review the GameService class. Noti.pdfThanks so much for your help. Review the GameService class. Noti.pdf
Thanks so much for your help. Review the GameService class. Noti.pdf
 
CS117-S18-AlharbiMohammed-master.gitignore.class.ctxt..docx
CS117-S18-AlharbiMohammed-master.gitignore.class.ctxt..docxCS117-S18-AlharbiMohammed-master.gitignore.class.ctxt..docx
CS117-S18-AlharbiMohammed-master.gitignore.class.ctxt..docx
 
Posfix
PosfixPosfix
Posfix
 
JavaExamples
JavaExamplesJavaExamples
JavaExamples
 
C346_PA3_W12srccommonBaseThread.javaC346_PA3_W12srccommonB.docx
C346_PA3_W12srccommonBaseThread.javaC346_PA3_W12srccommonB.docxC346_PA3_W12srccommonBaseThread.javaC346_PA3_W12srccommonB.docx
C346_PA3_W12srccommonBaseThread.javaC346_PA3_W12srccommonB.docx
 
BasicPizza.javapublic class BasicPizza { Declaring instance .pdf
BasicPizza.javapublic class BasicPizza { Declaring instance .pdfBasicPizza.javapublic class BasicPizza { Declaring instance .pdf
BasicPizza.javapublic class BasicPizza { Declaring instance .pdf
 
Beautiful code
Beautiful codeBeautiful code
Beautiful code
 
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docxNew folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
 
Frequency .java Word frequency counter package frequ.pdf
Frequency .java  Word frequency counter  package frequ.pdfFrequency .java  Word frequency counter  package frequ.pdf
Frequency .java Word frequency counter package frequ.pdf
 
We will be making 4 classes Main - for testing the code Hi.pdf
 We will be making 4 classes Main - for testing the code Hi.pdf We will be making 4 classes Main - for testing the code Hi.pdf
We will be making 4 classes Main - for testing the code Hi.pdf
 
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdfWorking with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
 
ikh331-06-distributed-programming
ikh331-06-distributed-programmingikh331-06-distributed-programming
ikh331-06-distributed-programming
 
Exercícios Netbeans - Vera Cymbron
Exercícios Netbeans - Vera CymbronExercícios Netbeans - Vera Cymbron
Exercícios Netbeans - Vera Cymbron
 
Write a class (BasketballTeam) encapsulating the concept of a tea.pdf
Write a class (BasketballTeam) encapsulating the concept of a tea.pdfWrite a class (BasketballTeam) encapsulating the concept of a tea.pdf
Write a class (BasketballTeam) encapsulating the concept of a tea.pdf
 
Manual tecnic sergi_subirats
Manual tecnic sergi_subiratsManual tecnic sergi_subirats
Manual tecnic sergi_subirats
 
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdfPLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
 
package com.tictactoe; public class Main {public void play() {.pdf
package com.tictactoe; public class Main {public void play() {.pdfpackage com.tictactoe; public class Main {public void play() {.pdf
package com.tictactoe; public class Main {public void play() {.pdf
 
Listing.javaimport java.util.Scanner;public class Listing {   .pdf
Listing.javaimport java.util.Scanner;public class Listing {   .pdfListing.javaimport java.util.Scanner;public class Listing {   .pdf
Listing.javaimport java.util.Scanner;public class Listing {   .pdf
 
Crawler 2
Crawler 2Crawler 2
Crawler 2
 

More from anushafashions

Q5Although this question asks about SN1 reactivity, knowledge of a.pdf
Q5Although this question asks about SN1 reactivity, knowledge of a.pdfQ5Although this question asks about SN1 reactivity, knowledge of a.pdf
Q5Although this question asks about SN1 reactivity, knowledge of a.pdf
anushafashions
 
Please find the answers and explanations belowPart 1The sequen.pdf
Please find the answers and explanations belowPart 1The sequen.pdfPlease find the answers and explanations belowPart 1The sequen.pdf
Please find the answers and explanations belowPart 1The sequen.pdf
anushafashions
 
Manganese Manganese is a pinkinsh-gray, chemically active elemen.pdf
Manganese Manganese is a pinkinsh-gray, chemically active elemen.pdfManganese Manganese is a pinkinsh-gray, chemically active elemen.pdf
Manganese Manganese is a pinkinsh-gray, chemically active elemen.pdf
anushafashions
 
Financial Crisis is a situation which leads to complete turmoil in a.pdf
Financial Crisis is a situation which leads to complete turmoil in a.pdfFinancial Crisis is a situation which leads to complete turmoil in a.pdf
Financial Crisis is a situation which leads to complete turmoil in a.pdf
anushafashions
 
CDKs (Cyclin-dependent Phosphokinases) are enzymes that play an impo.pdf
CDKs (Cyclin-dependent Phosphokinases) are enzymes that play an impo.pdfCDKs (Cyclin-dependent Phosphokinases) are enzymes that play an impo.pdf
CDKs (Cyclin-dependent Phosphokinases) are enzymes that play an impo.pdf
anushafashions
 
a) In terms of software risk,an event is any situation or happening .pdf
a) In terms of software risk,an event is any situation or happening .pdfa) In terms of software risk,an event is any situation or happening .pdf
a) In terms of software risk,an event is any situation or happening .pdf
anushafashions
 
In Niemann-Pick disease, the deficiency of a spec.pdf
                     In Niemann-Pick disease, the deficiency of a spec.pdf                     In Niemann-Pick disease, the deficiency of a spec.pdf
In Niemann-Pick disease, the deficiency of a spec.pdf
anushafashions
 

More from anushafashions (20)

we can predict that benzene(C6H6) will exhibit the highest enthalpy .pdf
we can predict that benzene(C6H6) will exhibit the highest enthalpy .pdfwe can predict that benzene(C6H6) will exhibit the highest enthalpy .pdf
we can predict that benzene(C6H6) will exhibit the highest enthalpy .pdf
 
The modulus of elasticity of a material (E) is given byE = Axial S.pdf
The modulus of elasticity of a material (E) is given byE = Axial S.pdfThe modulus of elasticity of a material (E) is given byE = Axial S.pdf
The modulus of elasticity of a material (E) is given byE = Axial S.pdf
 
Solution.cpp#include iostreamheader file for input output func.pdf
Solution.cpp#include iostreamheader file for input output func.pdfSolution.cpp#include iostreamheader file for input output func.pdf
Solution.cpp#include iostreamheader file for input output func.pdf
 
Insufficient dataSolutionInsufficient data.pdf
Insufficient dataSolutionInsufficient data.pdfInsufficient dataSolutionInsufficient data.pdf
Insufficient dataSolutionInsufficient data.pdf
 
Q5Although this question asks about SN1 reactivity, knowledge of a.pdf
Q5Although this question asks about SN1 reactivity, knowledge of a.pdfQ5Although this question asks about SN1 reactivity, knowledge of a.pdf
Q5Although this question asks about SN1 reactivity, knowledge of a.pdf
 
Portal Triad is an area in the liver named after its triangular shap.pdf
Portal Triad is an area in the liver named after its triangular shap.pdfPortal Triad is an area in the liver named after its triangular shap.pdf
Portal Triad is an area in the liver named after its triangular shap.pdf
 
Please find the answers and explanations belowPart 1The sequen.pdf
Please find the answers and explanations belowPart 1The sequen.pdfPlease find the answers and explanations belowPart 1The sequen.pdf
Please find the answers and explanations belowPart 1The sequen.pdf
 
NRSolutionNR.pdf
NRSolutionNR.pdfNRSolutionNR.pdf
NRSolutionNR.pdf
 
Manganese Manganese is a pinkinsh-gray, chemically active elemen.pdf
Manganese Manganese is a pinkinsh-gray, chemically active elemen.pdfManganese Manganese is a pinkinsh-gray, chemically active elemen.pdf
Manganese Manganese is a pinkinsh-gray, chemically active elemen.pdf
 
Humans first evovled in the African continent. Various sub species a.pdf
Humans first evovled in the African continent. Various sub species a.pdfHumans first evovled in the African continent. Various sub species a.pdf
Humans first evovled in the African continent. Various sub species a.pdf
 
FunctionFunctions in SQL SERVER are reusable. They can accept inpu.pdf
FunctionFunctions in SQL SERVER are reusable. They can accept inpu.pdfFunctionFunctions in SQL SERVER are reusable. They can accept inpu.pdf
FunctionFunctions in SQL SERVER are reusable. They can accept inpu.pdf
 
Financial Crisis is a situation which leads to complete turmoil in a.pdf
Financial Crisis is a situation which leads to complete turmoil in a.pdfFinancial Crisis is a situation which leads to complete turmoil in a.pdf
Financial Crisis is a situation which leads to complete turmoil in a.pdf
 
false (in linear region)Solutionfalse (in linear region).pdf
false (in linear region)Solutionfalse (in linear region).pdffalse (in linear region)Solutionfalse (in linear region).pdf
false (in linear region)Solutionfalse (in linear region).pdf
 
most of hydroxide salts (inorganic salts ) are in.pdf
                     most of hydroxide salts (inorganic salts ) are in.pdf                     most of hydroxide salts (inorganic salts ) are in.pdf
most of hydroxide salts (inorganic salts ) are in.pdf
 
CDKs (Cyclin-dependent Phosphokinases) are enzymes that play an impo.pdf
CDKs (Cyclin-dependent Phosphokinases) are enzymes that play an impo.pdfCDKs (Cyclin-dependent Phosphokinases) are enzymes that play an impo.pdf
CDKs (Cyclin-dependent Phosphokinases) are enzymes that play an impo.pdf
 
Benefit cost ratio = Incremental cash inflows InvestmentIncremen.pdf
Benefit cost ratio = Incremental cash inflows  InvestmentIncremen.pdfBenefit cost ratio = Incremental cash inflows  InvestmentIncremen.pdf
Benefit cost ratio = Incremental cash inflows InvestmentIncremen.pdf
 
Li2S Sol.pdf
                     Li2S                                      Sol.pdf                     Li2S                                      Sol.pdf
Li2S Sol.pdf
 
A. Packets addressed to a unicast address are delivered to a single .pdf
A. Packets addressed to a unicast address are delivered to a single .pdfA. Packets addressed to a unicast address are delivered to a single .pdf
A. Packets addressed to a unicast address are delivered to a single .pdf
 
a) In terms of software risk,an event is any situation or happening .pdf
a) In terms of software risk,an event is any situation or happening .pdfa) In terms of software risk,an event is any situation or happening .pdf
a) In terms of software risk,an event is any situation or happening .pdf
 
In Niemann-Pick disease, the deficiency of a spec.pdf
                     In Niemann-Pick disease, the deficiency of a spec.pdf                     In Niemann-Pick disease, the deficiency of a spec.pdf
In Niemann-Pick disease, the deficiency of a spec.pdf
 

Recently uploaded

Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
中 央社
 

Recently uploaded (20)

How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptx
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptx
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptx
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
An Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppAn Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge App
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDF
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 

import java.util.ArrayList; import java.util.Iterator; A.pdf

  • 1. import java.util.ArrayList; import java.util.Iterator; /** * A PokemonSpecies entry in the Pokedex. Maintains the number of candies associated * with the Pokemon species as well as the Trainer's inventory of Pokemon of this * species. */ public class PokemonSpecies { private int pokedexNumber; private String speciesName; private int candies; /** * Maintains the list of Pokemon of this species in the Trainer's inventory. */ private ArrayList caughtPokemon; /** * Constructor suitable for a newly encountered Pokemon species during the course of the * game and for loading species data from a save file. * * @param pokedexNumber the Pokedex Number for the species * @param speciesName the name of the species * @param candies the number of candies the player has obtained by catching * or transferring Pokemon of this species */ public PokemonSpecies(int pokedexNumber, String speciesName, int candies) { this.pokedexNumber = pokedexNumber; this.speciesName = speciesName; this.candies = candies; // construct caughtPokemon caughtPokemon = new ArrayList(); } /** * Getter methods */
  • 2. public Integer getPokedexNumber() { return pokedexNumber; } public String getSpeciesName() { return speciesName; } public int getCandies() { return candies; } /** * Add a newly caught Pokemon to the player's inventory and * increase the number of candies for this PokemonSpecies * * @param pokemon the newly caught Pokemon */ public void addNewPokemon(Pokemon pokemon) { // TODO caughtPokemon.add(pokemon); this.candies++; } /** * Helper function to load Pokemon from a save file into the player's inventory for this * Pokemon Species * * @param pokemon the pokemon to add to this species */ public void loadPokemon(Pokemon pokemon) { caughtPokemon.add(pokemon); } /** * Find a Pokemon of the given combatPower in the player's inventory for this species. * * @param cp the combatPower of the Pokemon to find * @throws PokedexException [Config.POKEMON_NOT_FOUND] if there is no Pokemon with the * given combatPower in the player's inventory.
  • 3. * @return the first Pokemon with the provided combatPower */ public Pokemon findPokemon(int cp) throws PokedexException { // TODO for(int i=0;i 0){ return true; } return false; } /** * Increment candies when a new pokemon is caught */ private void addNewPokemonCandies() { this.candies += PokemonGO.NEW_POKEMON_CANDIES; } /** * Increment candies when a pokemon is transferred to the professor */ private void addTransferCandies() { this.candies += PokemonGO.TRANSFER_POKEMON_CANDIES; } /** * Prepare a listing of all the Pokemon of this species that are currently in the * player's inventory. * * @return a String of the form * ... */ public String caughtPokemonToString() { // TODO return pokedexNumber+" Name: "+speciesName+" Candies"+candies; } /**
  • 4. * Prepare a String representing this entire PokemonSpecies. This is used to * save the PokemonSpecies (one part of the Pokedex) to a file to * save the player's game. * * @return a String of the form * [, , ...] */ public String toString() { // TODO return pokedexNumber+" Name: "+speciesName+" Candies"+candies; } } -------------------------------------------------------------------------------------------------------------------- ------------------------------------- import java.util.Arrays; import java.util.ArrayList; import java.util.Iterator; import java.util.InputMismatchException; import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; /** * The main class. Provides the main method which is responsible for game play. * Also provides assistance for gameplay with functions to generate Pokemon, and * save a player's progress. */ public class PokemonGO { public static final int NEW_POKEMON_CANDIES = 3; public static final int TRANSFER_POKEMON_CANDIES = 1; /** * The game begins here! The set of Pokemon that the player will encounter * must be provided as the first and only command-line argument which will * be loaded into the {@link PokemonDB} * * Players are prompted to enter their name which will be used to save their * progress. Players are then prompted with a set of menu options which can
  • 5. * be selected by entering a single character e.g. 's' for "search". Options * include search for Pokemon, display encountered Pokemon, display caught Pokemon, * transfer caught Pokemon, and quit. * * @param args the command line arguments. args[0] must be a file which is loaded * by {@link PokemonDB} */ public static void main(String[] args) throws FileNotFoundException { // Interpret command-line arguments and use them to load PokemonDB if(args.length != 1) { System.err.println("Invalid arguments"); System.exit(1); } PokemonDB db = new PokemonDB(args[0]); // Start System.out.println(Config.WELCOME); // Prompt user to enter player name System.out.println(Config.USER_PROMPT); // Take name of Pokemon trainer String playerName = Config.getNextLine(); // Provide a name for a txt file which will be used to save the player's progress String playerFileName = playerName + ".txt"; Pokedex pokedex = new Pokedex(); try { pokedex.loadFromFile(playerFileName); } catch (FileNotFoundException e) { // then the player has not saved any progress yet, start a new game! } PokemonTrainer pokemonTrainer = new PokemonTrainer(playerName, pokedex); System.out.println("Hi " + playerName); // main menu for the game. accept commands until the player enters 'q' to quit String option = null;
  • 6. while ((!"Q".equals(option)) && (!"q".equals(option))) { // Ask user to choose // [C] display caught Pokemon // [D]isplay encountered Pokemon // [S]earch for Pokemon // [T]ransfer Pokemon to the Professor // [Q]uit System.out.println(Config.MENU_PROMPT); option = Config.getNextLine(); switch(option) { case "C": case "c": System.out.println(pokemonTrainer.getPokedex().caughtPokemonMenu()); break; case "D": case "d": System.out.println(pokemonTrainer.getPokedex().seenPokemonMenu()); break; case "S": case "s": Pokemon wildPokemon = encounterPokemon(db); // Provide alternative guessing options int[] pokedexNumbers = new int[Config.DIFFICULTY]; pokedexNumbers[0] = wildPokemon.getPokedexNumber(); for(int i = 1; i < pokedexNumbers.length; i++) { pokedexNumbers[i] = db.generatePokedexNumber(); } Arrays.sort(pokedexNumbers); // Prompt user for input System.out.println(String.format( Config.ENCOUNTERED_POKEMON, wildPokemon.getSpecies(), wildPokemon.getCombatPower(), Arrays.toString(pokedexNumbers)));
  • 7. int guessedId = 0; while(guessedId < 1 || guessedId > 151) { // then prompt is invalid try { guessedId = Config.getNextInteger(); if(guessedId < 1 || guessedId > 151) { throw new RuntimeException(Config.INVALID_INT_INPUT); } } catch (InputMismatchException e) { System.out.println(Config.INVALID_INT_INPUT); Config.getNextLine(); } catch (RuntimeException e) { System.out.println(Config.INVALID_INT_INPUT); } } if (guessedId == wildPokemon.getPokedexNumber()) { // guessed correctly, pokemon is captured pokemonTrainer.capturePokemon(wildPokemon); System.out.println(String.format( Config.SUCCESSFUL_CAPTURE, playerName, wildPokemon.getSpecies(), wildPokemon.getCombatPower())); } else { // guessed incorrectly, pokemon escapes pokemonTrainer.seePokemon(wildPokemon); System.out.println(String.format( Config.FAILED_CAPTURE, playerName, wildPokemon.getSpecies())); } break; case "T":
  • 8. case "t": // Select Pokemon species to transfer System.out.println(Config.TRANSFER_PROMPT); pokedex = pokemonTrainer.getPokedex(); String speciesName = Config.getNextLine(); if(speciesName.toLowerCase().equals("cancel")) { break; } try { db.lookupPokedexNumber(speciesName); } catch (PokedexException e) { System.out.println(e.toString()); break; } // Begin transfer of selected species PokemonSpecies species = null; try { species = pokedex.findCaughtSpeciesData(speciesName); } catch (PokedexException e) { System.out.println(e.toString()); break; } String transferPokemonName = species.getSpeciesName(); // Select Pokemon of that species to transfer System.out.println(String.format( Config.TRANSFER_CP_PROMPT, transferPokemonName, species.caughtPokemonToString())); int transferPokemonCp = -1; while(transferPokemonCp == -1) { try { transferPokemonCp = Config.getNextInteger(); if(transferPokemonCp < 0) { System.out.println(Config.INVALID_CP_INPUT); transferPokemonCp = -1;
  • 9. } } catch (InputMismatchException e) { System.out.println(Config.INVALID_CP_INPUT); transferPokemonCp = -1; Config.getNextLine(); } catch (RuntimeException e) { System.out.println(Config.INVALID_CP_INPUT); transferPokemonCp = -1; } } if(transferPokemonCp == 0) { break; } try { // Call transfer function; should throw exceptions within transfer but are to be caught here pokemonTrainer.transferPokemon(transferPokemonName, transferPokemonCp); System.out.println(String.format( Config.SUCCESSFUL_TRANSFER, transferPokemonName, transferPokemonCp)); } catch (PokedexException pokedexException) { System.out.println (pokedexException.toString()); } break; case "Q": case "q": break; default: System.out.println(Config.INVALID_RESPONSE); } } // Save the game when the player quits File outFile = new File(playerFileName); saveGame(outFile, pokemonTrainer);
  • 10. System.out.println(String.format( Config.QUIT_MESSAGE, playerName)); } /** * A wild has appeared! ~*~ battle music plays ~*~ * * @param db the PokemonDB to generate a Pokemon from * @return a Pokemon for the encounter */ public static Pokemon encounterPokemon(PokemonDB db) { // random number to pick pokemon int pokedexNumber = db.generatePokedexNumber(); String species = db.lookupSpeciesName(pokedexNumber); // random number to decide CP int cp = Config.CP_GENERATOR.nextInt(Config.MAX_CP-1)+1; // adjustments for origin 0/1 Pokemon wildPokemon = new Pokemon(pokedexNumber, species, cp); return wildPokemon; } /** * Save the game by writing the Pokedex into a file. The game can be loaded again * by functions provided by the {@link Pokedex} class * * @param outFile the file handle to write the game progress to * @param pokemonTrainer the player whose game we are saving */ public static void saveGame(File outFile, PokemonTrainer pokemonTrainer) throws FileNotFoundException { // TODO outFile.println(pokemonTrainer); } }
  • 11. -------------------------------------------------------------------------------------------------------------------- ----------------------------------------- import java.io.File; import java.util.Scanner; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Iterator; /** * This class provides functions for interacting with the set of Pokemon that the player * will encounter during the course of the game. */ public class PokemonDB { public static final int NUM_POKEMON = 151; public static final int WILD_CANDIES = 3; private static ArrayList pokemonSpeciesNames; /** * Create a PokemonDB by parsing the Pokemon file in @path@ * * @param path the file path that contains the Pokemon used for this game */ public PokemonDB(String path) throws FileNotFoundException { pokemonSpeciesNames = new ArrayList(); File infile = new File(path); loadFile(infile); } /** * Parse Pokemon file with format * * File lines must be ordered from PokedexNumber and start from 1 * * @param infile the file with all the Pokemon that will be used for this game */ public void loadFile(File infile) throws FileNotFoundException { Scanner sc = new Scanner(infile); while(sc.hasNextLine()) {
  • 12. String line = sc.nextLine(); String[] tokens = line.split("s+"); String name = tokens[1]; name = name.toLowerCase(); // to make sure all cases are acceptable pokemonSpeciesNames.add(name); } sc.close(); } /** * Generate a Pokedex Number to (1) provide a Pokemon for an encounter and (2) * provide a set of alternative options for the Pokedex Number guessing game to * capture the encountered Pokemon * * @return the Pokedex Number generated */ public int generatePokedexNumber() { int pokedexNumber = Config.POKEDEX_NUMBER_GENERATOR.nextInt(PokemonDB.NUM_POKEMON-1)+1; // adjustments for origin 0/1 return pokedexNumber; } /** * Lookup a Pokemon species name using its Pokedex Number (the index+1 of the * string in pokemonSpeciesNames which matches the parameter pokedexNumber) * * @param pokedexNumber find the Pokemon species name associated with this number * @throws PokedexException thrown if the pokedexNumber is not in the range 1 to * NUM_POKEMON (inclusive) */ public String lookupSpeciesName(int pokedexNumber) throws PokedexException { // TODO for(int i=0;i Solution import java.util.ArrayList; import java.util.Iterator;
  • 13. /** * A PokemonSpecies entry in the Pokedex. Maintains the number of candies associated * with the Pokemon species as well as the Trainer's inventory of Pokemon of this * species. */ public class PokemonSpecies { private int pokedexNumber; private String speciesName; private int candies; /** * Maintains the list of Pokemon of this species in the Trainer's inventory. */ private ArrayList caughtPokemon; /** * Constructor suitable for a newly encountered Pokemon species during the course of the * game and for loading species data from a save file. * * @param pokedexNumber the Pokedex Number for the species * @param speciesName the name of the species * @param candies the number of candies the player has obtained by catching * or transferring Pokemon of this species */ public PokemonSpecies(int pokedexNumber, String speciesName, int candies) { this.pokedexNumber = pokedexNumber; this.speciesName = speciesName; this.candies = candies; // construct caughtPokemon caughtPokemon = new ArrayList(); } /** * Getter methods */ public Integer getPokedexNumber() { return pokedexNumber; }
  • 14. public String getSpeciesName() { return speciesName; } public int getCandies() { return candies; } /** * Add a newly caught Pokemon to the player's inventory and * increase the number of candies for this PokemonSpecies * * @param pokemon the newly caught Pokemon */ public void addNewPokemon(Pokemon pokemon) { // TODO caughtPokemon.add(pokemon); this.candies++; } /** * Helper function to load Pokemon from a save file into the player's inventory for this * Pokemon Species * * @param pokemon the pokemon to add to this species */ public void loadPokemon(Pokemon pokemon) { caughtPokemon.add(pokemon); } /** * Find a Pokemon of the given combatPower in the player's inventory for this species. * * @param cp the combatPower of the Pokemon to find * @throws PokedexException [Config.POKEMON_NOT_FOUND] if there is no Pokemon with the * given combatPower in the player's inventory. * @return the first Pokemon with the provided combatPower */ public Pokemon findPokemon(int cp) throws PokedexException {
  • 15. // TODO for(int i=0;i 0){ return true; } return false; } /** * Increment candies when a new pokemon is caught */ private void addNewPokemonCandies() { this.candies += PokemonGO.NEW_POKEMON_CANDIES; } /** * Increment candies when a pokemon is transferred to the professor */ private void addTransferCandies() { this.candies += PokemonGO.TRANSFER_POKEMON_CANDIES; } /** * Prepare a listing of all the Pokemon of this species that are currently in the * player's inventory. * * @return a String of the form * ... */ public String caughtPokemonToString() { // TODO return pokedexNumber+" Name: "+speciesName+" Candies"+candies; } /** * Prepare a String representing this entire PokemonSpecies. This is used to * save the PokemonSpecies (one part of the Pokedex) to a file to * save the player's game.
  • 16. * * @return a String of the form * [, , ...] */ public String toString() { // TODO return pokedexNumber+" Name: "+speciesName+" Candies"+candies; } } -------------------------------------------------------------------------------------------------------------------- ------------------------------------- import java.util.Arrays; import java.util.ArrayList; import java.util.Iterator; import java.util.InputMismatchException; import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; /** * The main class. Provides the main method which is responsible for game play. * Also provides assistance for gameplay with functions to generate Pokemon, and * save a player's progress. */ public class PokemonGO { public static final int NEW_POKEMON_CANDIES = 3; public static final int TRANSFER_POKEMON_CANDIES = 1; /** * The game begins here! The set of Pokemon that the player will encounter * must be provided as the first and only command-line argument which will * be loaded into the {@link PokemonDB} * * Players are prompted to enter their name which will be used to save their * progress. Players are then prompted with a set of menu options which can * be selected by entering a single character e.g. 's' for "search". Options * include search for Pokemon, display encountered Pokemon, display caught Pokemon, * transfer caught Pokemon, and quit.
  • 17. * * @param args the command line arguments. args[0] must be a file which is loaded * by {@link PokemonDB} */ public static void main(String[] args) throws FileNotFoundException { // Interpret command-line arguments and use them to load PokemonDB if(args.length != 1) { System.err.println("Invalid arguments"); System.exit(1); } PokemonDB db = new PokemonDB(args[0]); // Start System.out.println(Config.WELCOME); // Prompt user to enter player name System.out.println(Config.USER_PROMPT); // Take name of Pokemon trainer String playerName = Config.getNextLine(); // Provide a name for a txt file which will be used to save the player's progress String playerFileName = playerName + ".txt"; Pokedex pokedex = new Pokedex(); try { pokedex.loadFromFile(playerFileName); } catch (FileNotFoundException e) { // then the player has not saved any progress yet, start a new game! } PokemonTrainer pokemonTrainer = new PokemonTrainer(playerName, pokedex); System.out.println("Hi " + playerName); // main menu for the game. accept commands until the player enters 'q' to quit String option = null; while ((!"Q".equals(option)) && (!"q".equals(option))) { // Ask user to choose // [C] display caught Pokemon
  • 18. // [D]isplay encountered Pokemon // [S]earch for Pokemon // [T]ransfer Pokemon to the Professor // [Q]uit System.out.println(Config.MENU_PROMPT); option = Config.getNextLine(); switch(option) { case "C": case "c": System.out.println(pokemonTrainer.getPokedex().caughtPokemonMenu()); break; case "D": case "d": System.out.println(pokemonTrainer.getPokedex().seenPokemonMenu()); break; case "S": case "s": Pokemon wildPokemon = encounterPokemon(db); // Provide alternative guessing options int[] pokedexNumbers = new int[Config.DIFFICULTY]; pokedexNumbers[0] = wildPokemon.getPokedexNumber(); for(int i = 1; i < pokedexNumbers.length; i++) { pokedexNumbers[i] = db.generatePokedexNumber(); } Arrays.sort(pokedexNumbers); // Prompt user for input System.out.println(String.format( Config.ENCOUNTERED_POKEMON, wildPokemon.getSpecies(), wildPokemon.getCombatPower(), Arrays.toString(pokedexNumbers))); int guessedId = 0; while(guessedId < 1 || guessedId > 151) { // then prompt is invalid
  • 19. try { guessedId = Config.getNextInteger(); if(guessedId < 1 || guessedId > 151) { throw new RuntimeException(Config.INVALID_INT_INPUT); } } catch (InputMismatchException e) { System.out.println(Config.INVALID_INT_INPUT); Config.getNextLine(); } catch (RuntimeException e) { System.out.println(Config.INVALID_INT_INPUT); } } if (guessedId == wildPokemon.getPokedexNumber()) { // guessed correctly, pokemon is captured pokemonTrainer.capturePokemon(wildPokemon); System.out.println(String.format( Config.SUCCESSFUL_CAPTURE, playerName, wildPokemon.getSpecies(), wildPokemon.getCombatPower())); } else { // guessed incorrectly, pokemon escapes pokemonTrainer.seePokemon(wildPokemon); System.out.println(String.format( Config.FAILED_CAPTURE, playerName, wildPokemon.getSpecies())); } break; case "T": case "t": // Select Pokemon species to transfer System.out.println(Config.TRANSFER_PROMPT);
  • 20. pokedex = pokemonTrainer.getPokedex(); String speciesName = Config.getNextLine(); if(speciesName.toLowerCase().equals("cancel")) { break; } try { db.lookupPokedexNumber(speciesName); } catch (PokedexException e) { System.out.println(e.toString()); break; } // Begin transfer of selected species PokemonSpecies species = null; try { species = pokedex.findCaughtSpeciesData(speciesName); } catch (PokedexException e) { System.out.println(e.toString()); break; } String transferPokemonName = species.getSpeciesName(); // Select Pokemon of that species to transfer System.out.println(String.format( Config.TRANSFER_CP_PROMPT, transferPokemonName, species.caughtPokemonToString())); int transferPokemonCp = -1; while(transferPokemonCp == -1) { try { transferPokemonCp = Config.getNextInteger(); if(transferPokemonCp < 0) { System.out.println(Config.INVALID_CP_INPUT); transferPokemonCp = -1; } } catch (InputMismatchException e) { System.out.println(Config.INVALID_CP_INPUT);
  • 21. transferPokemonCp = -1; Config.getNextLine(); } catch (RuntimeException e) { System.out.println(Config.INVALID_CP_INPUT); transferPokemonCp = -1; } } if(transferPokemonCp == 0) { break; } try { // Call transfer function; should throw exceptions within transfer but are to be caught here pokemonTrainer.transferPokemon(transferPokemonName, transferPokemonCp); System.out.println(String.format( Config.SUCCESSFUL_TRANSFER, transferPokemonName, transferPokemonCp)); } catch (PokedexException pokedexException) { System.out.println (pokedexException.toString()); } break; case "Q": case "q": break; default: System.out.println(Config.INVALID_RESPONSE); } } // Save the game when the player quits File outFile = new File(playerFileName); saveGame(outFile, pokemonTrainer); System.out.println(String.format( Config.QUIT_MESSAGE, playerName));
  • 22. } /** * A wild has appeared! ~*~ battle music plays ~*~ * * @param db the PokemonDB to generate a Pokemon from * @return a Pokemon for the encounter */ public static Pokemon encounterPokemon(PokemonDB db) { // random number to pick pokemon int pokedexNumber = db.generatePokedexNumber(); String species = db.lookupSpeciesName(pokedexNumber); // random number to decide CP int cp = Config.CP_GENERATOR.nextInt(Config.MAX_CP-1)+1; // adjustments for origin 0/1 Pokemon wildPokemon = new Pokemon(pokedexNumber, species, cp); return wildPokemon; } /** * Save the game by writing the Pokedex into a file. The game can be loaded again * by functions provided by the {@link Pokedex} class * * @param outFile the file handle to write the game progress to * @param pokemonTrainer the player whose game we are saving */ public static void saveGame(File outFile, PokemonTrainer pokemonTrainer) throws FileNotFoundException { // TODO outFile.println(pokemonTrainer); } } -------------------------------------------------------------------------------------------------------------------- ----------------------------------------- import java.io.File;
  • 23. import java.util.Scanner; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Iterator; /** * This class provides functions for interacting with the set of Pokemon that the player * will encounter during the course of the game. */ public class PokemonDB { public static final int NUM_POKEMON = 151; public static final int WILD_CANDIES = 3; private static ArrayList pokemonSpeciesNames; /** * Create a PokemonDB by parsing the Pokemon file in @path@ * * @param path the file path that contains the Pokemon used for this game */ public PokemonDB(String path) throws FileNotFoundException { pokemonSpeciesNames = new ArrayList(); File infile = new File(path); loadFile(infile); } /** * Parse Pokemon file with format * * File lines must be ordered from PokedexNumber and start from 1 * * @param infile the file with all the Pokemon that will be used for this game */ public void loadFile(File infile) throws FileNotFoundException { Scanner sc = new Scanner(infile); while(sc.hasNextLine()) { String line = sc.nextLine(); String[] tokens = line.split("s+"); String name = tokens[1];
  • 24. name = name.toLowerCase(); // to make sure all cases are acceptable pokemonSpeciesNames.add(name); } sc.close(); } /** * Generate a Pokedex Number to (1) provide a Pokemon for an encounter and (2) * provide a set of alternative options for the Pokedex Number guessing game to * capture the encountered Pokemon * * @return the Pokedex Number generated */ public int generatePokedexNumber() { int pokedexNumber = Config.POKEDEX_NUMBER_GENERATOR.nextInt(PokemonDB.NUM_POKEMON-1)+1; // adjustments for origin 0/1 return pokedexNumber; } /** * Lookup a Pokemon species name using its Pokedex Number (the index+1 of the * string in pokemonSpeciesNames which matches the parameter pokedexNumber) * * @param pokedexNumber find the Pokemon species name associated with this number * @throws PokedexException thrown if the pokedexNumber is not in the range 1 to * NUM_POKEMON (inclusive) */ public String lookupSpeciesName(int pokedexNumber) throws PokedexException { // TODO for(int i=0;i