Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Objective The objective of this assignment is to get you familiar with the notion of classes and objects and understand how objects interact with each

Objective

The objective of this assignment is to get you familiar with the notion of classes and objects and understand how objects interact with each other.

Problem Description

Your professor is kind of a huge MCU fan. So much so, that he bought the new Avengers game that was released recently and has spent the last several days trying to beat it. However, he is not very good at it, and has wasted enough time trying to complete it. Frustrated, he has decided to task his students to make their own Avengers game, one that does not require you to spend several hours playing. Your task is to come up with a program that will allows your professor to play this new and improved version of Marvel's Avengers.

Program Requirements

The idea is simple. You will be creating a randomized game that simulates a fight between 2 avengers.

Your first task is to create a class called Avengers. This class will represent the blueprint of an avenger.

An avenger has the following attributes. (Please make sure you keep these variable names exactly as listed here)

  1. name (eg.Iron Man) - String
  2. health (All avengers will start with a health 100) - int
  3. maxDamage( this represents the maximum damage that can be dealt in a move) - int
  4. tagLine (the avenger's favorite quote eg. I am Iron Man) - String

Define the following methods in the Avenger Class.

  1. Define a parameterized constructor that accepts in the name, maxDamage and tagLine and sets the above attributes to these values.
  2. Define a method "fight" that accepts another Avenger(enemy) as a parameter and starts fighting with the enemy. For simplicity, I will refer to the object that calls this method as the "avenger" and the avenger that is being passed in as the "enemy". For example, if the fight method is called like this - Hulk.fight(Thor), then hulk is referred to as the avenger and thor is referred to as the enemy.
    • The calling avenger will always start first. On their move, the avenger will deal some random damage ranging between 0 and maxDamage to the enemy. The enemy health should be reduced by this amount. See below on how to generate random numbers.
    • On the next move, the enemy deals some random damage back based on their maxDamage stats to the Avenger. The Avenger's health should be reduced accordingly.
    • This should continue until either the enemy or the avenger has reached 0 health. Keep in mind the health of the avenger( or enemy) should not go below 0.
    • Once one of them reaches 0, the method should print a message saying who won, followed by the tagLine of the winner. (See below for sample output)

Write a separate test program to test your class. You should use the following code in your test-class.

public static void main(String[] args) { Avenger ironMan = new Avenger ("Iron Man",15,"I am Iron Man!"); Avenger hulk = new Avenger ("Hulk",25,"Hulk Smash!"); ironMan.fight(hulk); } 

Sample Output

Running your test program should generate the following output.

Iron Man and Hulk have entered the arena. Let the fight begin.. ------------------------------------------- Iron Man just dealt 9 damage to Hulk. Hulk now has 91 hp remaining Hulk just dealt 5 damage to Iron Man. Iron Man now has 95 hp remaining Iron Man just dealt 10 damage to Hulk. Hulk now has 81 hp remaining Hulk just dealt 0 damage to Iron Man. Iron Man now has 95 hp remaining Iron Man just dealt 12 damage to Hulk. Hulk now has 69 hp remaining Hulk just dealt 4 damage to Iron Man. Iron Man now has 91 hp remaining Iron Man just dealt 9 damage to Hulk. Hulk now has 60 hp remaining Hulk just dealt 21 damage to Iron Man. Iron Man now has 70 hp remaining Iron Man just dealt 15 damage to Hulk. Hulk now has 45 hp remaining Hulk just dealt 19 damage to Iron Man. Iron Man now has 51 hp remaining Iron Man just dealt 8 damage to Hulk. Hulk now has 37 hp remaining Hulk just dealt 24 damage to Iron Man. Iron Man now has 27 hp remaining Iron Man just dealt 0 damage to Hulk. Hulk now has 37 hp remaining Hulk just dealt 12 damage to Iron Man. Iron Man now has 15 hp remaining Iron Man just dealt 12 damage to Hulk. Hulk now has 25 hp remaining Hulk just dealt 7 damage to Iron Man. Iron Man now has 8 hp remaining Iron Man just dealt 6 damage to Hulk. Hulk now has 19 hp remaining Hulk just dealt 13 damage to Iron Man. Iron Man now has 0 hp remaining Hulk won. Hulk says Hulk Smash!

Keep in mind the damage numbers as well as the number of moves shown here will not be the same for you, since these were randomly generated on my machine. But make sure your prints are in the same format as above.

Approach

The challenging part of this problem is completing the fight() method. As a hint, each round of moves should be inside a loop. Since you do not know how many rounds the fight will go (due to the random nature of damage dealt), you should be utilizing a while loop and not a for loop. In each iteration of your while loop, the avenger should deal some damage and the enemy should deal some damage back (if applicable). Then you should check if the game is over, and if not repeat the process. The winner should be announced at the end of the while loop.

Since this is primarily an assignment to test your understanding of classes and objects and how to manipulate objects, I have provided a method called generateDamage which accepts the maxDamage power of an avenger and generates a random number indicating the damage to be dealt in a move. You should include this method in your Avenger class and invoke it inside the fight method. This method should be used by both the avenger and the enemy to generate random damages, based on their maxDamage power. Copy paste the method below

public int generateDamage(int max) { Random r = new Random(); int range = max + 1; return r.nextInt(range) ; }

Make sure to add the following line before your class declaration at the top (after any package declarations)

import java.util.Random 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

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

Relational Database Design With Microcomputer Applications

Authors: Glenn A. Jackson

1st Edition

0137718411, 978-0137718412

More Books

Students also viewed these Databases questions

Question

Describe the factors influencing of performance appraisal.

Answered: 1 week ago

Question

What is quality of work life ?

Answered: 1 week ago