Question
I cannot get this code to work. Please help! //Superhero class public class SuperHero { String name; int health; boolean isDead; public SuperHero(String name, int
I cannot get this code to work. Please help!
//Superhero class
public class SuperHero { String name; int health; boolean isDead; public SuperHero(String name, int health) { //Initialize internal variables this.name = name; this.health = health; } public void attack(Superhero opponent) { // Create a random number between 1 and 10 Random rand = new Random(); int damage = rand.ints(1, (10 + 1)).findFirst().getAsInt(); // Set the health of the provided opponent opponent.determineHealth(damage); System.out.println(String.format("%s has damage of %d and health of %d", oppoent.name, damage, opponent.health)); } public boolean isDead() { return isDead; } private void determineHealth(int damage) { // If no more health then mark as dead else subtract damage from health and continue if(this.health - damage
===========================================
//Superman class
public class Superman extends Superhero { public Superman(int health) { super("Superman", health); } }
============================
//Batman class
public class Batman extends Superhero { public Batman(int health) { super("Batman", health); }
}
=============================
//Game class
public class Game {
public static void main(String[] args) { // Create a random health between 1 and 1000 Random rand = new Random(); int health1 = rand.ints(1, (1000 + 1)).findFirst().getAsInt(); int health2 = rand.ints(1, (1000 + 1)).findFirst().getAsInt(); // Create Superman and Batman System.out.println("Creating our Super Heroes....."); Superman superman = new Superman(health1); Batman batman = new Batman(health2); System.out.println("Super Heroes created"); // Run the game until one of the Super Heroes are dead System.out.println("Running our game....."); while(!superman.isDead() && !batman.isDead()) { // Attach each other superman.attack(batman); batman.attack(superman); // See if anybody survived if(superman.isDead()) { System.out.println("Batman defeated Superman"); } if(batman.isDead()) { System.out.println("Superman defeated Batman"); } }
}
}
::: .. RE ER LILL . TILLS SI SLANIR. RE FERRER 2. .. . PER SENSIBLE 18 - ce . . RE .....: Heal Lines
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