Question
To begin, create a Java file named Asg07.java that consists of a public class named Asg07 that has a main method. In the same file,
To begin, create a Java file named Asg07.java that consists of a public class named Asg07 that has a main method.
In the same file, declare a Player class as follows:class Player
Do not declare the Player class public. You can only have one public class in a Java file, and youve already made the Asg07 class public.
Next, declare two private member variables for name and health. Then, create a constructor for the class that accepts a String for the name and sets the health to 100. Then, create getter and setter methods for both variables. You do not need to validate the name; but you do need to validate the incoming value for health to ensure the value is within the range of 0 and 100. If an attempt is made to set it below 0, set it 0; likewise, if an attempt is made to set the value higher than 100, set it to 100.
Next, create a method named attack() that accepts a Player object as a parameter. The declaration for the method should be
public void attack(Player opponent)
Within the method, generate a random number between 0 and 25. If the value falls between 0 and 15, reduce the opponents health by that amount. If the value is over 15, consider the attack a failed attempt. Display on the screen a message like this if the attack is successful
Player x attacked player y and the player's health is now z.
and the following when an attack is a failure
Player x attacked player y and missed. In these expressions, x is the attacking players name, y is the name of the player being attacked, and z
is the number of health points the player who is attacked possesses afterward.
To test your class, go to your main()method of your Asg07 class and create two Player objects, p1 andp2. Build a loop that continues until one of the players health reaches 0. Within the loop, have the first player attack the second, and then the second attack the first. Allow a player to attack the another only if both players are alive. Test your program to make sure the attacks progress until one of the players is left alive.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started