public class SuperHero { // instance variables go below here private static int
ID: 3550401 • Letter: P
Question
public class SuperHero
{
// instance variables go below here
private static int numberOfHeroes;
private String secretIdentity, heroName;
private int numberOfLifeChances, 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++;
numberOfLifeChances = 2;
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 numberOfPeopleSaved, int num)
{
// Increment numberOfPeopleSaved by num
numberOfPeopleSaved = numberOfPeopleSaved + num;
}
// the second recordSave method goes here
// killHero() goes below here
public void killHero() {
if (<check if the hero has lives, using numberOfLifeChances>) {
// Decrease numberOfLifeChances by 1
} else {
// Print a message saying the hero is dead
}
}
This method will simply print out the different attributes of the hero. The output should look like
the following:
Name: Superman
Secret Identity: Clark
Status: Alive
People Saved: 1000
Note that if the hero has no lives left, the status should be
Explanation / Answer
class SuperHero
{
// instance variables go below here
private static int numberOfHeroes;
private String secretIdentity, heroName;
private int numberOfLifeChances, 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++;
numberOfLifeChances = 2;
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 numberOfPeopleSaved, int num)
{
// Increment numberOfPeopleSaved by num
numberOfPeopleSaved = numberOfPeopleSaved + num;
}
// the second recordSave method goes here
// killHero() goes below here
public void killHero()
{
if (numberOfLifeChances>0)
{
numberOfLifeChances--;
// Decrease numberOfLifeChances by 1
} else
{
System.out.println(heroName+"Hero is already dead");
// Print a message saying the hero is dead
}
}
public void printSuperHeroRecord()
{
System.out.println("Name: "+heroName);
System.out.println("Secret Identity : "+secretIdentity);
if(numberOfLifeChances<=0)
{
System.out.println("Status : Dead");
}
else
{
System.out.println("Status : Alive");
}
System.out.println("People Saved : "+numberOfPeopleSaved);
}
// printSuperHeroRecord() goes below here
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.