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

In object-oriented programming, inheritance refers to the ability of an object to take on one or more characteristics from other classes of objects. The characteristics inherited are usually instance variables or member functions. An object that inherits these characteristics is known as a subclass. The object it inherits them from is known as a superclass. The details of how inheritance is implemented vary between languages, but the first language to implement it was Simula in 1967.

What is inheritance used for?

The purpose of inheritance is to consolidate and re-use code. For example, if the objects "car," "truck," and "motorcycle" are subclasses of vehicle, code applying to all of them can be consolidated into a vehicle superclass. The subclasses inherit this code and any future changes made to it, automatically.

Five types of inheritance

  • Single inheritance - Subclasses inherit characteristics from a single superclass.
  • Multiple inheritance - A subclass may have more than one superclass and inherit characteristics from all of them.
  • Multilevel inheritance - A subclass may have its own subclasses. In other words, a subclass of a superclass can itself be a superclass to other subclasses.
  • Hierarchical inheritance - A base class acts as the parent superclass to multiple levels of subclasses.
  • Hybrid inheritance - A combination of one or more of the other inheritance types.

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;

}

}


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