Java

OOP GUIDE / WORK

 

FOOD SUPERCLASS 2 SOLUTIONS

 

 

TASK – PART 1 – SETUP – FOOD CLASS

 

No solution required.

 

 

TASK – PART 2 – SANDWHICH CLASS

 

Here is my solution:

 

public class Sandwhich extends Food

{

     private String breadType;

     private String toppings;

     private String spreads;

    

     public Sandwhich (String n, int a, String u, String f, String bt, String t, String s)

     {

           super(n, a, u, f);

           breadType = bt;

           toppings = t;

           spreads = s;

     }

    

     public String toString()

     {

           return name + " on " + breadType + " bread";

     }

}

 

 

 

TASK – PART 3 – SANDWHICH TESTER

Here is my solution:

 

public class SandwhichTester

{

     public static void main(String[] args)

     {

           Sandwhich s1 = new Sandwhich("Club house", 4, "triangles", "savoury", "sourdough", "bacon, tomatoes, turkey & lettuce", "mayo");

           System.out.println(s1);

           System.out.println("*****");

           System.out.println("LUNCH MENU");

          

           Food[] lunch = new Food[5];

           lunch[0] = s1;

           lunch[1] = new Sandwhich("PB&J", 2, "halves", "sweet", "white", "", "peanut butter & jam");

           lunch[2] = new Food("Soup", 2, "cups", "savoury");

           lunch[3] = new Food("Soda crackers", 4, "pieces", "savoury");

           lunch[4] = new Food("Carrots", 5, "sticks", "sweet");

          

           for (int i = 0; i < lunch.length; i++)

           {

                System.out.println(lunch[i]);

           }

     }

}

 

And this is the toString() in Food that I changed:

 

     @Override

     public String toString()

     {

           return name;

     }

 

And this is the output:

 

Club house on sourdough bread

*****

LUNCH MENU

Club house on sourdough bread

PB&J on white bread

Soup

Soda crackers

Carrots