/**
*
* SuperHero class */
//SuperHero.java
public class SuperHero {
//static variable
private static int numberOfHeros;
//private data member
private String heroName;
private String secretIdentiy;
private int numberOfLifeChances;
private int numberOfPeopleSaved;
//constructor that takes three arguments
public SuperHero(String initHeroName,String initSecretIdentity,
int initPeopleSaved) {
numberOfHeros++;
numberOfLifeChances=2;
heroName=initHeroName;
secretIdentiy=initSecretIdentity;
numberOfPeopleSaved=initPeopleSaved;
}
//constructor takes only name
public SuperHero(String initHeroName) {
heroName=initHeroName;
numberOfHeros++;
numberOfLifeChances=2;
secretIdentiy=\"unknowm\";
numberOfPeopleSaved=0;
}
//Static method that returns the number of heros
public static int getNumberOfHeros()
{
return numberOfHeros;
}
/*The method recordSave that increments the count
of numberOfPeopleSaved by one.*/
public void recordSave(){
numberOfPeopleSaved++;
}
/*The method recordSave that takes a number of people saved
* and add to numberOfPeopleSaved.*/
public void recordSave(int num){
numberOfPeopleSaved+=num;
}
public void killHero(){
if(numberOfLifeChances>1)
numberOfLifeChances--;
else
System.out.println(\"Dead\");
}
public void printSuperHeoRecord(){
System.out.println(\"Name :\"+heroName);
System.out.println(\"Secret Identity:\"+secretIdentiy);
String status=numberOfLifeChances>0?\"Alive\":\"Dead\";
System.out.println(\"Status: \"+status);
System.out.println(\"Peaple Saved: \"+numberOfPeopleSaved);
}
}
--------------------------------------------------------------------------------------------------------------------
-------------------
/*
* The java TestSuperHero that tests the methods of
* SuperHero class and prompts
* user to enter name and secret code and
* prints the information to console.
* */
//TestSuperHero.java
import java.util.Scanner;
public class TestSuperHero {
public static void main(String[] args) {
//Create a Scanner object
Scanner scan=new Scanner(System.in);
//Create an superhero called Spider-Man
System.out.println(\"Creating Spider-Man......\");
SuperHero spiderman=new SuperHero(\"Spider-Man\");
//Ask the user to enter a superhero name
System.out.println(\"\ What is the name of your superheo?\");
String heroName=scan.nextLine();
System.out.println(\"What is his secret identity?\");
String secretIdentity=scan.nextLine();
System.out.println(\"Createing your super hero.......\");
SuperHero myHero=new SuperHero(heroName, secretIdentity, 10);
System.out.println(\"\ Spider-Man just saved 100 lives!\");
spiderman.recordSave(100);
System.out.println(\"OOps, Spider-Man was shot dead twice!\");
//killing spider man twice
spiderman.killHero();
spiderman.killHero();
System.out.println(\"\ Your hero saved a kidnapped kid\");
System.out.println(\"but was shot once\");
//calling recordSave
myHero.recordSave(1);
System.out.println(\"\ ---- Superhero information-----\");
//calling static method getNumberOfHeros();
int numHeroes=SuperHero.getNumber.
SuperHero class SuperHero.java public class Sup.pdf
1. /**
*
* SuperHero class */
//SuperHero.java
public class SuperHero {
//static variable
private static int numberOfHeros;
//private data member
private String heroName;
private String secretIdentiy;
private int numberOfLifeChances;
private int numberOfPeopleSaved;
//constructor that takes three arguments
public SuperHero(String initHeroName,String initSecretIdentity,
int initPeopleSaved) {
numberOfHeros++;
numberOfLifeChances=2;
heroName=initHeroName;
secretIdentiy=initSecretIdentity;
numberOfPeopleSaved=initPeopleSaved;
}
//constructor takes only name
public SuperHero(String initHeroName) {
heroName=initHeroName;
numberOfHeros++;
numberOfLifeChances=2;
2. secretIdentiy="unknowm";
numberOfPeopleSaved=0;
}
//Static method that returns the number of heros
public static int getNumberOfHeros()
{
return numberOfHeros;
}
/*The method recordSave that increments the count
of numberOfPeopleSaved by one.*/
public void recordSave(){
numberOfPeopleSaved++;
}
/*The method recordSave that takes a number of people saved
* and add to numberOfPeopleSaved.*/
public void recordSave(int num){
numberOfPeopleSaved+=num;
}
public void killHero(){
if(numberOfLifeChances>1)
numberOfLifeChances--;
else
System.out.println("Dead");
}
public void printSuperHeoRecord(){
System.out.println("Name :"+heroName);
System.out.println("Secret Identity:"+secretIdentiy);
String status=numberOfLifeChances>0?"Alive":"Dead";
System.out.println("Status: "+status);
3. System.out.println("Peaple Saved: "+numberOfPeopleSaved);
}
}
--------------------------------------------------------------------------------------------------------------------
-------------------
/*
* The java TestSuperHero that tests the methods of
* SuperHero class and prompts
* user to enter name and secret code and
* prints the information to console.
* */
//TestSuperHero.java
import java.util.Scanner;
public class TestSuperHero {
public static void main(String[] args) {
//Create a Scanner object
Scanner scan=new Scanner(System.in);
//Create an superhero called Spider-Man
System.out.println("Creating Spider-Man......");
SuperHero spiderman=new SuperHero("Spider-Man");
//Ask the user to enter a superhero name
System.out.println(" What is the name of your superheo?");
String heroName=scan.nextLine();
System.out.println("What is his secret identity?");
String secretIdentity=scan.nextLine();
System.out.println("Createing your super hero.......");
SuperHero myHero=new SuperHero(heroName, secretIdentity, 10);
System.out.println(" Spider-Man just saved 100 lives!");
4. spiderman.recordSave(100);
System.out.println("OOps, Spider-Man was shot dead twice!");
//killing spider man twice
spiderman.killHero();
spiderman.killHero();
System.out.println(" Your hero saved a kidnapped kid");
System.out.println("but was shot once");
//calling recordSave
myHero.recordSave(1);
System.out.println(" ---- Superhero information-----");
//calling static method getNumberOfHeros();
int numHeroes=SuperHero.getNumberOfHeros();
System.out.println("There are "+numHeroes+" known superheroes");
//calling printSuperHeoRecord()
spiderman.printSuperHeoRecord();
System.out.println();
//calling printSuperHeoRecord()
myHero.printSuperHeoRecord();
}
}
--------------------------------------------------------------------------------------------------------------------
-------------------
Sample Output:
Creating Spider-Man......
5. What is the name of your superheo?
Superman
What is his secret identity?
Clark
Createing your super hero.......
Spider-Man just saved 100 lives!
OOps, Spider-Man was shot dead twice!
Dead
Your hero saved a kidnapped kid
but was shot once
---- Superhero information-----
There are 2 known superheroes
Name :Spider-Man
Secret Identity:unknowm
Status: Alive
Peaple Saved: 100
Name :Superman
Secret Identity:Clark
Status: Alive
Peaple Saved: 11
Solution
/**
*
* SuperHero class */
//SuperHero.java
public class SuperHero {
//static variable
private static int numberOfHeros;
//private data member
private String heroName;
private String secretIdentiy;
6. private int numberOfLifeChances;
private int numberOfPeopleSaved;
//constructor that takes three arguments
public SuperHero(String initHeroName,String initSecretIdentity,
int initPeopleSaved) {
numberOfHeros++;
numberOfLifeChances=2;
heroName=initHeroName;
secretIdentiy=initSecretIdentity;
numberOfPeopleSaved=initPeopleSaved;
}
//constructor takes only name
public SuperHero(String initHeroName) {
heroName=initHeroName;
numberOfHeros++;
numberOfLifeChances=2;
secretIdentiy="unknowm";
numberOfPeopleSaved=0;
}
//Static method that returns the number of heros
public static int getNumberOfHeros()
{
return numberOfHeros;
}
/*The method recordSave that increments the count
of numberOfPeopleSaved by one.*/
public void recordSave(){
7. numberOfPeopleSaved++;
}
/*The method recordSave that takes a number of people saved
* and add to numberOfPeopleSaved.*/
public void recordSave(int num){
numberOfPeopleSaved+=num;
}
public void killHero(){
if(numberOfLifeChances>1)
numberOfLifeChances--;
else
System.out.println("Dead");
}
public void printSuperHeoRecord(){
System.out.println("Name :"+heroName);
System.out.println("Secret Identity:"+secretIdentiy);
String status=numberOfLifeChances>0?"Alive":"Dead";
System.out.println("Status: "+status);
System.out.println("Peaple Saved: "+numberOfPeopleSaved);
}
}
--------------------------------------------------------------------------------------------------------------------
-------------------
/*
* The java TestSuperHero that tests the methods of
* SuperHero class and prompts
* user to enter name and secret code and
* prints the information to console.
* */
//TestSuperHero.java
import java.util.Scanner;
8. public class TestSuperHero {
public static void main(String[] args) {
//Create a Scanner object
Scanner scan=new Scanner(System.in);
//Create an superhero called Spider-Man
System.out.println("Creating Spider-Man......");
SuperHero spiderman=new SuperHero("Spider-Man");
//Ask the user to enter a superhero name
System.out.println(" What is the name of your superheo?");
String heroName=scan.nextLine();
System.out.println("What is his secret identity?");
String secretIdentity=scan.nextLine();
System.out.println("Createing your super hero.......");
SuperHero myHero=new SuperHero(heroName, secretIdentity, 10);
System.out.println(" Spider-Man just saved 100 lives!");
spiderman.recordSave(100);
System.out.println("OOps, Spider-Man was shot dead twice!");
//killing spider man twice
spiderman.killHero();
spiderman.killHero();
System.out.println(" Your hero saved a kidnapped kid");
System.out.println("but was shot once");
//calling recordSave
myHero.recordSave(1);
System.out.println(" ---- Superhero information-----");
9. //calling static method getNumberOfHeros();
int numHeroes=SuperHero.getNumberOfHeros();
System.out.println("There are "+numHeroes+" known superheroes");
//calling printSuperHeoRecord()
spiderman.printSuperHeoRecord();
System.out.println();
//calling printSuperHeoRecord()
myHero.printSuperHeoRecord();
}
}
--------------------------------------------------------------------------------------------------------------------
-------------------
Sample Output:
Creating Spider-Man......
What is the name of your superheo?
Superman
What is his secret identity?
Clark
Createing your super hero.......
Spider-Man just saved 100 lives!
OOps, Spider-Man was shot dead twice!
Dead
Your hero saved a kidnapped kid
but was shot once
---- Superhero information-----
There are 2 known superheroes
Name :Spider-Man
Secret Identity:unknowm