SlideShare a Scribd company logo
1 of 11
Download to read offline
Animal.java
public abstract class Animal {
private int hunger;
private int happiness;
public Animal() {
this.hunger=100;
this.happiness=100;
}
public int getHunger() {
return hunger;
}
public void modifyHunger(int hunger) {
if(hunger>=0 && hunger<=100)
this.hunger = getHunger()+hunger;
else
new IllegalArgumentException();
}
public int getHappiness() {
return happiness;
}
public void modifyHappiness(int happiness) {
if(happiness>=0 && happiness<=100)
this.happiness =getHappiness()+ happiness;
else
new IllegalArgumentException();
}
@Override
public String toString() {
return "Hunger :"+hunger+" Happiness :"+happiness;
}
public abstract void eat(Food meal);
public abstract int sleep();
public abstract void clean();
}
_____________________________________________
Dolphin.java
public class Dolphin extends Animal {
public Dolphin() {
super();
}
@Override
public void eat(Food meal) {
if(meal.type.equals("grass"))
{
modifyHunger(40);
modifyHappiness(-10);
}
else if(meal.type.equals("fish"))
{
modifyHunger(100);
modifyHappiness(10);
}
else if(meal.type.equals("fruit"))
{
modifyHunger(100);
modifyHappiness(20);
}
else if(meal.type.equals("meat"))
{
modifyHunger(80);
}
else if(meal.type.equals("seeds"))
{
modifyHunger(20);
modifyHappiness(-30);
}
}
@Override
public int sleep() {
modifyHappiness(-30);
modifyHunger(-10);
return getHappiness()+getHunger();
}
@Override
public void clean() {
modifyHappiness(10);
}
public void swim()
{
modifyHappiness(30);
}
@Override
public String toString() {
return " Dolphin  HUNGER :"+getHunger()+" HAPPINESS :"+getHappiness();
}
}
_______________________________________________
Food.java
public class Food {
String type;
public Food(String type) {
this.type = type;
}
}
___________________________________________
Driver.java
public class Driver {
public static void main(String[] args) {
Dolphin d1=new Dolphin();
Food f1=new Food("grass");
d1.eat(f1);
System.out.println("After Eating Grass :"+d1.toString()+" ");
Dolphin d2=new Dolphin();
Food f2=new Food("fish");
d2.eat(f2);
System.out.println("After Eating Fish :"+d2.toString()+" ");
Dolphin d3=new Dolphin();
Food f3=new Food("fruit");
d3.eat(f3);
System.out.println("After Eating Fruit :"+d3.toString()+" ");
Dolphin d4=new Dolphin();
Food f4=new Food("meat");
d4.eat(f4);
System.out.println("After Eating Meat :"+d4.toString()+" ");
Dolphin d5=new Dolphin();
Food f5=new Food("seeds");
d5.eat(f5);
System.out.println("After Eating Seeds :"+d5.toString()+" ");
Dolphin d6=new Dolphin();
d6.sleep();
System.out.println("After Sleeping :"+d6.toString()+" ");
Dolphin d7=new Dolphin();
d7.clean();
System.out.println("After Cleaning :"+d7.toString()+" ");
Dolphin d8=new Dolphin();
d8.swim();
System.out.println("After Swiming :"+d8.toString()+" ");
}
}
______________________________________________
Output:
After Eating Grass :
Dolphin
HUNGER :140
HAPPINESS :100
After Eating Fish :
Dolphin
HUNGER :200
HAPPINESS :110
After Eating Fruit :
Dolphin
HUNGER :200
HAPPINESS :120
After Eating Meat :
Dolphin
HUNGER :180
HAPPINESS :100
After Eating Seeds :
Dolphin
HUNGER :120
HAPPINESS :100
100
100
After Sleeping :
Dolphin
HUNGER :100
HAPPINESS :100
After Cleaning :
Dolphin
HUNGER :100
HAPPINESS :110
After Swiming :
Dolphin
HUNGER :100
HAPPINESS :130
_____________________________________
Solution
Animal.java
public abstract class Animal {
private int hunger;
private int happiness;
public Animal() {
this.hunger=100;
this.happiness=100;
}
public int getHunger() {
return hunger;
}
public void modifyHunger(int hunger) {
if(hunger>=0 && hunger<=100)
this.hunger = getHunger()+hunger;
else
new IllegalArgumentException();
}
public int getHappiness() {
return happiness;
}
public void modifyHappiness(int happiness) {
if(happiness>=0 && happiness<=100)
this.happiness =getHappiness()+ happiness;
else
new IllegalArgumentException();
}
@Override
public String toString() {
return "Hunger :"+hunger+" Happiness :"+happiness;
}
public abstract void eat(Food meal);
public abstract int sleep();
public abstract void clean();
}
_____________________________________________
Dolphin.java
public class Dolphin extends Animal {
public Dolphin() {
super();
}
@Override
public void eat(Food meal) {
if(meal.type.equals("grass"))
{
modifyHunger(40);
modifyHappiness(-10);
}
else if(meal.type.equals("fish"))
{
modifyHunger(100);
modifyHappiness(10);
}
else if(meal.type.equals("fruit"))
{
modifyHunger(100);
modifyHappiness(20);
}
else if(meal.type.equals("meat"))
{
modifyHunger(80);
}
else if(meal.type.equals("seeds"))
{
modifyHunger(20);
modifyHappiness(-30);
}
}
@Override
public int sleep() {
modifyHappiness(-30);
modifyHunger(-10);
return getHappiness()+getHunger();
}
@Override
public void clean() {
modifyHappiness(10);
}
public void swim()
{
modifyHappiness(30);
}
@Override
public String toString() {
return " Dolphin  HUNGER :"+getHunger()+" HAPPINESS :"+getHappiness();
}
}
_______________________________________________
Food.java
public class Food {
String type;
public Food(String type) {
this.type = type;
}
}
___________________________________________
Driver.java
public class Driver {
public static void main(String[] args) {
Dolphin d1=new Dolphin();
Food f1=new Food("grass");
d1.eat(f1);
System.out.println("After Eating Grass :"+d1.toString()+" ");
Dolphin d2=new Dolphin();
Food f2=new Food("fish");
d2.eat(f2);
System.out.println("After Eating Fish :"+d2.toString()+" ");
Dolphin d3=new Dolphin();
Food f3=new Food("fruit");
d3.eat(f3);
System.out.println("After Eating Fruit :"+d3.toString()+" ");
Dolphin d4=new Dolphin();
Food f4=new Food("meat");
d4.eat(f4);
System.out.println("After Eating Meat :"+d4.toString()+" ");
Dolphin d5=new Dolphin();
Food f5=new Food("seeds");
d5.eat(f5);
System.out.println("After Eating Seeds :"+d5.toString()+" ");
Dolphin d6=new Dolphin();
d6.sleep();
System.out.println("After Sleeping :"+d6.toString()+" ");
Dolphin d7=new Dolphin();
d7.clean();
System.out.println("After Cleaning :"+d7.toString()+" ");
Dolphin d8=new Dolphin();
d8.swim();
System.out.println("After Swiming :"+d8.toString()+" ");
}
}
______________________________________________
Output:
After Eating Grass :
Dolphin
HUNGER :140
HAPPINESS :100
After Eating Fish :
Dolphin
HUNGER :200
HAPPINESS :110
After Eating Fruit :
Dolphin
HUNGER :200
HAPPINESS :120
After Eating Meat :
Dolphin
HUNGER :180
HAPPINESS :100
After Eating Seeds :
Dolphin
HUNGER :120
HAPPINESS :100
100
100
After Sleeping :
Dolphin
HUNGER :100
HAPPINESS :100
After Cleaning :
Dolphin
HUNGER :100
HAPPINESS :110
After Swiming :
Dolphin
HUNGER :100
HAPPINESS :130
_____________________________________

More Related Content

More from mukhtaransarcloth

The balanced equation is2 H2O2 = 2 H2O + O2SolutionThe bal.pdf
The balanced equation is2 H2O2 = 2 H2O + O2SolutionThe bal.pdfThe balanced equation is2 H2O2 = 2 H2O + O2SolutionThe bal.pdf
The balanced equation is2 H2O2 = 2 H2O + O2SolutionThe bal.pdfmukhtaransarcloth
 
Tay-Sachs disease is caused by a mutation (abnormal change) in the g.pdf
Tay-Sachs disease is caused by a mutation (abnormal change) in the g.pdfTay-Sachs disease is caused by a mutation (abnormal change) in the g.pdf
Tay-Sachs disease is caused by a mutation (abnormal change) in the g.pdfmukhtaransarcloth
 
E.) is less acidic in other A ,B triple bond an.pdf
                     E.) is less acidic   in other A ,B triple bond an.pdf                     E.) is less acidic   in other A ,B triple bond an.pdf
E.) is less acidic in other A ,B triple bond an.pdfmukhtaransarcloth
 
Solution when user sending the email, then user should select eithe.pdf
Solution when user sending the email, then user should select eithe.pdfSolution when user sending the email, then user should select eithe.pdf
Solution when user sending the email, then user should select eithe.pdfmukhtaransarcloth
 
publicclass Date {privatestatic String DATE_SEPARATOR = ;pr.pdf
publicclass Date {privatestatic String DATE_SEPARATOR = ;pr.pdfpublicclass Date {privatestatic String DATE_SEPARATOR = ;pr.pdf
publicclass Date {privatestatic String DATE_SEPARATOR = ;pr.pdfmukhtaransarcloth
 
please give me points as nobody ecen noticed the qusetion as it is u.pdf
please give me points as nobody ecen noticed the qusetion as it is u.pdfplease give me points as nobody ecen noticed the qusetion as it is u.pdf
please give me points as nobody ecen noticed the qusetion as it is u.pdfmukhtaransarcloth
 
Part-IQuestion 1. What is Dr. Warren’s hypothesis regarding the ba.pdf
Part-IQuestion 1. What is Dr. Warren’s hypothesis regarding the ba.pdfPart-IQuestion 1. What is Dr. Warren’s hypothesis regarding the ba.pdf
Part-IQuestion 1. What is Dr. Warren’s hypothesis regarding the ba.pdfmukhtaransarcloth
 
Peru is not a part of the Southern South America.SolutionPeru .pdf
Peru is not a part of the Southern South America.SolutionPeru .pdfPeru is not a part of the Southern South America.SolutionPeru .pdf
Peru is not a part of the Southern South America.SolutionPeru .pdfmukhtaransarcloth
 
Combustion of glucose is respiration. The equatio.pdf
                     Combustion of glucose is respiration. The equatio.pdf                     Combustion of glucose is respiration. The equatio.pdf
Combustion of glucose is respiration. The equatio.pdfmukhtaransarcloth
 
in truss one...the forces in the member BC and DE have zero.in tru.pdf
in truss one...the forces in the member BC and DE have zero.in tru.pdfin truss one...the forces in the member BC and DE have zero.in tru.pdf
in truss one...the forces in the member BC and DE have zero.in tru.pdfmukhtaransarcloth
 
iam giving you entire process of  forensc duplication;the response.pdf
iam giving you entire process of  forensc duplication;the response.pdfiam giving you entire process of  forensc duplication;the response.pdf
iam giving you entire process of  forensc duplication;the response.pdfmukhtaransarcloth
 
How does traffic analysis work Internet data packets have two parts.pdf
How does traffic analysis work Internet data packets have two parts.pdfHow does traffic analysis work Internet data packets have two parts.pdf
How does traffic analysis work Internet data packets have two parts.pdfmukhtaransarcloth
 
Br2 is the answer. note for nonpolar molecules,.pdf
                     Br2 is the answer.  note for nonpolar molecules,.pdf                     Br2 is the answer.  note for nonpolar molecules,.pdf
Br2 is the answer. note for nonpolar molecules,.pdfmukhtaransarcloth
 
Because leucine is hydrophobic and does not have .pdf
                     Because leucine is hydrophobic and does not have .pdf                     Because leucine is hydrophobic and does not have .pdf
Because leucine is hydrophobic and does not have .pdfmukhtaransarcloth
 
Designing affective Human Computer-Interfaces such as Embodied Conve.pdf
Designing affective Human Computer-Interfaces such as Embodied Conve.pdfDesigning affective Human Computer-Interfaces such as Embodied Conve.pdf
Designing affective Human Computer-Interfaces such as Embodied Conve.pdfmukhtaransarcloth
 
Dispersal of populationindividuals is described as movement of indi.pdf
Dispersal of populationindividuals is described as movement of indi.pdfDispersal of populationindividuals is described as movement of indi.pdf
Dispersal of populationindividuals is described as movement of indi.pdfmukhtaransarcloth
 
Decreases..SolutionDecreases...pdf
Decreases..SolutionDecreases...pdfDecreases..SolutionDecreases...pdf
Decreases..SolutionDecreases...pdfmukhtaransarcloth
 
ConversionDemo.javaimport java.util.;package for scanner class.pdf
ConversionDemo.javaimport java.util.;package for scanner class.pdfConversionDemo.javaimport java.util.;package for scanner class.pdf
ConversionDemo.javaimport java.util.;package for scanner class.pdfmukhtaransarcloth
 
C. Yes, YN + NY is greater than or equal to 30.SolutionC. Yes,.pdf
C. Yes, YN + NY is greater than or equal to 30.SolutionC. Yes,.pdfC. Yes, YN + NY is greater than or equal to 30.SolutionC. Yes,.pdf
C. Yes, YN + NY is greater than or equal to 30.SolutionC. Yes,.pdfmukhtaransarcloth
 

More from mukhtaransarcloth (20)

The balanced equation is2 H2O2 = 2 H2O + O2SolutionThe bal.pdf
The balanced equation is2 H2O2 = 2 H2O + O2SolutionThe bal.pdfThe balanced equation is2 H2O2 = 2 H2O + O2SolutionThe bal.pdf
The balanced equation is2 H2O2 = 2 H2O + O2SolutionThe bal.pdf
 
Tay-Sachs disease is caused by a mutation (abnormal change) in the g.pdf
Tay-Sachs disease is caused by a mutation (abnormal change) in the g.pdfTay-Sachs disease is caused by a mutation (abnormal change) in the g.pdf
Tay-Sachs disease is caused by a mutation (abnormal change) in the g.pdf
 
E.) is less acidic in other A ,B triple bond an.pdf
                     E.) is less acidic   in other A ,B triple bond an.pdf                     E.) is less acidic   in other A ,B triple bond an.pdf
E.) is less acidic in other A ,B triple bond an.pdf
 
Solution when user sending the email, then user should select eithe.pdf
Solution when user sending the email, then user should select eithe.pdfSolution when user sending the email, then user should select eithe.pdf
Solution when user sending the email, then user should select eithe.pdf
 
publicclass Date {privatestatic String DATE_SEPARATOR = ;pr.pdf
publicclass Date {privatestatic String DATE_SEPARATOR = ;pr.pdfpublicclass Date {privatestatic String DATE_SEPARATOR = ;pr.pdf
publicclass Date {privatestatic String DATE_SEPARATOR = ;pr.pdf
 
please give me points as nobody ecen noticed the qusetion as it is u.pdf
please give me points as nobody ecen noticed the qusetion as it is u.pdfplease give me points as nobody ecen noticed the qusetion as it is u.pdf
please give me points as nobody ecen noticed the qusetion as it is u.pdf
 
covalent bond .pdf
                     covalent bond                                    .pdf                     covalent bond                                    .pdf
covalent bond .pdf
 
Part-IQuestion 1. What is Dr. Warren’s hypothesis regarding the ba.pdf
Part-IQuestion 1. What is Dr. Warren’s hypothesis regarding the ba.pdfPart-IQuestion 1. What is Dr. Warren’s hypothesis regarding the ba.pdf
Part-IQuestion 1. What is Dr. Warren’s hypothesis regarding the ba.pdf
 
Peru is not a part of the Southern South America.SolutionPeru .pdf
Peru is not a part of the Southern South America.SolutionPeru .pdfPeru is not a part of the Southern South America.SolutionPeru .pdf
Peru is not a part of the Southern South America.SolutionPeru .pdf
 
Combustion of glucose is respiration. The equatio.pdf
                     Combustion of glucose is respiration. The equatio.pdf                     Combustion of glucose is respiration. The equatio.pdf
Combustion of glucose is respiration. The equatio.pdf
 
in truss one...the forces in the member BC and DE have zero.in tru.pdf
in truss one...the forces in the member BC and DE have zero.in tru.pdfin truss one...the forces in the member BC and DE have zero.in tru.pdf
in truss one...the forces in the member BC and DE have zero.in tru.pdf
 
iam giving you entire process of  forensc duplication;the response.pdf
iam giving you entire process of  forensc duplication;the response.pdfiam giving you entire process of  forensc duplication;the response.pdf
iam giving you entire process of  forensc duplication;the response.pdf
 
How does traffic analysis work Internet data packets have two parts.pdf
How does traffic analysis work Internet data packets have two parts.pdfHow does traffic analysis work Internet data packets have two parts.pdf
How does traffic analysis work Internet data packets have two parts.pdf
 
Br2 is the answer. note for nonpolar molecules,.pdf
                     Br2 is the answer.  note for nonpolar molecules,.pdf                     Br2 is the answer.  note for nonpolar molecules,.pdf
Br2 is the answer. note for nonpolar molecules,.pdf
 
Because leucine is hydrophobic and does not have .pdf
                     Because leucine is hydrophobic and does not have .pdf                     Because leucine is hydrophobic and does not have .pdf
Because leucine is hydrophobic and does not have .pdf
 
Designing affective Human Computer-Interfaces such as Embodied Conve.pdf
Designing affective Human Computer-Interfaces such as Embodied Conve.pdfDesigning affective Human Computer-Interfaces such as Embodied Conve.pdf
Designing affective Human Computer-Interfaces such as Embodied Conve.pdf
 
Dispersal of populationindividuals is described as movement of indi.pdf
Dispersal of populationindividuals is described as movement of indi.pdfDispersal of populationindividuals is described as movement of indi.pdf
Dispersal of populationindividuals is described as movement of indi.pdf
 
Decreases..SolutionDecreases...pdf
Decreases..SolutionDecreases...pdfDecreases..SolutionDecreases...pdf
Decreases..SolutionDecreases...pdf
 
ConversionDemo.javaimport java.util.;package for scanner class.pdf
ConversionDemo.javaimport java.util.;package for scanner class.pdfConversionDemo.javaimport java.util.;package for scanner class.pdf
ConversionDemo.javaimport java.util.;package for scanner class.pdf
 
C. Yes, YN + NY is greater than or equal to 30.SolutionC. Yes,.pdf
C. Yes, YN + NY is greater than or equal to 30.SolutionC. Yes,.pdfC. Yes, YN + NY is greater than or equal to 30.SolutionC. Yes,.pdf
C. Yes, YN + NY is greater than or equal to 30.SolutionC. Yes,.pdf
 

Recently uploaded

8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital ManagementMBA Assignment Experts
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi RajagopalEADTU
 
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MysoreMuleSoftMeetup
 
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
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportDenish Jangid
 
Rich Dad Poor Dad ( PDFDrive.com )--.pdf
Rich Dad Poor Dad ( PDFDrive.com )--.pdfRich Dad Poor Dad ( PDFDrive.com )--.pdf
Rich Dad Poor Dad ( PDFDrive.com )--.pdfJerry Chew
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...Gary Wood
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024Borja Sotomayor
 
The Liver & Gallbladder (Anatomy & Physiology).pptx
The Liver &  Gallbladder (Anatomy & Physiology).pptxThe Liver &  Gallbladder (Anatomy & Physiology).pptx
The Liver & Gallbladder (Anatomy & Physiology).pptxVishal Singh
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxMarlene Maheu
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjMohammed Sikander
 
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
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code ExamplesPeter Brusilovsky
 
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
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSAnaAcapella
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 
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
 
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
 

Recently uploaded (20)

8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
 
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...
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
Rich Dad Poor Dad ( PDFDrive.com )--.pdf
Rich Dad Poor Dad ( PDFDrive.com )--.pdfRich Dad Poor Dad ( PDFDrive.com )--.pdf
Rich Dad Poor Dad ( PDFDrive.com )--.pdf
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
The Liver & Gallbladder (Anatomy & Physiology).pptx
The Liver &  Gallbladder (Anatomy & Physiology).pptxThe Liver &  Gallbladder (Anatomy & Physiology).pptx
The Liver & Gallbladder (Anatomy & Physiology).pptx
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptx
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
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...
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
 
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
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
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
 
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
 

Animal.javapublic abstract class Animal {    private int hunger;.pdf

  • 1. Animal.java public abstract class Animal { private int hunger; private int happiness; public Animal() { this.hunger=100; this.happiness=100; } public int getHunger() { return hunger; } public void modifyHunger(int hunger) { if(hunger>=0 && hunger<=100) this.hunger = getHunger()+hunger; else new IllegalArgumentException(); } public int getHappiness() { return happiness; } public void modifyHappiness(int happiness) { if(happiness>=0 && happiness<=100) this.happiness =getHappiness()+ happiness; else new IllegalArgumentException(); } @Override public String toString() { return "Hunger :"+hunger+" Happiness :"+happiness; } public abstract void eat(Food meal); public abstract int sleep(); public abstract void clean();
  • 2. } _____________________________________________ Dolphin.java public class Dolphin extends Animal { public Dolphin() { super(); } @Override public void eat(Food meal) { if(meal.type.equals("grass")) { modifyHunger(40); modifyHappiness(-10); } else if(meal.type.equals("fish")) { modifyHunger(100); modifyHappiness(10); } else if(meal.type.equals("fruit")) { modifyHunger(100); modifyHappiness(20); } else if(meal.type.equals("meat")) { modifyHunger(80); } else if(meal.type.equals("seeds")) { modifyHunger(20); modifyHappiness(-30);
  • 3. } } @Override public int sleep() { modifyHappiness(-30); modifyHunger(-10); return getHappiness()+getHunger(); } @Override public void clean() { modifyHappiness(10); } public void swim() { modifyHappiness(30); } @Override public String toString() { return " Dolphin HUNGER :"+getHunger()+" HAPPINESS :"+getHappiness(); } } _______________________________________________ Food.java public class Food { String type; public Food(String type) { this.type = type; } } ___________________________________________ Driver.java public class Driver { public static void main(String[] args) { Dolphin d1=new Dolphin(); Food f1=new Food("grass");
  • 4. d1.eat(f1); System.out.println("After Eating Grass :"+d1.toString()+" "); Dolphin d2=new Dolphin(); Food f2=new Food("fish"); d2.eat(f2); System.out.println("After Eating Fish :"+d2.toString()+" "); Dolphin d3=new Dolphin(); Food f3=new Food("fruit"); d3.eat(f3); System.out.println("After Eating Fruit :"+d3.toString()+" "); Dolphin d4=new Dolphin(); Food f4=new Food("meat"); d4.eat(f4); System.out.println("After Eating Meat :"+d4.toString()+" "); Dolphin d5=new Dolphin(); Food f5=new Food("seeds"); d5.eat(f5); System.out.println("After Eating Seeds :"+d5.toString()+" "); Dolphin d6=new Dolphin(); d6.sleep(); System.out.println("After Sleeping :"+d6.toString()+" "); Dolphin d7=new Dolphin(); d7.clean(); System.out.println("After Cleaning :"+d7.toString()+" "); Dolphin d8=new Dolphin(); d8.swim(); System.out.println("After Swiming :"+d8.toString()+" "); } }
  • 5. ______________________________________________ Output: After Eating Grass : Dolphin HUNGER :140 HAPPINESS :100 After Eating Fish : Dolphin HUNGER :200 HAPPINESS :110 After Eating Fruit : Dolphin HUNGER :200 HAPPINESS :120 After Eating Meat : Dolphin HUNGER :180 HAPPINESS :100 After Eating Seeds : Dolphin HUNGER :120 HAPPINESS :100 100 100 After Sleeping : Dolphin HUNGER :100 HAPPINESS :100 After Cleaning : Dolphin HUNGER :100 HAPPINESS :110 After Swiming : Dolphin HUNGER :100 HAPPINESS :130
  • 6. _____________________________________ Solution Animal.java public abstract class Animal { private int hunger; private int happiness; public Animal() { this.hunger=100; this.happiness=100; } public int getHunger() { return hunger; } public void modifyHunger(int hunger) { if(hunger>=0 && hunger<=100) this.hunger = getHunger()+hunger; else new IllegalArgumentException(); } public int getHappiness() { return happiness; } public void modifyHappiness(int happiness) { if(happiness>=0 && happiness<=100) this.happiness =getHappiness()+ happiness; else new IllegalArgumentException(); } @Override public String toString() { return "Hunger :"+hunger+" Happiness :"+happiness; } public abstract void eat(Food meal);
  • 7. public abstract int sleep(); public abstract void clean(); } _____________________________________________ Dolphin.java public class Dolphin extends Animal { public Dolphin() { super(); } @Override public void eat(Food meal) { if(meal.type.equals("grass")) { modifyHunger(40); modifyHappiness(-10); } else if(meal.type.equals("fish")) { modifyHunger(100); modifyHappiness(10); } else if(meal.type.equals("fruit")) { modifyHunger(100); modifyHappiness(20); } else if(meal.type.equals("meat")) { modifyHunger(80); } else if(meal.type.equals("seeds"))
  • 8. { modifyHunger(20); modifyHappiness(-30); } } @Override public int sleep() { modifyHappiness(-30); modifyHunger(-10); return getHappiness()+getHunger(); } @Override public void clean() { modifyHappiness(10); } public void swim() { modifyHappiness(30); } @Override public String toString() { return " Dolphin HUNGER :"+getHunger()+" HAPPINESS :"+getHappiness(); } } _______________________________________________ Food.java public class Food { String type; public Food(String type) { this.type = type; } } ___________________________________________ Driver.java public class Driver {
  • 9. public static void main(String[] args) { Dolphin d1=new Dolphin(); Food f1=new Food("grass"); d1.eat(f1); System.out.println("After Eating Grass :"+d1.toString()+" "); Dolphin d2=new Dolphin(); Food f2=new Food("fish"); d2.eat(f2); System.out.println("After Eating Fish :"+d2.toString()+" "); Dolphin d3=new Dolphin(); Food f3=new Food("fruit"); d3.eat(f3); System.out.println("After Eating Fruit :"+d3.toString()+" "); Dolphin d4=new Dolphin(); Food f4=new Food("meat"); d4.eat(f4); System.out.println("After Eating Meat :"+d4.toString()+" "); Dolphin d5=new Dolphin(); Food f5=new Food("seeds"); d5.eat(f5); System.out.println("After Eating Seeds :"+d5.toString()+" "); Dolphin d6=new Dolphin(); d6.sleep(); System.out.println("After Sleeping :"+d6.toString()+" "); Dolphin d7=new Dolphin(); d7.clean(); System.out.println("After Cleaning :"+d7.toString()+" "); Dolphin d8=new Dolphin(); d8.swim();
  • 10. System.out.println("After Swiming :"+d8.toString()+" "); } } ______________________________________________ Output: After Eating Grass : Dolphin HUNGER :140 HAPPINESS :100 After Eating Fish : Dolphin HUNGER :200 HAPPINESS :110 After Eating Fruit : Dolphin HUNGER :200 HAPPINESS :120 After Eating Meat : Dolphin HUNGER :180 HAPPINESS :100 After Eating Seeds : Dolphin HUNGER :120 HAPPINESS :100 100 100 After Sleeping : Dolphin HUNGER :100 HAPPINESS :100 After Cleaning : Dolphin HUNGER :100 HAPPINESS :110 After Swiming :