SlideShare a Scribd company logo
1 of 10
Download to read offline
/**
*
* 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.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
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;
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.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
Status: Alive
Peaple Saved: 100
Name :Superman
Secret Identity:Clark
Status: Alive
Peaple Saved: 11

More Related Content

More from annaimobiles

Hg2+ is more water soluble, methyl mercury HgCH3+.pdf
                     Hg2+ is more water soluble, methyl mercury HgCH3+.pdf                     Hg2+ is more water soluble, methyl mercury HgCH3+.pdf
Hg2+ is more water soluble, methyl mercury HgCH3+.pdfannaimobiles
 
HCl is a polar-covalent compound and also Acetoni.pdf
                     HCl is a polar-covalent compound and also Acetoni.pdf                     HCl is a polar-covalent compound and also Acetoni.pdf
HCl is a polar-covalent compound and also Acetoni.pdfannaimobiles
 
for a compound to be soluble in water 1. it shou.pdf
                     for a compound to be soluble in water  1. it shou.pdf                     for a compound to be soluble in water  1. it shou.pdf
for a compound to be soluble in water 1. it shou.pdfannaimobiles
 
e) II and III have the loest, equal stability .pdf
                     e) II and III have the loest, equal stability .pdf                     e) II and III have the loest, equal stability .pdf
e) II and III have the loest, equal stability .pdfannaimobiles
 
wait doingSolutionwait doing.pdf
wait doingSolutionwait doing.pdfwait doingSolutionwait doing.pdf
wait doingSolutionwait doing.pdfannaimobiles
 
TRUE The Reynolds number of falling 2.5 micrometer flyash particles .pdf
TRUE The Reynolds number of falling 2.5 micrometer flyash particles .pdfTRUE The Reynolds number of falling 2.5 micrometer flyash particles .pdf
TRUE The Reynolds number of falling 2.5 micrometer flyash particles .pdfannaimobiles
 
the electron rich molecule is a central Xe with 4 F singlebonded.pdf
the electron rich molecule is a central Xe with 4 F singlebonded.pdfthe electron rich molecule is a central Xe with 4 F singlebonded.pdf
the electron rich molecule is a central Xe with 4 F singlebonded.pdfannaimobiles
 
The given formulae themselves are explicit. Explicit means direct i..pdf
The given formulae themselves are explicit. Explicit means direct i..pdfThe given formulae themselves are explicit. Explicit means direct i..pdf
The given formulae themselves are explicit. Explicit means direct i..pdfannaimobiles
 
The answer is Meiosis, It occurs in human body at reporductive organ.pdf
The answer is Meiosis, It occurs in human body at reporductive organ.pdfThe answer is Meiosis, It occurs in human body at reporductive organ.pdf
The answer is Meiosis, It occurs in human body at reporductive organ.pdfannaimobiles
 
Structure of HemoglobinHemoglobin is a chromo protein and is found.pdf
Structure of HemoglobinHemoglobin is a chromo protein and is found.pdfStructure of HemoglobinHemoglobin is a chromo protein and is found.pdf
Structure of HemoglobinHemoglobin is a chromo protein and is found.pdfannaimobiles
 
SolutionSelection Sort after two iterations1 14 8 9 5 16 2 1.pdf
SolutionSelection Sort after two iterations1 14 8 9 5 16 2 1.pdfSolutionSelection Sort after two iterations1 14 8 9 5 16 2 1.pdf
SolutionSelection Sort after two iterations1 14 8 9 5 16 2 1.pdfannaimobiles
 
Starch agar consists of starch. The secretion of amylase enzyme by s.pdf
Starch agar consists of starch. The secretion of amylase enzyme by s.pdfStarch agar consists of starch. The secretion of amylase enzyme by s.pdf
Starch agar consists of starch. The secretion of amylase enzyme by s.pdfannaimobiles
 
Solution find the solution below and please give the feedback and p.pdf
Solution find the solution below and please give the feedback and p.pdfSolution find the solution below and please give the feedback and p.pdf
Solution find the solution below and please give the feedback and p.pdfannaimobiles
 
C BOTH of them .pdf
                     C BOTH of them                                   .pdf                     C BOTH of them                                   .pdf
C BOTH of them .pdfannaimobiles
 
public class DoubleArraySeq implements Cloneable {    Priva.pdf
public class DoubleArraySeq implements Cloneable {     Priva.pdfpublic class DoubleArraySeq implements Cloneable {     Priva.pdf
public class DoubleArraySeq implements Cloneable {    Priva.pdfannaimobiles
 
plese post the question clearly not able to understandSolution.pdf
plese post the question clearly not able to understandSolution.pdfplese post the question clearly not able to understandSolution.pdf
plese post the question clearly not able to understandSolution.pdfannaimobiles
 
Lymphatic system function is to produce T- helper cells and B- helpe.pdf
Lymphatic system function is to produce T- helper cells and B- helpe.pdfLymphatic system function is to produce T- helper cells and B- helpe.pdf
Lymphatic system function is to produce T- helper cells and B- helpe.pdfannaimobiles
 
b) 0.2 M HI HI is a strong acid, which can comple.pdf
                     b) 0.2 M HI HI is a strong acid, which can comple.pdf                     b) 0.2 M HI HI is a strong acid, which can comple.pdf
b) 0.2 M HI HI is a strong acid, which can comple.pdfannaimobiles
 
Integer version of the member functions are as belowint operate(in.pdf
Integer version of the member functions are as belowint operate(in.pdfInteger version of the member functions are as belowint operate(in.pdf
Integer version of the member functions are as belowint operate(in.pdfannaimobiles
 
I have done it under neatbeans IDE and just added ToggleGroup for gr.pdf
I have done it under neatbeans IDE and just added ToggleGroup for gr.pdfI have done it under neatbeans IDE and just added ToggleGroup for gr.pdf
I have done it under neatbeans IDE and just added ToggleGroup for gr.pdfannaimobiles
 

More from annaimobiles (20)

Hg2+ is more water soluble, methyl mercury HgCH3+.pdf
                     Hg2+ is more water soluble, methyl mercury HgCH3+.pdf                     Hg2+ is more water soluble, methyl mercury HgCH3+.pdf
Hg2+ is more water soluble, methyl mercury HgCH3+.pdf
 
HCl is a polar-covalent compound and also Acetoni.pdf
                     HCl is a polar-covalent compound and also Acetoni.pdf                     HCl is a polar-covalent compound and also Acetoni.pdf
HCl is a polar-covalent compound and also Acetoni.pdf
 
for a compound to be soluble in water 1. it shou.pdf
                     for a compound to be soluble in water  1. it shou.pdf                     for a compound to be soluble in water  1. it shou.pdf
for a compound to be soluble in water 1. it shou.pdf
 
e) II and III have the loest, equal stability .pdf
                     e) II and III have the loest, equal stability .pdf                     e) II and III have the loest, equal stability .pdf
e) II and III have the loest, equal stability .pdf
 
wait doingSolutionwait doing.pdf
wait doingSolutionwait doing.pdfwait doingSolutionwait doing.pdf
wait doingSolutionwait doing.pdf
 
TRUE The Reynolds number of falling 2.5 micrometer flyash particles .pdf
TRUE The Reynolds number of falling 2.5 micrometer flyash particles .pdfTRUE The Reynolds number of falling 2.5 micrometer flyash particles .pdf
TRUE The Reynolds number of falling 2.5 micrometer flyash particles .pdf
 
the electron rich molecule is a central Xe with 4 F singlebonded.pdf
the electron rich molecule is a central Xe with 4 F singlebonded.pdfthe electron rich molecule is a central Xe with 4 F singlebonded.pdf
the electron rich molecule is a central Xe with 4 F singlebonded.pdf
 
The given formulae themselves are explicit. Explicit means direct i..pdf
The given formulae themselves are explicit. Explicit means direct i..pdfThe given formulae themselves are explicit. Explicit means direct i..pdf
The given formulae themselves are explicit. Explicit means direct i..pdf
 
The answer is Meiosis, It occurs in human body at reporductive organ.pdf
The answer is Meiosis, It occurs in human body at reporductive organ.pdfThe answer is Meiosis, It occurs in human body at reporductive organ.pdf
The answer is Meiosis, It occurs in human body at reporductive organ.pdf
 
Structure of HemoglobinHemoglobin is a chromo protein and is found.pdf
Structure of HemoglobinHemoglobin is a chromo protein and is found.pdfStructure of HemoglobinHemoglobin is a chromo protein and is found.pdf
Structure of HemoglobinHemoglobin is a chromo protein and is found.pdf
 
SolutionSelection Sort after two iterations1 14 8 9 5 16 2 1.pdf
SolutionSelection Sort after two iterations1 14 8 9 5 16 2 1.pdfSolutionSelection Sort after two iterations1 14 8 9 5 16 2 1.pdf
SolutionSelection Sort after two iterations1 14 8 9 5 16 2 1.pdf
 
Starch agar consists of starch. The secretion of amylase enzyme by s.pdf
Starch agar consists of starch. The secretion of amylase enzyme by s.pdfStarch agar consists of starch. The secretion of amylase enzyme by s.pdf
Starch agar consists of starch. The secretion of amylase enzyme by s.pdf
 
Solution find the solution below and please give the feedback and p.pdf
Solution find the solution below and please give the feedback and p.pdfSolution find the solution below and please give the feedback and p.pdf
Solution find the solution below and please give the feedback and p.pdf
 
C BOTH of them .pdf
                     C BOTH of them                                   .pdf                     C BOTH of them                                   .pdf
C BOTH of them .pdf
 
public class DoubleArraySeq implements Cloneable {    Priva.pdf
public class DoubleArraySeq implements Cloneable {     Priva.pdfpublic class DoubleArraySeq implements Cloneable {     Priva.pdf
public class DoubleArraySeq implements Cloneable {    Priva.pdf
 
plese post the question clearly not able to understandSolution.pdf
plese post the question clearly not able to understandSolution.pdfplese post the question clearly not able to understandSolution.pdf
plese post the question clearly not able to understandSolution.pdf
 
Lymphatic system function is to produce T- helper cells and B- helpe.pdf
Lymphatic system function is to produce T- helper cells and B- helpe.pdfLymphatic system function is to produce T- helper cells and B- helpe.pdf
Lymphatic system function is to produce T- helper cells and B- helpe.pdf
 
b) 0.2 M HI HI is a strong acid, which can comple.pdf
                     b) 0.2 M HI HI is a strong acid, which can comple.pdf                     b) 0.2 M HI HI is a strong acid, which can comple.pdf
b) 0.2 M HI HI is a strong acid, which can comple.pdf
 
Integer version of the member functions are as belowint operate(in.pdf
Integer version of the member functions are as belowint operate(in.pdfInteger version of the member functions are as belowint operate(in.pdf
Integer version of the member functions are as belowint operate(in.pdf
 
I have done it under neatbeans IDE and just added ToggleGroup for gr.pdf
I have done it under neatbeans IDE and just added ToggleGroup for gr.pdfI have done it under neatbeans IDE and just added ToggleGroup for gr.pdf
I have done it under neatbeans IDE and just added ToggleGroup for gr.pdf
 

Recently uploaded

會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文中 央社
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17Celine George
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptxPoojaSen20
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxAdelaideRefugio
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code ExamplesPeter Brusilovsky
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxLimon Prince
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...Nguyen Thanh Tu Collection
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppCeline George
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...EduSkills OECD
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFVivekanand Anglo Vedic Academy
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnershipsexpandedwebsite
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...EADTU
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaEADTU
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project researchCaitlinCummins3
 
How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17Celine George
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxneillewis46
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjMohammed Sikander
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptNishitharanjan Rout
 

Recently uploaded (20)

會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptx
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDF
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 

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
  • 10. Status: Alive Peaple Saved: 100 Name :Superman Secret Identity:Clark Status: Alive Peaple Saved: 11