Looking for help with writing this program Part 1: Defining the Class The first
ID: 3555098 • Letter: L
Question
Looking for help with writing this program
Part 1: Defining the Class
The first part of creating any class is to declare it as a class. The overall set up for the SuperHero
class when the lab is done should look something like the code below. Copy it to your
SuperHero.java file.
public class SuperHero {
// instance variables go below here
// the two constructors go below here
// getNumberOfHeroes() goes below here
// recordSave() goes below here
// the second recordSave method goes here
// killHero() goes below here
// printSuperHeroRecord() goes below here
}Part 2: Defining the Instance Variables
Instance variables are used to define different traits about an object. In the SuperHero class,
define the following instance variables in the appropriate place:
? numberOfHeroes, a static int
? heroName, a String
? secretIdentity, a String
? numberOfLifeChances, an int
? numberOfPeopleSaved, an int
Notes & Hints
? Be sure to declare all of your instance variables as private, as we do not want other
programs to have direct access to them.
? numberOfHeroes needs to be a static variable, because its value needs to be the same
between all instances of the SuperHero class.
? As an example for the other three, the heroName instance variable is written below:
private String heroName;
Part 3: Creating the Constructors
The purpose of a constructor is to define the instance variables of the object upon its creation. By
allowing for arguments to be passed into the constructor, these instance variables can be easily
customized. For example, the following constructor will allow for the heroName, secretIdentity,
and numberOfPeopleSaved to be customized:
public SuperHero(String initHeroName, String initSecretIdentity, int initPeopleSaved)
{
numberOfHeroes++; // one more hero created
numberOfLifeChances = 2; // start the hero with two lives
heroName = initHeroName;
secretIdentity = initSecretIdentity;
numberOfPeopleSaved = initPeopleSaved;
}
Include this code in the constructor section of your SuperHero class.
Notice how the numberOfHeroes and numberOfLifeChances are explicitly defined in the
constructor, while the values of the other instance variables depend on what was passed into the
constructor.Now, create another constructor that will only take one parameter for the hero's name. The
constructor should contain the following (you can place it below the first one):
? Only allow the heroName as a parameter for the constructor
? Use the same code for defining the numberOfHeroes and the numberOfLifeChances
? Set the heroName attribute to the parameter passed into the constructor
? Set the secretIdentity to
Explanation / Answer
//SuperHero.java Class
public class SuperHero {
// instance variables go below here
private static int numberOfHeroes;
private String heroName;
private String secretIdentity;
private int numberOfLifeChances;
private int numberOfPeopleSaved;
// the two constructors go below here
public SuperHero(String initHeroName, String initSecretIdentity,
int initPeopleSaved) {
numberOfHeroes++; // one more hero created
numberOfLifeChances = 2; // start the hero with two lives
heroName = initHeroName;
secretIdentity = initSecretIdentity;
numberOfPeopleSaved = initPeopleSaved;
}
public SuperHero(String initHeroName) {
numberOfHeroes++; // one more hero created
numberOfLifeChances = 2; // start the hero with two lives
heroName = initHeroName;
secretIdentity = "Unknown";
numberOfPeopleSaved = 0;
}
// getNumberOfHeroes() goes below here
public static int getNumberOfHeroes() {
// Put something here (only 1 line is required)
return numberOfHeroes;
}
// recordSave() goes below here
public void recordSave() {
numberOfPeopleSaved++;
}
// the second recordSave method goes here
public void recordSave(int num) {
numberOfPeopleSaved += num;
}
// killHero() goes below here
public void killHero() {
if (numberOfLifeChances == 0) {
// Print a message saying the hero is dead
System.out.println("The Hero is Dead.. :(");
} else {
// Decrease numberOfLifeChances by 1
numberOfLifeChances--;
}
}
// printSuperHeroRecord() goes below here
public void printSuperHeroRecord() {
System.out.println("Name: " + heroName + " Secret Identity: "
+ secretIdentity + " Status: "
+ ((numberOfLifeChances == 0) ? "Dead" : "Alive")
+ " People Saved: " + numberOfPeopleSaved);
}
}
//TestSuperHero.java Class
import java.util.Scanner;
public class TestSuperHero {
public static void main(String[] args) {
/*
* SuperHero hero1 = new SuperHero("Superman", "Clark", 1000); SuperHero
* hero2 = new SuperHero("Batman");
*/
Scanner scan = new Scanner(System.in);
// Create a superhero called Spider-Man
SuperHero spiderman = new SuperHero("Spider-Man");
// Ask the user to enter a superhero name
System.out.println(" What is your super hero's name?");
String heroName = scan.nextLine();
System.out.println("What is his secret identity?");
/** 13: Read in the identity */
String identity = scan.next();
System.out.println(" Creating your super hero.......");
/** 16: Create the hero called yourHero, who saved 10 people */
SuperHero yourHero = new SuperHero(heroName, identity, 10);
System.out.println("Spider-Man just saved 100 lives!");
/** 19: Call recordSave on spiderman with 100 as the input */
spiderman.recordSave(100);
System.out.println("Oops, Spider-Man was shot dead twice!");
/** 22: Kill spiderman twice */
spiderman.killHero();
spiderman.killHero();
System.out.print("Your hero saved a kidnapped kid but was shot once ");
/** 26: Kill your hero once */
yourHero.killHero();
/** 27: Add 1 to your hero
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.