Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Change the Pokemon class so that it can store images for Pokemon. First, add an instance variable to the Pokemon class to store an image.

Change the Pokemon class so that it can store images for Pokemon.

First, add an instance variable to the Pokemon class to store an image.

Then, add a new Constructor, still keeping the old one. This will allow Pokemon to be constructed in multiple ways, one way is the Constructor already written, one way is this new Constructor:

public Pokemon(String name, String image)

This new Constructor should do everything the old one did, but also add an image for the Pokemon.

Then add the following methods to the Pokemon class:

// Sets this Pokemon's image to be `image` public void setImage(String image) // Returns the ASCII Art image for this Pokemon public String getImage()

Finally, modify the Pokemon toString() method so that it includes the Pokemon's image, if it has one.

For example, the following code:

PokemonImages images = new PokemonImages(); Pokemon pikachu = new Pokemon("Pikachu", images.getPokemonImage("Pikachu")); System.out.println(pikachu); System.out.println("==========================="); Pokemon riachu = new Pokemon("Riachu"); System.out.println(riachu);

Should print out

 @&&&( (@&&& @@&..*& (&../&&& %&.....&. &.....#& &......#. ,&@&&&@%, %......& *&................,...,&/ ./...............&, /@/..& &..,*........#&...( /@*........% &.%&%&......(@@&..&%#..............% (........*......,.(...,.............& #(((/..,%###(..*((((*.../**,,.........& @(#....(///.....(......**********(%&@& &......*....,.......@, (.....,..,,./......**(. #.................&***(. .,....(........,(*******/, .,....*...........**% &/(./..../...........###& &*,../..,/..../.(,(...%&. #**...,,,,....(**..../ /**************/*,.& &%,**,...#@#*(*/&* Pikachu (Health: 100 / 100) =========================== Riachu (Health: 100 / 100)

Because the Pokemon pikachu has an image, and riachu does not.

pokemonImages.txt

$$ This file holds ASCII Images for a couple Pokemon $$ $$ Image courtesy of http://pokemon.wikia.com/wiki/Bulbasaur $$ Converted to ASCII Art with http://manytools.org/hacker-tools/convert-images-to-ascii-art/ ##Bulbasaur ((( .(((/(((((((((((((( ((((( ((((((((((((((( (( /(((((( ((((((((((((((( (( ((((((((((((((/### .(( ./((/. (#### .((((((((( (((((*################# *#### ((((((((((((*((( (##############.,,, ##### ( (((((((((((//((((##############*,, ####### (/(((((((((((((( .((######### ##, ##### ### # /(,(((((((((((((( ((#### @,@ #########,, ##@,@# //((((((((((((( ## ((### @ ,@, #######( ,,#/@, /// ((((((((( ,,.#######@@ ,@,, ############,,, * ///,*//// ,,,,, #######@@ ,,,,.############ (### ,//// # ,,,,.################################ ##################. (#############. ## ######### ####### #################### # ,. *## #########((( ########. .#### #.,,,,(###### .,####*(((((((((((((((###### ###( (### ##,,,,#####(((((((((((((((#### .# (######## (##,,.####(((((((((((( (((###.,,# ((######*(### #### (((((( ,((####/ # *(((((((( (#######*((####, .((######% &@,& /@ ( #/# , ((,@# $$ Image courtesy of http://pokemon.wikia.com/wiki/Charmander $$ Converted to ASCII Art using http://manytools.org/hacker-tools/convert-images-to-ascii-art/ ##Charmander ,%%(((((,. ,#(((((((((((. ,/((((((((/##.(((((* //(((((((((...../((((( .,,*.(((((**..*#((((((( .,,,,,.(((((((((((((( # %.,,,./,(((((((((((* && , ..###/*((((((((((% &&&&&( ,((((%.,,##/.(((((((((((/ *,. %&&/#( .(((((///(((((///((((**%%#(((((, &&&&//// /((((((((*(/(((((((((((((((((, (&////// .(((((/&&&&%/(((((((((((((. ##*(*/& .((,&&&&&&&,(((((((//, (, *&&&&&&&&&/((((//(/ (, %&&&&&&&&&*(((((((( *(* ,&&&&&&&&&((((((((. ,((( *&&&&&&&&&&(((((((((, ,((((% .%%((,&&&&&&&&&&,((((((((* .,(((((%, .%(((((*&&&&&&&&,(((((((((*/((((((,%* /(*,, ,(((////*&&&&((((((((((/,//((*(%* #*((((((///////(%%%/((((((((((/,*%%%%. */////////,. /////(((((///*%%(. .///// /////////% .. ./////, /,,/,/,, %,%#,,. $$ Image courtesy of http://pokemon.wikia.com/wiki/Squirtle $$ Converted to ASCII Art using http://manytools.org/hacker-tools/convert-images-to-ascii-art/ ##Squirtle @@(*******@@ @@**************@ @******/ &*********@ @******% %%/*******@*@ @ @******@%@%@*********, @**&@ @******/&%@@********@% &***@*@*@&****/*************@@@ @//*@*****/**********@********@ @@*****%@ @///******@*****************@ @**********& @(@///*******%*************@ @************@ @(((&////****@,,,*/@@((&@/***@ //************@#((((&////**@,,,,,,,,,,,@********@ ///*****@*****##(((( (//,&,,,,@,,,,,,,,********* @///////@****@#@((( (,,,,,,,,,,,,,,,,,(/////***@ @@//#@******@##@# @,,,,,,,,,,,,,,@,,,@ @/////*****@##@,*/,,,,,,@@,,,,,,#,,,, @/////****##/****@,,,,,,,#@,,,,,,,@ @#/////*@*******%,,,,,,,,,,,,,@**@ @@(%*********@*******/**@*****@ %********/@******@@/*******@ ********/@ @////*****@@ @*********/ @////////@ /*****% %//@@ $$ Image courtesy of http://pocket-monstersbr.tumblr.com/ $$ Converted to ASCII Art using http://manytools.org/hacker-tools/convert-images-to-ascii-art/ ##Pikachu @&&&( (@&&& @@&..*& (&../&&& %&.....&. &.....#& &......#. ,&@&&&@%, %......& *&................,...,&/ ./...............&, /@/..& &..,*........#&...( /@*........% &.%&%&......(@@&..&%#..............% (........*......,.(...,.............& #(((/..,%###(..*((((*.../**,,.........& @(#....(///.....(......**********(%&@& &......*....,.......@, (.....,..,,./......**(. #.................&***(. .,....(........,(*******/, .,....*...........**% &/(./..../...........###& &*,../..,/..../.(,(...%&. #**...,,,,....(**..../ /**************/*,.& &..#@#*(*/&*

PokemonTester.java

public class PokemonTester { PokemonImages images = new PokemonImages(); public void run() { // Using the PokemonImages class: // Get an image by String name String pikachuImage = images.getPokemonImage("Pikachu"); System.out.println("Pikachu: " + pikachuImage); // Get an image by Pokemon Pokemon charmander = new Pokemon("Charmander"); String charmanderImage = images.getPokemonImage(charmander); System.out.println("Charmander: " + charmanderImage); // Some Pokemon don't have images, `nothingImage` will be null String nothingImage = images.getPokemonImage("nothing"); System.out.println("Nothing: " + nothingImage); // Test out the Pokemon class here! } }

PokemonImages.java DO NOT MODIFY THIS CODE DO NOT MODIFY THIS CODE

import java.util.*; import java.io.*; // This is the PokemonImages class // It acts as a dictionary to get ASCII art for a given pokemon name // Feel free to explore this code, it uses FileReader and BufferedReader // to read in Pokemon images from the "pokemonImages.txt" file. // DO NOT CHANGE THIS CODE, the program may not work if you do. public class PokemonImages { private HashMap pokedex; private static final String IMAGE_FILE = "pokemonImages.txt"; public PokemonImages() { pokedex = new HashMap(); loadImages(IMAGE_FILE); } public String getPokemonImage(String name) { return pokedex.get(name.toLowerCase()); } public String getPokemonImage(Pokemon pokemon) { String name = pokemon.getName().toLowerCase(); return pokedex.get(name); } public void setPokemonImage(String pokemonName, String image) { if(pokemonName != null && image != null) { pokedex.put(pokemonName, image); } } private void loadImages(String filename) { // Try to do the following code, but there may be errors // when reading the file. try { // Create BufferedReader to read from the file where the // ASCII Art is stored BufferedReader input = new BufferedReader(new FileReader(filename)); String line = input.readLine(); String currentImage = ""; String currentPokemon = null; // Read each line of the file and add each pokemon name and image // to the HashMap while(line != null) { // Comment line, we can skip it if(line.startsWith("$$")) { line = input.readLine(); continue; } // The "##" indicates a new pokemon name // Store the old pokemon if there was one and start a new pokemon else if(line.startsWith("##")) { if(currentPokemon != null) { pokedex.put(currentPokemon, currentImage); } // Get the name of the new pokemon currentPokemon = line.substring(2).toLowerCase(); currentImage = ""; } else { currentImage += line + " "; } line = input.readLine(); } // Fencepost situation // The last pokemon has not been added yet, add it to the HashMap if(currentPokemon != null) { pokedex.put(currentPokemon, currentImage); } input.close(); } // If there were any errors when reading the file // they will be handled by these `catch` clauses catch (FileNotFoundException e) { System.out.println("Couldn't open file: " + filename); } catch (IOException e) { System.out.println("There was an error while reading the file: " + filename); e.printStackTrace(); } } }

Pokemon.java ONLY MODIFY ACCORDING TO DIRECTION

public class Pokemon { /** * instance variables */ int moveCount; String pokemonName; int health; String arr[] = new String[4]; Pokemon(String name) { this.pokemonName = name; this.health = 100; } void addMoves(Move other) { arr[moveCount]=other.moveName; moveCount++; System.out.println("Move Added to collection"); } /** * Returns the name of the Pokemon */ public String getName() { return this.pokemonName; } /** * Returns how much health this Pokemon has */ public int getHealth() { return this.health; } /* * Returns true if this Pokemon has fainted, * False otherwise. A Pokemon faints when their health is * less than or equal to 0 */ public boolean hasFainted() { if(this.health <= 0) { return true; } else return false; } /* * A Pokemon can only know 4 Moves. This method * returns true if this Pokemon can still learn more * Moves, returns false otherwise. */ public boolean canLearnMoreMoves() { if(this.moveCount < 4) { return true; } else return false; } /* * Adds the Move `move` to the collection of Moves * that this Pokemon knows. Returns true if the Move was * successfully added. Returns false if the Pokemon already * knew 4 Moves and couldn't learn a new one. */ public boolean learnMove(Move move) { if(this.moveCount < 4) { addMoves(move); return true; } else return false; } /* * Remove the Move `move` from this Pokemon's * collection of Moves, if it's there. */ public void forgetMove(Move move) { for(int i = 0; i < moveCount; i++) { if(arr[i].equalsIgnoreCase(move.moveName)) { arr[i]=""; System.out.println("Move removed "); moveCount--; break; } } } /* * Return a String containing the name and health * of this Pokemon * Example: "Pikachu (Health: 85 / 100)" */ public String toString() { return this.pokemonName + "(Health:" + this.health + "/100)"; } }

Move.java DO NOT MODIFY THIS CODE DO NOT MODIFY THIS CODE

public class Move { /** * Private constants to represent the max damage, the name, and the amount of damage */ private static final int MAX_DAMAGE = 25; private String name; private int damage; public Move(String name, int damage) { this.name = name; this.damage = Math.min(damage, MAX_DAMAGE); } /** * This method returns the name of the move * @return string name */ public String getName() { return name; } /** * This method returns the damage of the move * @return int damage */ public int getDamage() { return damage; } /** * This method returns a String of the move * @return String name of move, + damage dealt. */ public String toString() { return getName() + " (" + getDamage() + " Damage)"; } String moveName; Move(String moven) { this.moveName = moven; } public boolean equals(Move other) { if(this == other) { return true; } if(other == null || getClass() != other.getClass()) { return false; } Move n = (Move) other; return moveName.equals(n.moveName); } }

After changing the Pokemon class, give them the ability to use moves on other pokemon.

Add the following methods to the Pokemon class:

// Returns an ArrayList of all the Moves this Pokemon knows public ArrayList getMoves() // Returns true if the Pokemon knows this move (has this Move // in its collection), false otherwise public boolean knowsMove(Move move) // Returns true if the Pokemon knows a Move with // the name `moveName`, false otherwise public boolean knowsMove(String moveName) /* * Tries to perform the Move `move` on the Pokemon `other` * A Pokemon can only use a Move if it knows the Move * An attack should subtract the damage of the Move * from the health of the opponent. * Returns true if the Pokemon knew the Move and used it * Returns false otherwise. */ public boolean attack(Pokemon opponent, Move move) /* * Tries to perform a Move with the name `moveName` on * the Pokemon `other` * A Pokemon can only use a Move if it knows the Move * An attack should subtract the damage of the Move * from the health of the opponent. * Returns true if the Pokemon knew the Move and used it * Returns false otherwise. */ public boolean attack(Pokemon opponent, String moveName)

Test out these new methods in the PokemonTester class.

Lastly, make the PokemonTrainer class to represent a player in the battle. A PokemonTrainer has their own Pokemon and commands them to use certain Moves during the battle. A PokemonTrainter has lost when all of their Pokemon have fainted.

A PokemonTrainer has a name as well as a collection of Pokemon. A PokemonTrainer cannot have more than MAX_POKEMON Pokemon. A PokemonTrainer has zero Pokemon when it is first created.

Decide what instance variables the PokemonTrainer class should have, write Constructor for the class, and write the following methods:

/* * Adds Pokemon p to the PokemonTrainer's collection of Pokemon. * A Player is only allowed MAX_POKEMON Pokemon, so this method * will return true if there was room for the new Pokemon and * it was successfully added, false if there was no room for the * new Pokemon. */ public boolean addPokemon(Pokemon p) /* * Returns true if all of the PokemonTrainer's Pokemon * have fainted, false otherwise. */ public boolean hasLost() /* * Returns the first Pokemon that has not yet fainted * from this PokemonTrainer's collection of Pokemon. * If every Pokemon has fainted, this method returns null. */ public Pokemon getNextPokemon() // Returns this PokemonTrainer's name public String toString()

PokemonTrainer.java

public class PokemonTrainer { // private constants private static final int MAX_POKEMON = 2; // Write the PokemonTrainer class here }

PokemonTrainerTester.java

public class PokemonTrainerTester { PokemonImages images = new PokemonImages(); public void run() { // Test out your PokemonTrainer class here! } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

Heres the updated Pokemon class with image storage capabilities Java public cla... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Smith and Roberson Business Law

Authors: Richard A. Mann, Barry S. Roberts

15th Edition

1285141903, 1285141903, 9781285141909, 978-0538473637

More Books

Students also viewed these Programming questions