Question: This is a multi-class console lab that has two files. I will give you the code for your main() method and class and I want
This is a multi-class console lab that has two files. I will give you the code for your main() method and class and I want you to upload only the Monster.java file with your Monster class.
Main
First create your lab project, ie Lab20, with NetBeans. Next copy and paste the code below into the main method replacing the To do comment.
Monster genericMonster = new Monster();
System.out.println(genericMonster.toString());
Monster smallGhost = new Monster("Small ghost", (short)2); System.out.println(smallGhost.toString());
Monster meanGhost = new Monster("Mean ghost", (short)5, (byte)2, (byte)0); System.out.println(meanGhost.toString() + " ");
// small ghost is damaged by 5 magic damage
System.out.println("Small ghost is hit for 5 magic damage"); smallGhost.damageMonster((short)5, Monster.Damage.MAGIC);
System.out.println(smallGhost.toString() + " ");
// mean ghost is hit for 3 physical damage and resists 2 hps of damage
System.out.println("Mean ghost is hit for 3 physical damage"); meanGhost.damageMonster((short)3, Monster.Damage.PHYSICAL);
System.out.println(meanGhost.toString());
I want you to use this code exactly because if you make a mistake in your Monster class, it should cause errors or warnings in main. Likewise, you should get this exact output from running the code in main():
If you don't have the same output you have a bug/typo in your toString() method in Monster.
Monster Class
To add another class to your lab 20 project, right click the Java Source Package and select New -> Java Class. This will pop up the New Java Class window. For the Class Name fill in: Monster and then click the Finish button at the bottom and a new class file named Monster.Java will be added to your project.
Field Variables
The field variables of a class are normally private, but the first field variable of Monster should be public because it is a constant variable type called an enumeration. The concern about public variables is they can be changed without using a mutator or accessor but since a constant cant be changed this is not a concern.
Enum Types
An enum type is a special data type that allows a variable to only be one of a set of predefined constants. The Monster class enumeration should be named Damage and have two constants: PHYSICAL and MAGIC. The syntax is as follows:
public enum Damage { PHYSICAL, MAGIC } You can see the constants being used in the main code as an argument in the damageMonster() method call.
The other field variables of the class are private and are as follows:
a variable type to hold the monsters name
a variable type to hold the monsters maximum hit points. No monster will have more than 1,500 maximum hit points.
a variable to hold the monsters current hit points. Current hit points will never exceed maximum hit points.
a variable to hold the monsters physical resistance. No monster will have more than 25 physical resistance.
a variable to hold the monsters magic resistance. No monster will have more than 25 magic resistance.
For each variable type use the type with the smallest memory size that is able to hold the maximum expected value.
Accessor and Mutator Methods
Create the accessor and mutator methods before the constructors because we want to make calls to the set methods inside the constructors. All the accessors (gets) of Monster just return the same variable type as the private field variable.
The mutators (sets) for the monsters name and two resistances just have an argument of the same type as the field variable and just set the field variable to the value of the argument passed into the mutator. The mutators for setting maximum hit points and current hit points do error checking and correcting as follows.
For the setting maximum hit points mutator if the argument is less than 1, set maximum hit points to 1 or if the argument is > 1,500 then set maximum hit points to 1,500. Otherwise set maximum hit points to the value passed in.
The setting current hit points mutator is similar but 0 hit points is allowed so if the argument is the maximum hit points well set current hit points to the monsters maximum hit points. Otherwise, we set current hit points to argument value passed in.
Constructors
At this point inheritance is a powerful and dangerous magic we havent learned yet, but NetBeans is going to generate (valid) warnings if we use public mutators inside the constructor. We will stop the warnings on this lab by stopping inheritance magic from happening from the Monster class. Do this by adding the final keyword in the class declaration so it looks like this:
final public class Monster
Create a working constructor that has arguments for the monsters name, maximum hit points, physical resistance and magic resistance. Notice how this working constructor doesnt have an argument for the fifth private variable: current hit points.
In the constructor body call the mutator to set the name with the name argument, and call the mutator set the maximum hit points with the maximum hit point argument.
When we create a monster, we want the current hit points to be equal to the amount that ends up in the maximum hit point field - this is important because if there is a bad input that gets adjusted inside of the maximum hit point mutator being called by the constructor we want that adjusted input to be what we set current hit points to. So next we call the set current hit points mutator with the field variable for maximum hit points not the argument passed into the constructor.
Finish up the working constructor by calling the mutator to set the physical resistance with the physical resistance argument and the mutator to set the magic resistance with the magic resistance argument. Note in main() meanGhost is created with the working constructor.
For the other constructors we want to use the one code path technique and just have them call the working constructor with the arguments they get and some default values.
Create a two argument constructor that has monster name and maximum hit point arguments and in the body of the constructor call the working constructor passing in the name and maximum hit point arguments and setting physical and magic resistance to 0. Note smallGhost in main() is created with the two argument constructor.
Create a default (no argument) constructor calls the working constructor setting the monster name to The Monster, maximum hit points to 1 and setting physical and magic resistance to 0. Note genericMonster in main() is created with the default constructor.
toString() Method
We would like our Monster class to work with GUI or console applications so we dont want to do any type of output with print or println inside the class, however what we can do is return a string containing all information about all the monsters private fields. That would allow the information string to be output to the console in a console app or be displayed in a control like a text field in a GUI app.
To do this create a public toString() method that uses string concatenation to create and return a string with that has the monsters name, maximum hit points, current hit points, physical resistance, magic resistance and some text explain what the variables are. If you look at the program output you should notice the first three outputs are from calls in main to the toString() methods of genericMonster, smallGhost and meanGhost that are being output to the console by println. Make sure your output is *identical*.
damageMonster() Method
This method is used to adjust the monsters current hit points when it is damaged, taking into account the type of damage taken and the monsters resistances to physical and magic damage. Its not a lot of code, but the logic of the method is complex enough Ill walk you through it.
Input: two arguments: one with the amount of damage the other with the type of damage. The amount of damage argument is straight forward. The other argument is where we use that enum constant Damage. This is because using it guarantees the value is *only* going to be Monster.Damage.PHYSICAL or Monster.Damage.MAGIC because those are the only two possibilities. The compiler wont let the method be called with any other value for damage type. Look in main at smallGhost calling damageMonster for 5 damage of damage type magic and meanGhost calling damageMonster for 3 damage of damage type physical.
Processing: The way we will implement magic and physical resistance is they subtract from the incoming damage amount but *only* up to the point the damage becomes 0. So, if the monster has incoming 5 physical damage and has 3 physical resistance, we will subtract 3 from 5 so the monster only takes 2 damage. However, if the incoming damage is 2 and the monster has a resistance to that damage type of 3, we will *not* increase the monsters current hit points like it would if we just did simple subtraction. Putting it another way, the monsters current hit points will only stay the same as a result of enough resistance to counter the damage or go *down* as a result of damage overcoming the resistance. In addition, we are going to ignore negative damage as bad input.
Output: this class method does output by adjusted the current hit point field as necessary.
The damageMonster() method uses a series of nested ifs. First we check to see if the damage is physical or magic, once we know that, we check to see if the incoming damage is greater than the resistance and if it is then we go about updating current hit points. Checking to see that the incoming damage is greater than resistance also allows us to ignore negative inputs (or strictly speaking less than the monsters resistance). Here is pseudo code for the method body:
if damage type is physical
{ if damage amount > physical resistance
{ damage amount -= physical resistance
updateHP = field current hp - damage amount
call set current hit point mutator with updateHP argument
}
}
else // must be magic because of enumerator
{ if damage amount > magic resistance
{ damage amount -= magic resistance
updateHP = field current hp - damage amount
call set current hit point mutator with updateHP argument
}
}
Notice that we again re-use the set current hit point mutator which handles the logic of what to do if the monsters hit points go negative. You can see this working in main() where smallGhost with 2 hit points takes 5 magic damage and has no magic resistance and goes to 0 hit points instead of -3 hit points.
Reminder: For this lab I only want your Monster.java file. Make sure you have a comment with your name, section and lab 20.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
