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:
- 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.
- 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.
- 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