Question

In: Computer Science

Inheritance - What is inheritance - Answer your own description in Readme.txt Based on Hamburger project,...

Inheritance - What is inheritance - Answer your own description in Readme.txt

Based on Hamburger project, you will create a package about Pizza.

In your Readme.txt, write how you make your Pizza package differently from the Hamburger package and also explain how inheritance work with your Pizza package.

package Hamburger;

/**
* Inheritance challenge – Hamburger place (Main, Hamburger, two other Burger type class)
* Hamburger class should have name, bread roll type, meat, and up to 4 additional
* additions(e.g. lettuce, tomato, carrot, etc)
* to select to be added to the burger. Each item will be charged an additional
*/
public class Hamburger {
private String name;
//meat, price, breadRollType
private String meat;
private double price;
private String breadRollType;

private String additionName1;
private double additionPrice1;

private String additionName2;
private double additionPrice2;

private String additionName3;
private double additionPrice3;

private String additionName4;
private double additionPrice4;

public Hamburger(String name, String meat, double price, String breadRollType) {
this.name = name;
this.meat = meat;
this.price = price;
this.breadRollType = breadRollType;
}

public void addHamburgerAddition1(String name, double price){
this.additionName1 = name;
this.additionPrice1 = price;
}

public void addHamburgerAddition2(String name, double price){
this.additionName2 = name;
this.additionPrice2 = price;
}

public void addHamburgerAddition3(String name, double price){
this.additionName3 = name;
this.additionPrice3 = price;
}

public void addHamburgerAddition4(String name, double price){
this.additionName4 = name;
this.additionPrice4 = price;
}


public double hamberPriceTotal(){

double hamburgerPrice = this.price;

System.out.println(this.name + " hambuger on a " + this.breadRollType + " roll with " + this.meat + "'s price is " + this.price);

if(this.additionName1 != null){
hamburgerPrice += this.additionPrice1;
System.out.println("Added " + this.additionName1 + " for an extra " + this.additionPrice1);
}

if(this.additionName2 != null){
hamburgerPrice += this.additionPrice2;
System.out.println("Added " + this.additionName2 + " for an extra " + this.additionPrice2);
}

if(this.additionName3 != null){
hamburgerPrice += this.additionPrice1;
System.out.println("Added " + this.additionName3 + " for an extra " + this.additionPrice3);
}

if(this.additionName4 != null){
hamburgerPrice += this.additionPrice4;
System.out.println("Added " + this.additionName4 + " for an extra " + this.additionPrice4);
}

return hamburgerPrice;

}

}

Solutions

Expert Solution

Inheritance can be defined as the process where one class acquires(inherits) the properties or attributes(methods and fields) of another. With the use of inheritance the information is made manageable in a hierarchical order.

The class which inherits the properties of other is known as subclass (derived class, child class) and the class whose properties are inherited is known as superclass (base class, parent class).

In the Pizza class we extend a separate class addition which has the variables additionName and additionPrice. These variables are inturn used by the pizza class when required.

addition.java

public class addition {

   private String additionName;
   private double additionPrice;
   public addition(String additionName, double additionPrice) {
       super();
       this.additionName = additionName;
       this.additionPrice = additionPrice;
   }
   public addition() {
       super();
   }
   public String getAdditionName() {
       return additionName;
   }
   public void setAdditionName(String additionName) {
       this.additionName = additionName;
   }
   public double getAdditionPrice() {
       return additionPrice;
   }
   public void setAdditionPrice(double additionPrice) {
       this.additionPrice = additionPrice;
   }
}

Pizza.java

public class Pizza extends addition{

   private String name;
   //meat, price, breadRollType
   private String meat;
   private double price;
   private String breadRollType;
  
   public Pizza(String name, String meat, double price, String breadRollType) {
   this.name = name;
   this.meat = meat;
   this.price = price;
   this.breadRollType = breadRollType;
   }

   public Pizza() {}


   public double hamberPriceTotal(){

   double PizzaPrice = this.price;
   Pizza pizza = new Pizza();
   System.out.println(this.name + " hambuger on a " + this.breadRollType + " roll with " + this.meat + "'s price is " + this.price);

   if(pizza.getAdditionName() != null){
   PizzaPrice += pizza.getAdditionPrice();
   System.out.println("Added " + pizza.getAdditionName() + " for an extra " + pizza.getAdditionPrice());
   }

   return PizzaPrice;

   }

}

This shows the property of inheritance and how its used in java. Inheritance ultimately makes the code more flexible and robust and also makes it easier for the code to be reused in any other package.


Related Solutions

Inheritance - What is inheritance - Answer your own description in Readme.txt Based on Hamburger project,...
Inheritance - What is inheritance - Answer your own description in Readme.txt Based on Hamburger project, you will create a package about Pizza. In your Readme.txt, write how you make your Pizza package differently from the Hamburger package and also explain how inheritance work with your Pizza package. package Hamburger; /** * Inheritance challenge – Hamburger place (Main, Hamburger, two other Burger type class) * Hamburger class should have name, bread roll type, meat, and up to 4 additional *...
Inheritance - What is inheritance - Answer your own description in Readme.txt Based on Hamburger project,...
Inheritance - What is inheritance - Answer your own description in Readme.txt Based on Hamburger project, you will create a package about Pizza. In your Readme.txt, write how you make your Pizza package differently from the Hamburger package and also explain how inheritance work with your Pizza package. package Hamburger; /** * Inheritance challenge – Hamburger place (Main, Hamburger, two other Burger type class) * Hamburger class should have name, bread roll type, meat, and up to 4 additional *...
You own and operate a hamburger. Each year, you receive revenue of $400,000 from your hamburger...
You own and operate a hamburger. Each year, you receive revenue of $400,000 from your hamburger and associated food sales, and it costs you $200,000 for the food. In addition, you pay $80,000 for electricity, taxes, and other expenses per year. Instead of running the hamburger, you could become a management consultant and receive a yearly salary of $100,000. A large clothing retail chain wants to expand and offers to rent the store from you for $40,000 per year. How...
Assignment One: Description of Your Individual Entrepreneurial Project Please describe your own individual project which may...
Assignment One: Description of Your Individual Entrepreneurial Project Please describe your own individual project which may be unique or you had been thinking about since it has invented or innovative ideas. Need to include description of the product, service, or idea you intend to do. The competitive advantage of your output and what make you think it is sustainable? The estimated cost of your project and some financial expectations. You may include any other details that you think it is...
Based on your approved project proposal develop the following sections: Organizational Description - this section will...
Based on your approved project proposal develop the following sections: Organizational Description - this section will provide an overview of the organization. This overview will include a summary of: 1- Potential population characteristics (i.e., majority aboriginal populations, etc.). 2- Potential challenges that you can identify for this organization (i.e., access to services due to geographic location, etc.) in which technology can play a significant role. 3- Description of the technological need(s) that this organization is required to address. 4- Proposal...
1. Read each question. 2. Answer based upon your knowledge of the chapter and your own...
1. Read each question. 2. Answer based upon your knowledge of the chapter and your own experience. 3. Indicate by number (1 or 2) which question you are answering first. 4. Proofread your answer--demonstrate your best writing ability. CHAPTER 12 Effective Business Presentations 1. What role do visuals play in business presentations? 2. The four Ps: Planning, preparing, practicing, and presenting, are very important to presentations. Explain how you understand each of them. Is one more important than the others?
Based on the five case studies, answer the questions in your own words for each case...
Based on the five case studies, answer the questions in your own words for each case study, using complete sentences, and providing examples, if applicable. Case Study 1 During an appendectomy, the patient, a 16-year-old male, exhibits decreased oxygen saturation, hypotension, decreased breath sounds, and increased airway pressures during ventilation. What is most likely happening with this patient? What should you do as a surgical tech in the scrubbed role? What will the anesthesia provider most likely do? Case Study...
What is the definition of Project Management? Using your own example, discuss project scheduling with the...
What is the definition of Project Management? Using your own example, discuss project scheduling with the critical path method (CPM).
do mendelian principles of inheritance apply to humans? Explain your answer
do mendelian principles of inheritance apply to humans? Explain your answer
Which is the BEST description of project scope? All of the features and deliverables your project...
Which is the BEST description of project scope? All of the features and deliverables your project will deliver All of the products your project will make All of the people involved in your project All of the work you will do to build the product
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT