Let's take all the common functionalities and place those in a base class. We will call this base class Character. A. Op

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Let's take all the common functionalities and place those in a base class. We will call this base class Character. A. Op

Post by answerhappygod »

Let S Take All The Common Functionalities And Place Those In A Base Class We Will Call This Base Class Character A Op 1
Let S Take All The Common Functionalities And Place Those In A Base Class We Will Call This Base Class Character A Op 1 (106.59 KiB) Viewed 47 times
Let S Take All The Common Functionalities And Place Those In A Base Class We Will Call This Base Class Character A Op 2
Let S Take All The Common Functionalities And Place Those In A Base Class We Will Call This Base Class Character A Op 2 (61.91 KiB) Viewed 47 times
Let S Take All The Common Functionalities And Place Those In A Base Class We Will Call This Base Class Character A Op 3
Let S Take All The Common Functionalities And Place Those In A Base Class We Will Call This Base Class Character A Op 3 (58.06 KiB) Viewed 47 times
Let S Take All The Common Functionalities And Place Those In A Base Class We Will Call This Base Class Character A Op 4
Let S Take All The Common Functionalities And Place Those In A Base Class We Will Call This Base Class Character A Op 4 (54.23 KiB) Viewed 47 times
Let S Take All The Common Functionalities And Place Those In A Base Class We Will Call This Base Class Character A Op 5
Let S Take All The Common Functionalities And Place Those In A Base Class We Will Call This Base Class Character A Op 5 (70.86 KiB) Viewed 47 times
Please complete all three classes as this is oneassignment and show code running. Use code and setters andgetters provided in code as well. Follow instructions clearly.
Let's take all the common functionalities and place those in a base class. We will call this base class Character. A. Open the Lab 5 Project B. Right-click on sre > New > Package. Set the name to lab5. C. Right-click on lab5 > New > Class. Set the name to Character. D. Add the common fields of PlayableChacter and Enemy as our class fields for the Character.java class. Remember to use the "private" access modifier as we are continuing to practice encapsulation. These fields are chosen to represent a bare-minimum character, but this list of variables can be expanded in the future. //class fields private String name; private int currentHitPoints = 0; private int weaponDamage = 0; private int locationx; private int locationY; E. Let's let Eclipse generate getters and setters for us. Remember that it is under Source > Generate Getters and Setters. F. Now, we will add the common methods of the PlayableCharacter and Enemy classes to our base class. We will add methods to be able to do the following: A. Receive damage, lowering our character's current hitpoints by the damage amount B. Attack, causing the opponent to take damage equal to our weapon damage. C. Override the toString method, so that we can show the current status of the character. We will use String.format here to create a formatted String. Remember that the arguments after the String in String.format will be used to replace the Format Specifiers (ie. %s or %d etc.). For something like String.format("Hello %s", "world"), the %s will be replaced with "world". We are overriding the toString method since it exists in the parent class Object. All classes inherit from the Object class. D. Override the equals method, so that we can determine if one character is "equivalent" to another character based on their names. It will take an Object as a parameter and we will cast it to a Character object so we can use its getName() method. We are overriding the equals method since it exists in the parent class Object. All classes inherit from the Object class. G. To accomplish our previous point, add the following code to your class: //Lower our current hitpoints by the damageTaken argument public void receiveDamage(int damageTaken) { this.currentHitPoints damageTaken; } //Object interaction. The parameter is an Character object //Check if this character is in the same location as the other character, if so then do weapon damage to them //We call the opponent's receiveDamage method within this attack method public void attack (Character other) { } if (other.getLocationX() == this.getLocationX() && other.getLocationY() ==this.getLocationY() && other.getCurrentHitPoints() > 0) { System.out.printf("%s attacked %s\n", this.getName(), other.getName()); other.receiveDamage (this.weaponDamage); } if (other.getCurrentHitPoints() <= 0) {//if true, then the other character is defeated System.out.printf("%s defeated %s\n", this.getName(), other.getName()); //Move them off the board public String toString() { return String.format("%s location: %d, %d. HitPoints: %d", this.getName(), this.getLocationX(), this.getLocationY(), this.getCurrentHitPoints()); other.setLocationX(-999); other.setLocationY(-999); public boolean equals(Object other) { //Cast the Object parameter to a Character so we can access its name Character tempCast = (Character) other; if (this.name.equals(tempCast.getName())){ return true; } else { return false;
H. Finally, let's create our class constructor. It should do the following: A. Take a String that represents the character's name and assign the value to the name variable. I. With all of the previous step, we can create our constructor with the following code: /** * * @param name @param locationx @param locationY */ public Character (String name, int locationx, int locationY) { this.name = name; this.locationX = locationX; this.locationY = locationY; } J. We have finished the base (parent/superclass) Character class! name: String currentHitPoints: int=0 weaponDamage: int=0 locationX: int locationY: int Character Character (name: String, locationX: int, locationY: int ): Character receiveDamage (damage Taken: int ) setLocationX (locationX: int ) setLocationY (locationY: int ) toString(): String attack (other: Character) equals (other: Object): boolean K. L. Make sure there are no syntax errors (red underlines) and make sure to save the file. M. Commit and push your code to the GitHub repo.
Let's extend the base class and add the specialized functionalities for the PlayableChacter and Enemy classes. The user playing the game will make decisions for the PlayableChacter and the randomly generated number will be used to make decisions for the Enemy. A. Open the Lab 5 Project B. Right-click on src. C. Right-click on lab5 > New > Class. Set the name to Enemy. D. We want to reuse the code already implemented in the base class, so let's make sure we use inheritance here. Edit the class definition to look like the following. public class Enemy extends Character A. Add the fields of Enemy as our class fields for the Enemy.java class. Remember to use the "private" access modifier as we are continuing to practice encapsulation. These fields are chosen to represent a bare-minimum character, but this list of variables can be expanded in the future. //class fields private Random rand = new Random(); //Make sure to import java.util.Random; B. We will not add getters and setters for this field. C. Now, we will add the method to our Enemy class. We will add a method to be able to do the following: A. move, Randomly chooses to move up, down, left, or right. D. To accomplish our previous point, add the following code to your class: //Randomly chooses to move up, down, left, or right public void move() { //we use a randomly generated number to automate the moves for the enemies if (this.getCurrentHitPoints() > 0) { int direction = rand.nextInt(4); switch (direction) { case 0: this.setLocationY(this.getLocationY() + 1); //moves up break; case 1: this.setLocationY(this.getLocationY() - 1);//moves down break; case 2: this.setLocationx(this.getLocationx() - 1);//moves left break; case 3: this.setLocationx(this.getLocationx() + 1);//moves right break; E. Finally, let's create our class constructor. It should do the following: A. Call the base class constructor and pass the needed arguments to the base class constructor. The arguments come from the Enemy's constructor and they are as follows: A. A String that represents the character's name. B. An integer that represents the starting X location and pass the value to the base constructor. C. An integer that represents the starting Y location and pass the value to the base constructor. B. Set the Enemy's health to 8 as a default value.
F. With all of the previous step, we can create our constructor with the following code: /** * @param name * @param locationX * @param locationY H. */ public Enemy (String name, int locationx, int locationY) { super (name, locationX, location Y); //call to our parent class's construct this.setCurrentHitPoints (8); //Change the hitpoints from 0 to 8 this.setWeaponDamage (2); //Change the weaponDamage from 0 to 2 } G. We have finished the derived (child/subclass) Enemy class! Enemy rand: Random = new Random() Enemy (name: String, locationX: int, locationY: int): Enemy move() I. Make sure there are no syntax errors (red underlines) and make sure to save the file. J. Commit and push your code to the GitHub repo.
Similar to to the previous exercise, let's extend the base class and add the specialized functionalities for the PlayableCharacter class. Recall that the user playing the game will make decisions for the PlayableCharacter and the randomly generated number will be used to make decisions for the Enemy. A. Open the Lab 5 Project B. Right-click on src. C. Right-click on lab5 > New > Class. Set the name to PlayableCharacter. D. We want to reuse the code already implemented in the base class, so let's make sure we use inheritance here. Edit the class definition to look like the following. public class PlayableCharacter extends Character A. We will not not add any fields for the PlayableCharacter. B. Now, we will add the method to our PlayableCharacter class. We will add a method to be able to do the following: A. move, The user chooses to move up, down, left, or right. It will take a Scanner object as input. We will do this so we can use the same Scanner object as we use in the main method. C. To accomplish our previous point, add the following code to your class: //Player's turn to move public void move(Scanner input) { System.out.println("Where to move? w for up, s for down, a for left, d for right. Any other key will skip the turn."); String in input.next(); switch (in) ( case "w": this.setLocationY(this.getLocationY() + 1); break; case "s": this.setLocationY(this.getLocation() - 1); break; case "a": } this.setLocationx(this.getLocationx() - 1); break; case "d": this.setLocationx(this.getLocationX() + 1); break; default: System.out.println("Turn Skipped. u for up, d for down, 1 for left, r for right. Any other key will skip the turn."); D. Finally, let's create our class constructor. It should do the following: A. Call the base class constructor and pass the needed arguments to the base class constructor. A. Take a String that represents the character's name and pass the value to the base constructor. B. Take an integer that represents the starting X location and pass the value to the base constructor. C. Take an integer that represents the starting Y location and pass the value to the base constructor. B. Set the hitpoints and weapon damage for this character E. With all of the previous step, we can create our constructor with the following code: @paran nane * @paran locationx * @paran location public playableCharacter(String name) { super (name, 0, 0); //This character always starts furthest from the stairs this.setCurrentHitPoints(10); this.setWeaponDamage (4); } F. We have finished the derived (child/subclass) PlayableCharacter class! PlayableCharacter PlayableCharacter (name: String): PlayableCharacter move(input: Scanner) G. H. Make sure there are no syntax errors (red underlines) and make sure to save the file. I. Commit and push your code to the GitHub repo.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply