Ask Experts Questions for FREE Help !
Ask
    Lucyasks's Avatar
    Lucyasks Posts: 1, Reputation: 1
    New Member
     
    #1

    Apr 8, 2016, 01:39 PM
    How to demonstarte the idea of Polymorphism and inheritance in Java?
    Listen, I really need help with my assignment I've been working on it for hours and I'm just stuck!
    I don't think I understood the question fully and what's the ms is actually expecting me to output for her.
    I created the super class along with it's 2 sub classes and the main class for testing everything.
    1- But How do I make a condition for the value price to be greater than 0? 2- What does the Ms. mean by saying create a collection of three Food that are either FastFood or Pizza? 3- and how do I add objects to the array in the main class?

    This is the question:

    1. Create a class Food that has:

    a. Two private attributes • name which specifies the name of food • price, which specifies the price as a double value. The price can be any value, but must be checked to be greater than 0, in order to be valid.
    b. A constructor with two arguments that sets the values of both instance variables to those passed as parameters. Implement the class Food with all mentioned methods in addition to set and get methods.

    1. Derive a class FastFood that inherits from Food and has

    a. a private attribute type, which specifies whether the fast food is “Fresh” or “Hot”.
    b. a constructor that initializes all attributes Implement the class FastFood with all mentioned methods in addition to accessor and mutator methods.

    1. Derive a class Pizza that inherits from FastFood and has

    a. two private attributes • an array toppings of five String values • a boolean value for isSpicy that specifies whether the sauce is spicy (value 1) or not (value 0).
    a. a constructor that initializes all instance variables. The type of the FastFood should be set to “Hot” value.
    4.Write a test program that test the above classes demonstrating the idea of polymorphism. Let us say we want to set up a collection of three Food that are either FastFood or Pizza. Create an array of type Food and add objects to it. Next output the information about the chosen food using method toString().

    and this is my code so far:
    package food;

    public class Food {

    private double price;
    private String name;


    Food (double price, String name){

    this.price = price;
    this.name= name; } //consturctor


    Food() { } //consturctor


    public void setName(String foodName){

    name = foodName; }


    public String getName(){

    return name; }
    /* return local variable price, it returns the type
    of the variable that's why we need tp specify in the method it's a String*/


    public void setPrice(double foodPrice){

    price = foodPrice; }


    public double getPrice(){

    return price; }
    /* return local variable price, it returns the type
    of the variable that's why we need tp specify in the method it's a double*/

    public void output(){

    System.out.printf("The food is %s", getName());
    System.out.printf("The food is %f", getPrice()); }

    }


    //Super class
    //Note: if the getMethod returns nothing, then put it a void
    // %s to let the method know it's a String value
    // %d is for integers so instead I used %f which can be for both double and float valuesand for the sub class FastFood:
    package food;


    public class FastFood extends Food{

    private String foodtype;

    FastFood(double price, String name, String foodtype){

    super(price, name);
    this.foodtype = foodtype; }


    FastFood() { }

    public void setFoodType(String Foodtype){

    foodtype = Foodtype; }


    public String getFoodType(){

    return foodtype; }

    }

    //Sub class
    // "extends" --> Inherits fromsub class Pizza:
    /* Pizza Honor */

    package food;


    public class Pizza extends FastFood{

    private String[] toppings = new String[5];
    boolean isSpicy = true;


    Pizza(double price, String name){

    super(); } //constructor

    Pizza() { } //constructor

    public void setIsSpicy(){

    if ( isSpicy == true) {
    System.out.println("it's spicy"); }

    else {
    System.out.println("it's not spicy"); } }


    public void setToppings(){

    toppings[0] = "Cheese";
    toppings[1] = "Pepperoni";
    toppings[2] = "onion";
    toppings[3] = "olives";
    toppings[4] = "chicken";

    for (String s: toppings){

    System.out.println(s); }

    }



    }

    //Sub class
    // "extends" --> Inherits fromand finally this is my main class:
    package food;


    public class Test{

    public static void main(String[] args){

    Food foodObject = new Food(); //Creating an object for the Food class

    FastFood fastObject = new FastFood(); //Creating an object for the FastFood class

    Pizza pizzaObject = new Pizza(); //Creating an object for the Pizza class



    }

    }

    //Main class for testing

Check out some similar questions!

What all topics comes under advance java?what is framework how it is related to java? [ 0 Answers ]

I want to know about the topics related to advance java?and how the framework likke struts and hibernate realted to each other?what are there uses?

I am new to java GUI and multiple classes, I have knowledge of core java. I have [ 0 Answers ]

Sir, I am new to java GUI and multiple classes, I have knowledge of core java. I have to call a rest client from my net beans project give values to it and receive it's output in the same project. Please suggest me what I need to study and what to do.

Java netbeans program- Connection between java and mysql,How to conn [ 0 Answers ]

which I wrote from a book but when I run it, it shows exception please give me the code to connect jlistbox to database when I click on btn1. btn coding should search "name" in database and shows all the names in listbox my coding: here-- r1,r2,r3 are radio btns and l1,l2,l3 are labels,...

Using a blog to vent a good idea? Bad idea? Other ideas? Suggestions? [ 13 Answers ]

If you've seen my other question, then you know I've had some drama lately.. Well concerning that post I will say, things settled out. Well.. more of, she read my journal that morning while I took my regents test and found out that I thought I liked her. Today, we discussed that. She doesn't...

Rerouting pipes in 12 unit condo building? Good idea or bad idea [ 2 Answers ]

Is it a good idea to reroute pipes for a 12 unit building rather then take the chance of digging up concrete parking lot and plumber not finding where break in clay pipe is? If there is a break in the pipe at all how can you put a liner in if you can't see?? I personally think the plumber is lying...


View more questions Search
 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.