Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.16 KB | None | 0 0
  1. public static void main(String[] args) {
  2.         //Order: Food -> FoodType -> Nutritous
  3.         //Creates arrays for different classes
  4.         //Nutritious(String name, boolean fresh, String time, boolean healthy) {
  5.         Food[] array = new Food[4];
  6.         array[0] = new FoodType("Apple", true, "Breakfast");
  7.         array[1] = new FoodType("Orange", false, "Snack");
  8.         array[2] = new Nutritious("Sandwhich", true, "Lunch", true);
  9.         array[3] = new Nutritious("Steak", true, "Dinner", false);
  10.  
  11.         for (int i = 0; i < array.length; i++) {
  12.             //Prints class of element                
  13.             if (array[i].getClass().getSimpleName().equals("FoodType")) {
  14.                 FoodType food = (FoodType) array[i];
  15.                 food.shouldEat();
  16.             } else {
  17.                 Nutritious foods = (Nutritious) array[i];
  18.                 foods.isNutritous();
  19.             }
  20.         }
  21.  
  22.         for (int i = 0; i < array.length; i++) {
  23.             //Prints out objects in array        
  24.             System.out.println("\nClass is: " + array[i].getClass().getName());
  25.             System.out.println(array[i].toString());
  26.         }
  27.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement