Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this question, you will write a number of classes to create a battle game be

ID: 3809464 • Letter: F

Question

For this question, you will write a number of classes to create a battle game between a player and a monster. Your code for this question will go in multiple .java files.

Note that in addition to the required methods below, you are free to add as many other private methods as you want.

(a) Character Class
Character.java represents a character in our battle game. The monster and the player will both

be instances of the Character class, each with their own attributes. The Character class should contain the following (private) attributes:

• A String name
• A double attack value
• A double maximum health value
• A double current health value
• A int number of wins in the battle game

Here are the required public methods for this class. Note that you will also have to add getters and setters for the instance attributes as needed.

1) A constructor

The constructor for the Character class takes one String, two doubles, and one int as input. These parameters represent the name, attack value, maximum health, and number of wins in the battle game for the character, in that order. Note that the current health of a new character is the same as the maximum health.

2) The toString method

This method returns a String consisting of the character’s name and current health. Format the String in any way you want. This method will be very handy for debugging your code, and will be used during the battle game to keep track of the health of each character.

3) calcAttack method

This method calculates how much attack damage one character does in a battle. The calculation is as follows: Take the character’s attack value and multiply it by a random value between 0.3 (inclusive) and 0.7 (exclusive). Return this value as a double. Use the Random class to generate the random numbers, and do not use a seed.

4) takeDamage method
In this method, we take the damage done to this character as a double parameter, and then we

subtract this value from the character’s current health. This method does not return anything. 5) increaseWins method

This method will increase the number of wins by the character by one, and does not return anything. This method will be called when the character wins the battle game.

(b) FileIO
FileIO.java must contain a static method readCharacter.

This method takes as input a filename as a String parameter, and returns a new Character, using the constructor defined in the Character class.

The readCharacter method must use a FileReader and a BufferedReader to open the file specified by filename. Make sure to have two catch blocks to catch both FileNotFoundException and IOException when reading from the file. In these cases, throw an IllegalArgumentException with an appropriate message that the file was not found, or that there was an IO exception. Note that the catch block for the FileNotFoundException must be directly above the catch block for the IOException.

If you prefer, you can also use the throws statement to throw the FileNotFoundException and IOException up to the calling playGame method. Then these Exceptions would be caught in a try/catch block there.

Example player.txt and monster.txt files are provided with the assignment. The format of these files is exactly this, one value per line:

• Name of the character
• Attack value
• Maximum health
• Number of wins so far in the battle game

Use the readLine() method of the BufferedReader to get the lines from the file. Do not use the Scanner class. Then, use the Double.parseDouble() and Integer.parseInt() methods to parse the lines. Finally, use those values to create and return a new Character.

(c) BattleGame
The code for this part will go in a file named BattleGame.java.

The BattleGame class only contains the playGame method, and the doAttack method, though you are allowed to create as many other private methods as you want.

1) playGame method must do the following things:
• Create the player and their enemy, using the FileIO.readCharacter method

– After a character is created, print the character’s name, current health, attack value, and number of wins.

– We recommend you place this print statement in a public method inside the Character class

• Use a Scanner to take input from the user
• Loop while both the player and the monster have health above zero

– Ask the user for a command

– For this question, the only options will be attack and quit

– If the input is attack:

Call the doAttack method (in the BattleGame class) with the player as first parameter and the monster as second parameter

Call the doAttack method with the monster as first parameter and the player as second parameter

If the input is quit

Print a goodbye message and return from the method

If the input is anything else

Print a message that the input was not recognized and suggest the attack or quit commands

• If the loop stops because one of the character's health is zero or below,then that character is knocked out. Print an appropriate message either congratulating the player, or saying how they lost. Also make sure to call the increaseWins method on either the player or the monster, depending who won.

2) The doAttack method takes two Characters as input, and returns nothing as output. This method must:

Get the damage from the first Character by calling the calcAttack method

Print out a statement with the first character’s name and how much damage they do.

Apply the damage to the second Character with the takeDamage method

If the second character’s current health is still above zero, print a message with their name and current health. If their current health is zero or below, print out a message saying that the second character was knocked out.

To make your output nicer, we suggest using a String formatting statement, though this is not necessary. For example, you could write this to only show two decimal places of the attack damage: String attackStr = String.format(‘‘%1$.2f’’, attack);

Here is some sample output produced by running the playGame method after Question 1 has been fin- ished. Output for the finished assignment is found at the bottom of this document. Note that for this assignment, your output doesn’t need to match this exactly. You are free to change these statements as you wish, as long as the required information still appears

Name Odin Health 30.00 Attack 10.00 Number of Wins 0 Name Fenrir Health 30.00 Attack 12:00 Number of Wins 0 Enter a Command attack Odin attacks for 9.85 damage Fenrir takes 9.85 damage! Name Fenrir Health 20.15 Fenrir attacks for 9.86 damage Odin takes 9.86 damage Name Odin Health 20.14 Enter a Command attack Odin attacks for 9.30 damage Fenrir takes 9.30 damage! Name Fenrir Health: 10.85

Explanation / Answer

You can create separate .java file for separate class given here.

Code:

package newpkg;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
import java.util.Scanner;

class Character{
   private String name;
   private double attack_value;
   private double max_health_val;
   private double current_health_val;
   private int num_wins;
   public Character(String name,double attack_value,double max_health_val,int num_wins){
       this.name=name;
       this.attack_value=attack_value;
       this.max_health_val=max_health_val;
       this.current_health_val=max_health_val;
       this.num_wins=num_wins;
   }
   public String toString(){
       return "Name :"+name+" max_health: "+max_health_val+" attack val : "+attack_value;
   }
   public double calcAttack(){
       Random rand=new Random();
       double randval=0.3+(0.6-0.3)*rand.nextDouble();
       return this.getAttack_value()*randval;
      
   }
   public void increaseWins(){
       num_wins++;
   }
   public void takeDamage(double damage){
       this.current_health_val=this.current_health_val-damage;
      
   }
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
   public double getAttack_value() {
       return attack_value;
   }
   public void setAttack_value(double attack_value) {
       this.attack_value = attack_value;
   }
   public double getMax_health_val() {
       return max_health_val;
   }
   public void setMax_health_val(double max_health_val) {
       this.max_health_val = max_health_val;
   }
   public double getCurrent_health_val() {
       return current_health_val;
   }
   public void setCurrent_health_val(double current_health_val) {
       this.current_health_val = current_health_val;
   }
   public int getNum_wins() {
       return num_wins;
   }
   public void setNum_wins(int num_wins) {
       this.num_wins = num_wins;
   }
  
  
  
}
class FileIO{
   public static Character readCharacter(String filename){
       Character character=null;
       FileInputStream fileInputStream = null;
       InputStreamReader inputStreamReader = null;
       BufferedReader bufferedReader = null;
       String name="";
       double attack_val=0;
       double max_health=0;
       int num_wins=0;
       try {
            fileInputStream = new FileInputStream(filename);
            inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
            bufferedReader = new BufferedReader(inputStreamReader);
            String s;
            int i=0;
      
            while((s = bufferedReader.readLine()) != null){
               if(i==0)
               {
                   name=s;
               }else if(i==1){
                   attack_val=Double.parseDouble(s);
               }else if(i==2){
                   max_health=Double.parseDouble(s);
               }else if(i==3){
                   num_wins=Integer.parseInt(s);
               }
               i++;
              
            }
          
          
       }catch(FileNotFoundException fe){
           fe.printStackTrace();
       }catch(IOException ioe){
           ioe.printStackTrace();
       }
       character=new Character(name,attack_val,max_health,num_wins);
       return character;
   }
}
public class BattleGame {
   public static void playGame(){
       Character player=FileIO.readCharacter("C:\Users\IBM_ADMIN\workspace\testnew\src ewpkg\player.txt");
       Character monster=FileIO.readCharacter("C:\Users\IBM_ADMIN\workspace\testnew\src ewpkg\monster.txt");
       System.out.println(player);
       System.out.println(monster);
       Scanner scan=new Scanner(System.in);
       String command=null;
       while(player.getCurrent_health_val()>0 && monster.getCurrent_health_val()>0){
           System.out.println("Enter a command:");
           command=scan.next();
           if(command.equalsIgnoreCase("attack")){
               doAttack(player,monster);
               doAttack(monster,player);
           }else if(command.equalsIgnoreCase("quit")){
               System.out.println("Goodbye !");
               return;
           }else{
               System.out.println("Input was not recognised: Recommended inputs -> attack or quit");
           }
       }
       if(player.getCurrent_health_val()==0 && monster.getCurrent_health_val()==0){
           System.out.println("This is a draw");
       }else if(player.getCurrent_health_val()==0){
           System.out.println("Congratulations monster.. you won!!!");
           System.out.println("Hi Player..You have lost the game.. better luck next time");
           monster.setNum_wins(monster.getNum_wins()+1);
       }else if(monster.getCurrent_health_val()==0){
           System.out.println("Congratulations player.. you won!!!");
           System.out.println("Hi monster..You have lost the game.. better luck next time");
           player.setNum_wins(player.getNum_wins()+1);
       }
      
   }

   private static void doAttack(Character player1, Character player2) {
       double damage=player1.calcAttack();
       System.out.println(player1.getName()+" attacks with "+damage+" damage");
       player2.takeDamage(damage);
       if(player2.getCurrent_health_val()>0){
           System.out.println("Name :"+player2.getName()+" : "+player2.getCurrent_health_val());
       }else
           System.out.println("Name :"+player2.getName()+" knocked out");  
      
   }
   public static void main(String args[]){
       playGame();
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote