{"id":1393,"date":"2016-10-07T15:57:19","date_gmt":"2016-10-07T15:57:19","guid":{"rendered":"http:\/\/www.testingdocs.com\/questions\/?p=1393"},"modified":"2024-12-14T04:07:48","modified_gmt":"2024-12-14T04:07:48","slug":"write-a-java-program-to-calculate-the-total-earnings","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/write-a-java-program-to-calculate-the-total-earnings\/","title":{"rendered":"Pet Clinic total earnings Java program"},"content":{"rendered":"<h1>Pet Clinic total earnings Java program<\/h1>\n<h3>Problem Statement<\/h3>\n<p>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 object-oriented approach.<\/p>\n<p>Treatment Menu: Fee<\/p>\n<p>Dog\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 100$<\/p>\n<p>Cat\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 250$<\/p>\n<p>Cow\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1250$<\/p>\n<h3>Solution:<\/h3>\n<p><strong>Total Pet Doctor Earnings $:7500<\/strong><\/p>\n<p><strong>Calculation = ( 25 * 100)\u00a0 + ( 10 * 250 ) + ( 2 * 1250 )<\/strong><\/p>\n<h3>Java Classes<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">public class PetDoctorEarnings {\r\n\r\n    public static void  main(String[] args){\r\n        Dog dog = new Dog(25);\r\n        Cat cat = new Cat(10);\r\n        Cow cow = new Cow(2);\r\n        PetClinic clinic = new PetClinic(3);\r\n        clinic.addAnimal(dog,0);\r\n        clinic.addAnimal(cat,1);\r\n        clinic.addAnimal(cow,2);\r\n        System.out.println(\"Total Pet Doctor Earnings $:\" + clinic.calculateFee());\r\n\r\n    }\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Classes used Animal , Dog , Cat and Cow as shown in below picture.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1383\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Java-Inheritance-Example.png\" alt=\"\" width=\"1085\" height=\"573\" title=\"\"><\/p>\n<p>&nbsp;<\/p>\n<h3><strong>Single inheritance<\/strong><\/h3>\n<p>All derived classes have Animal as the super class. We have used single inheritance for the derived classes.\u00a0Single inheritance is a mechanism that allows a class to only inherit from a single base class.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1384\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Single-Inheritance.png\" alt=\"Single Inheritance\" width=\"872\" height=\"585\" title=\"\"><\/p>\n<h3><strong>Dog<\/strong><\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">public class Dog extends Animal{\r\n    int fee = 100;\r\n    public Dog()\r\n    {\r\n        super();\r\n    }\r\n\r\n    public Dog(int number)\r\n    {\r\n        super(number);\r\n    }\r\n\r\n    public int getTreatmentFee() {\r\n        return super.getNumber()*100;\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h3><strong>Cat<\/strong><\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">public class Cat extends Animal {\r\n    int fee = 250;\r\n    public Cat()\r\n    {\r\n        super();\r\n    }\r\n\r\n    public Cat( int number)\r\n    {\r\n        super(number);\r\n    }\r\n\r\n    public int getTreatmentFee() {\r\n        return  super.getNumber() * fee;\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h3><strong>Cow<\/strong><\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">public class Cow extends Animal {\r\n    int fee = 1250;\r\n    public Cow()\r\n    {\r\n        super();\r\n    }\r\n\r\n    public Cow(int number)\r\n    {\r\n        super(number);\r\n    }\r\n\r\n    public int getTreatmentFee() {\r\n        return super.getNumber()*1250;\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h3>Pet Clinic<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">public class PetClinic {\r\n\r\n    private Animal[] animals;\r\n\r\n    public PetClinic(int numAnimals) {\r\n        animals = new Animal[numAnimals];\r\n    }\r\n\r\n    public void addAnimal(Animal op, int index)\r\n    {\r\n        animals[index] = op;\r\n    }\r\n\r\n    public Animal[] getAnimals()\r\n    {\r\n        return animals;\r\n    }\r\n\r\n    public int calculateFee()\r\n    {\r\n        int fee = 0;\r\n        for (int i=0; i &lt; animals.length; i++)\r\n            fee += animals[i].getTreatmentFee();\r\n\r\n        return fee;\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h3>Further Enhancements<\/h3>\n<p>We can make further enhancements to the program. Consider designing an interface contract for the treatable animals.<\/p>\n<p>Let&#8217;s name The interface as ITreatable.<\/p>\n<p><strong>public interface ITreatable {<\/strong><br \/>\n<strong>public int getTreatmentFee();<\/strong><br \/>\n<strong>}<\/strong><\/p>\n<p>Now all treatable animals can implement this interface.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Pet Clinic total earnings Java program 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 object-oriented approach. Treatment Menu: Fee Dog\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 100$ Cat\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 250$ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[24],"class_list":["post-1393","post","type-post","status-publish","format-standard","hentry","category-java-programs","tag-java-programs","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1393","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=1393"}],"version-history":[{"count":6,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1393\/revisions"}],"predecessor-version":[{"id":26377,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1393\/revisions\/26377"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=1393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=1393"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=1393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}