TestingDocs.com
Testing Questions
  • Home
  • Automation
    • Selenium
    • TestNG
  • Tutorials
    • MySQL Tutorials
    • TestLink
    • Maven
    • Git
  • Flowcharts
    • Flowgorithm
    • Raptor

Java Programs

Write a java program to calculate the total earnings( Pet Clinic).

Java Programs

Problem Statement

A pet doctor treats dogs, cats and cows in his pet clinic. Write a simple java program to calculate his earnings in the clinic if he treats 25 dogs, 10 cats, 2 cows in a day. Use an objected oriented approach.

Treatment Menu: Fee

Dog          100$

Cat          250$

Cow         1250$

Solution:

Total Pet Doctor Earnings $:7500

Calculation = ( 25 * 100)  + ( 10 * 250 ) + ( 2 * 1250 )

Java Classes

public class PetDoctorEarnings {

 public static void main(String[] args){
 Dog dog = new Dog(25);
 Cat cat = new Cat(10);
 Cow cow = new Cow(2);
 PetClinic clinic = new PetClinic(3);
 clinic.addAnimal(dog,0);
 clinic.addAnimal(cat,1);
 clinic.addAnimal(cow,2);
 System.out.println("Total Pet Doctor Earnings $:" + clinic.calculateFee());

 }
}

 

Classes used Animal , Dog , Cat and Cow as shown in below picture.

 

Single inheritance

All derived classes have Animal as the super class. We have used single inheritance for the derived classes. Single inheritance is a mechanism that allows a class to only inherit from a single base class.

Single Inheritance

Dog

public class Dog extends Animal{
 int fee = 100;
 public Dog()
 {
 super();
 }

 public Dog(int number)
 {
 super(number);
 }

 public int getTreatmentFee() {
 return super.getNumber()*100;
 }
}

 

Cat

public class Cat extends Animal {
 int fee = 250;
 public Cat()
 {
 super();
 }

 public Cat( int number)
 {
 super(number);
 }

 public int getTreatmentFee() {
 return super.getNumber() * fee;
 }
}

 

Cow

public class Cow extends Animal {
 int fee = 1250;
 public Cow()
 {
 super();
 }

 public Cow(int number)
 {
 super(number);
 }

 public int getTreatmentFee() {
 return super.getNumber()*1250;
 }
}

 

Pet Clinic

public class PetClinic {

 private Animal[] animals;

 public PetClinic(int numAnimals) {
 animals = new Animal[numAnimals];
 }

 public void addAnimal(Animal op, int index)
 {
 animals[index] = op;
 }

 public Animal[] getAnimals()
 {
 return animals;
 }

 public int calculateFee()
 {
 int fee = 0;
 for (int i=0; i < animals.length; i++)
 fee += animals[i].getTreatmentFee();

 return fee;
 }
}

 

Further Enhancements

We can make further enhancements to the program. Consider designing an interface contract for the treatable animals.

Let’s name The interface as ITreatable.

public interface ITreatable {
public int getTreatmentFee();
}

Now all treatable animals can implement this interface.

Related Posts

Add Two Numbers Flow Chart

Java Programs /

Write a simple java program to add two numbers?

Fahrenheit To Celsius

Java Programs /

Java program to Convert Fahrenheit to Celsius

Java Programs /

Java program for area of triangle

Factorial Java Program using Recursion

Java Programs /

Factorial Java Program using Recursion

Area of Rectangle

Java Programs /

Write a Java program to calculate Area of Rectangle

‹ Write a java program for Bubble Sort?› Write a program to reverse an array in Java?

Recent Posts

  • How to Hide Taskbar on Windows 11
  • How to edit PATH variable on Windows 11?
  • Enable BitLocker Drive Encryption on Windows 11
  • How to lock Windows 11 PC
  • How to Add, Remove Widgets on Windows 11
  • How to Rename PC on Windows 11?
  • How to Shut down Windows 11 PC?
  • How to launch command prompt on Windows 11
  • Install Windows 11 Insider Preview on Virtual Machine
  • How to Create a Restore point in Windows 11

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version