Advertisement
karlakmkj

Child classes in Array

Dec 3rd, 2020
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.32 KB | None | 0 0
  1. class Noodle {
  2.  
  3.   protected double lengthInCentimeters;
  4.   protected double widthInCentimeters;
  5.   protected String shape;
  6.   protected String ingredients;
  7.   protected String texture = "brittle";
  8.  
  9.   //Constructor
  10.   Noodle(double lenInCent, double wthInCent, String shp, String ingr) {
  11.     this.lengthInCentimeters = lenInCent;
  12.     this.widthInCentimeters = wthInCent;
  13.     this.shape = shp;
  14.     this.ingredients = ingr;
  15.   }
  16.  
  17.   public String getCookPrep() {
  18.     return "Boil noodle for 7 minutes and add sauce.";
  19.   }
  20.  
  21.  
  22.   public static void main(String[] args) {
  23.    
  24.     Noodle spaghetti, ramen, pho; // put instances of different classes that share a parent class together in an array
  25.    
  26.     spaghetti = new Spaghetti();
  27.     ramen = new Ramen();
  28.     pho = new Pho();
  29.    
  30.     // Add your code below:
  31.     Noodle[] allTheNoodles = {spaghetti, ramen, pho};  //declare and initialize array of Noodle class type here
  32.    
  33.     for (Noodle nood: allTheNoodles){    //loop through each noodle in the array to print the method of the child class
  34.       System.out.println(nood.getCookPrep());
  35.     }
  36.   }
  37. }
  38. ============================================================================================================================
  39. class Spaghetti extends Noodle {
  40.  
  41.   Spaghetti() {
  42.     super(30.0, 0.2, "round", "semolina flour");
  43.   }
  44.  
  45.   @Override
  46.   public String getCookPrep() {    //Method will be printed
  47.     return "Boil spaghetti for 8 - 12 minutes and add sauce, cheese, or oil and garlic.";
  48.   }
  49. }
  50. ============================================================================================================================
  51. class Ramen extends Noodle {
  52.  
  53.   Ramen() {
  54.     super(30.0, 0.3, "flat", "wheat flour");
  55.   }
  56.  
  57.   @Override
  58.   public String getCookPrep() {    //Method will be printed
  59.     return "Boil ramen for 5 minutes in broth, then add meat, mushrooms, egg, and vegetables.";
  60.   }
  61. }
  62. ============================================================================================================================
  63. class Pho extends Noodle {
  64.  
  65.   Pho() {
  66.     super(30.0, 0.64, "flat", "rice flour");
  67.   }
  68.  
  69.   @Override
  70.   public String getCookPrep() {    //Method will be printed
  71.     return "Soak pho for 1 hour, then boil for 1 minute in broth. Then garnish with cilantro and jalapeno.";
  72.   }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement