Question

1-Assume that the Plant class looks like the following: public abstract class Plant { private int...

1-Assume that the Plant class looks like the following:

public abstract class Plant {

private int age=0;

private int height=0;

public int getAge() {

return age;

}

public void addYearToAge() {

age++;

}

public int getHeight() {

return height;

}

public void setHeight(int height) {

this.height = height;

}

abstract public void doSpring();

abstract public void doSummer();

abstract public void doFall();

abstract public void doWinter();

}

2- Assume that the MapleTree class looks like the following:

public class MapleTree extends Plant {

private static final int AMOUNT_TO_

GROW_I_ONE_GROWING_SEASON = 2;

// A tree grows upwards a certain number of feet a year.A tree does not die down to ground level during the winter. //

private void grow() {

int currentHeight = getHeight();

setHeight(currentHeight + AMOUNT_TO_GROW_IN_ONE_GROWING_SEASON);

}

public void doSpring() {

grow();

addYearToAge();

System.out.println("Spring: The maple tree is starting to grow " + "leaves and new branches");

System.out.println("\t Current Age: " + getAge() + " " +"Current Height: " + getHeight());

}

public void doSummer() {

grow();

System.out.println("Summer: The maple tree is continuing to grow");

System.out.println("\t Current Age: " + getAge() + " " +"Current Height: " + getHeight());

}

public void doFall() {

System.out.println("Fall: The maple tree has stopped growing and is losing its leaves");

System.out.println("\t Current Age: " + getAge() + " " + "Current Height: " + getHeight());

}

public void doWinter() {

System.out.println("Winter: The maple tree is dormant");

System.out.println("\t Current Age: " + getAge() + " " +"Current Height: " + getHeight());

}

}

3- Assume that the class Tulip looks like:

        public class Tulip extends Plant {

// A tulip grows each year to the same height. During the winter they die down to ground level.//

private void grow() {

int currentHeight = getHeight();

setHeight(currentHeight + AMOUNT_TO_GROW_IN_ONE_GROWING_SEASON);

}

private void dieDownForWinter(){

setHeight(0);

}

public void doSpring() {

grow();

addYearToAge();

System.out.println("Spring: The tulip is starting to grow " +"up from the ground");

System.out.println("\t Current Age: " + getAge() + " " +"Current Height: " + getHeight());

}

public void doSummer() {

System.out.println("Summer: The tulip has stopped growing " +"and is flowering");

System.out.println("\t Current Age: " + getAge() + " " + "Current Height: " + getHeight());

}

public void doFall() {

System.out.println("Fall: The tulip begins to wilt");

System.out.println("\t Current Age: " + getAge() + " " +"Current Height: " + getHeight());

}

public void doWinter() {

dieDownForWinter();

System.out.println("Winter: The tulip is dormant underground");

System.out.println("\t Current Age: " + getAge() + " " + "Current Height: " + getHeight());}

}

This exercise will use the previous classes and add new functionality to it.

  1. Add a new class called Rose that will represent a rose. Use the Plant base class and implement all of the required methods.(20 points)
  2. Create a class called Simulator that will do the following:
    1. Instantiate a maple tree object from the class MapleTree(10 points)
    2. Instantiate a rose object from the class Rose (10 points)
    3. Instantiate a tulip object from the class Tulip (10 points)
    4. We want to simulate the life of these trees for 3 years. Use a for loop that will run for 3 years.(50 points)
      1. We will let the maple tree, rose, and tulip do the spring.
      2. We will let the maple tree, rose, and tulip do the summer.
      3. We will let the maple tree, rose, and tulip do the fall.
      4. We will let the maple tree, rose, and tulip do the winter.
      5. Copy the plant simulator into the text editor or IDE of your choice.
  3. Compile and run the example to ensure that the code has been copied correctly.
  4. Make sure that the output will look like this:
  5. Creating a maple tree and tulip and a rose...

    Entering a loop to simulate 3 years

    Spring: The maple tree is starting to grow leaves and new branches

    Current Age: 1 Current Height: 2

    Spring: The tulip is starting to grow up from the ground

    Current Age: 1 Current Height: 1

    Spring: The rose is starting to grow up from the ground

    Current Age: 1 Current Height: 1

    Summer: The maple tree is continuing to grow

    Current Age: 1 Current Height: 4

    Summer: The tulip has stopped growing and is flowering

    Current Age: 1 Current Height: 1

    Summer: The rose has stopped growing and is flowering

    Current Age: 1 Current Height: 1

    Fall: The maple tree has stopped growing and is losing its leaves

    Current Age: 1 Current Height: 4

    Fall: The tulip begins to wilt

    Current Age: 1 Current Height: 1

    Fall: The rose begins to wilt

    Current Age: 1 Current Height: 1

    Winter: The maple tree is dormant

    Current Age: 1 Current Height: 4

    Winter: The tulip is dormant underground

    Current Age: 1 Current Height: 0

    Winter: The rose is dormant underground

    Current Age: 1 Current Height: 0

    Spring: The maple tree is starting to grow leaves and new branches

    Current Age: 2 Current Height: 6

    Spring: The tulip is start

    ing to grow up from the ground

    Current Age: 2 Current Height: 1

    Spring: The rose is starting to grow up from the ground

    Current Age: 2 Current Height: 1

    Summer: The maple tree is continuing to grow

    Current Age: 2 Current Height: 8

    Summer: The tulip has stopped growing and is flowering

    Current Age: 2 Current Height: 1

    Summer: The rose has stopped growing and is flowering

    Current Age: 2 Current Height: 1

    Fall: The maple tree has stopped growing and is losing its leaves

    Current Age: 2 Current Height: 8

    Fall: The tulip begins to wilt

    Current Age: 2 Current Height: 1

    Fall: The rose begins to wilt

    Current Age: 2 Current Height: 1

    Winter: The maple tree is dormant

    Current Age: 2 Current Height: 8

    Winter: The tulip is dormant underground

    Current Age: 2 Current Height: 0

    Winter: The rose is dormant underground

    Current Age: 2 Current Height: 0

    Spring: The maple tree is starting to grow leaves and new branches

    Current Age: 3 Current Height: 10

    Spring: The tulip is starting to grow up from the ground

    Current Age: 3 Current Height: 1

    Spring: The rose is starting to grow up from the ground

    Current Age: 3 Current Height: 1

    Summer: The maple tree is continuing to grow

    Current Age: 3 Current Height: 12

    Summer: The tulip has stopped growing and is flowering

    Current Age: 3 Current Height: 1

    Summer: The rose has stopped growing and is flowering

    Current Age: 3 Current Height: 1

    Fall: The maple tree has stopped growing and is losing its leaves

    Current Age: 3 Current Height: 12
    Fall: The tulip begins to wilt
    Current Age: 3 Current Height: 1
    Fall: The rose begins to wilt
    Current Age: 3 Current Height: 1
    Winter: The maple tree is dormant
    Current Age: 3 Current Height: 12
    Winter: The tulip is dormant underground
    Current Age: 3 Current Height: 0
    Winter: The rose is dormant underground
    Current Age: 3 Current Height: 0

0 0
Add a comment Improve this question Transcribed image text
Answer #1

NOTE:-

1) Save the files with respective names given and run the Simulator.java file for simulation. Save files as it is provided in answers in 5 different class files and then only run Simulator.

2) Don't forget to upvote. Ask in comment section for any doubts.

CODE(IMAGES):-

1) Plant.java

- 0 X C\Users\CEBRUSIUM\Desktop Plantjava (Code) - Sublime Text (UNREGISTERED) Eile Edit Selection Find View Goto Tools Proje

2) MapleTree.java: -

- C\Users\CEBRUSIUM\Desktop Maple Tree.java (Code) - Sublime Text (UNREGISTERED) Eile Edit Selection Find View Goto Tools Pro

3) Tulip.java

C\Users\CEBRUSIUM\Desktop Tulip.java (Code) - Sublime Text (UNREGISTERED) Eile Edit Selection Find View Goto Tools Project Pr

4) Rose.java: -

CUsers\CEBRUSIUM\Desktop Rose.java (Code) - Sublime Text (UNREGISTERED) Eile Edit Selection Find View Goto Tools Project Pref

5) Simulator.java

CUsers\CEBRUSIUM\Desktop Simulatorjava (Code) - Sublime Text (UNREGISTERED) Eile Edit Selection Find View Goto Tools Project

CODE(TEXT): -

1) Plant.java

public abstract class Plant {
   private int age=0;
   private int height=0;
  
   public int getAge() {
   return age;
   }

   public void addYearToAge() {
       age++;
   }

   public int getHeight() {
   return height;
   }

   public void setHeight(int height) {
   this.height = height;
   }

   abstract public void doSpring();
   abstract public void doSummer();
   abstract public void doFall();
   abstract public void doWinter();
}

2) MapleTree.java

public class MapleTree extends Plant {
   private static final int AMOUNT_TO_GROW_IN_ONE_GROWING_SEASON = 2;

   // A tree grows upwards a certain number of feet a year.A tree does not die down to ground level during the winter. //

   private void grow() {
       int currentHeight = getHeight();
       setHeight(currentHeight + AMOUNT_TO_GROW_IN_ONE_GROWING_SEASON);
   }

   public void doSpring() {
       grow();
       addYearToAge();
       System.out.println("Spring: The maple tree is starting to grow " + "leaves and new branches");
       System.out.println("\t Current Age: " + getAge() + " " +"Current Height: " + getHeight());
   }

   public void doSummer() {
       grow();
       System.out.println("Summer: The maple tree is continuing to grow");
       System.out.println("\t Current Age: " + getAge() + " " +"Current Height: " + getHeight());
   }

   public void doFall() {
       System.out.println("Fall: The maple tree has stopped growing and is losing its leaves");
       System.out.println("\t Current Age: " + getAge() + " " + "Current Height: " + getHeight());
   }

   public void doWinter() {
       System.out.println("Winter: The maple tree is dormant");
       System.out.println("\t Current Age: " + getAge() + " " +"Current Height: " + getHeight());
   }

}

3) Tulip.java

public class Tulip extends Plant {
   // A tulip grows each year to the same height. During the winter they die down to ground level.//
   private static final int AMOUNT_TO_GROW_IN_ONE_GROWING_SEASON = 1;
   private void grow() {
       int currentHeight = getHeight();
       setHeight(currentHeight + AMOUNT_TO_GROW_IN_ONE_GROWING_SEASON);
   }

   private void dieDownForWinter(){
       setHeight(0);
   }

   public void doSpring() {
       grow();
       addYearToAge();
       System.out.println("Spring: The tulip is starting to grow " +"up from the ground");
       System.out.println("\t Current Age: " + getAge() + " " +"Current Height: " + getHeight());
   }

   public void doSummer() {
       System.out.println("Summer: The tulip has stopped growing " +"and is flowering");
       System.out.println("\t Current Age: " + getAge() + " " + "Current Height: " + getHeight());
   }

   public void doFall() {
       System.out.println("Fall: The tulip begins to wilt");
       System.out.println("\t Current Age: " + getAge() + " " +"Current Height: " + getHeight());
   }

   public void doWinter() {
       dieDownForWinter();
       System.out.println("Winter: The tulip is dormant underground");
       System.out.println("\t Current Age: " + getAge() + " " + "Current Height: " + getHeight());
   }

}

4) Rose.java

public class Rose extends Plant {
   // nearly same as Tulip

   private static final int AMOUNT_TO_GROW_IN_ONE_GROWING_SEASON = 1;
   private void grow() {
       int currentHeight = getHeight();
       setHeight(currentHeight + AMOUNT_TO_GROW_IN_ONE_GROWING_SEASON);
   }

   private void dieDownForWinter(){
       setHeight(0);
   }

   public void doSpring() {
       grow();
       addYearToAge();
       System.out.println("Spring: The rose is starting to grow " +"up from the ground");
       System.out.println("\t Current Age: " + getAge() + " " +"Current Height: " + getHeight());
   }

   public void doSummer() {
       System.out.println("Summer: The rose has stopped growing " +"and is flowering");
       System.out.println("\t Current Age: " + getAge() + " " + "Current Height: " + getHeight());
   }

   public void doFall() {
       System.out.println("Fall: The rose begins to wilt");
       System.out.println("\t Current Age: " + getAge() + " " +"Current Height: " + getHeight());
   }

   public void doWinter() {
       dieDownForWinter();
       System.out.println("Winter: The rose is dormant underground");
       System.out.println("\t Current Age: " + getAge() + " " + "Current Height: " + getHeight());
   }

}

5) Simulator.java

public class Simulator // for simulationg all plant objects
{
   public static void main(String[] args) // main function
   {
       System.out.println("Creating a maple tree and tulip and a rose...");
       MapleTree mapleObj = new MapleTree();
       Rose roseObj = new Rose();
       Tulip tulipObj = new Tulip();

       System.out.println("Entering a loop to simulate 3 years");
       for(int i = 0; i < 3; i++) // loops for 3 times
       {
           mapleObj.doSpring();
           tulipObj.doSpring(); // call for spring
           roseObj.doSpring();

           mapleObj.doSummer();
           tulipObj.doSummer(); // call for summer
           roseObj.doSummer();

           mapleObj.doFall();
           tulipObj.doFall(); // call for fall
           roseObj.doFall();

           mapleObj.doWinter();
           tulipObj.doWinter(); // call for winter
           roseObj.doWinter();
       }
   }
}

SAMPLE OUTPUT: -

C. Command Prompt C:\Users\CEBRUSIUM\Desktop>java Simulator Creating a maple tree and tulip and a rose... Entering a loop toOn Command Prompt Vinter: The rose is dormant underground Current Age: 2 Current Height: 0 Spring: The maple tree is starting

Add a comment
Know the answer?
Add Answer to:
1-Assume that the Plant class looks like the following: public abstract class Plant { private int...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • package rectangle; public class Rectangle {    private int height;    private int width;    public...

    package rectangle; public class Rectangle {    private int height;    private int width;    public Rectangle(int aHeight, int aWidth) {    super();    height = aHeight;    width = aWidth;    }    public int getHeight() {    return height;    }    public int getWidth() {    return width;    }    public void setHeight(int aHeight) {    height = aHeight;    }    public void setWidth(int aWidth) {    width = aWidth;    }    public int...

  • Here is the code for the Infant class: public class Infant{ private String name; private int...

    Here is the code for the Infant class: public class Infant{ private String name; private int age; // in months public Infant(String who, int months){ name = who; age = months; } public String getName(){ return name;} public int getAge(){ return age;} public void anotherMonth() {age = age + 1;} } The code box below includes a live Infant array variable, thoseKids. You cannot see its declaration or initialization. Your job is to find out which Infant in the array...

  • What is output? public abstract class People { protected string name; protected int age; public abstract...

    What is output? public abstract class People { protected string name; protected int age; public abstract void PrintInfo(); public void PrintInformation() { System.out.println("In Base Class People"); public class Teacher extends People { private int experience; public void PrintInfo() { System.out.println("In Child Class Teacher"); public class Principal extends Teacher { public void PrintInformation() { System.out.println("In Child Class Principal"); public static void main(String args[]) { Principal tim; tim = new Principal(); tim.PrintInfo(); In Base Class People Error: Compiler error In Child Class...

  • Examine the following class definition: public class Date private int year; private int month; private int...

    Examine the following class definition: public class Date private int year; private int month; private int day; public Date() { ...) public void set (int x, int y, int z) { ...) public int getYear() { ...) // returns year public int getMonth() { } // returns month public int get Day () { ...) // returns day //more methods here -- 1 Which of the following statements in a client program correctly prints out the day of the object...

  • Using your Dog class from earlier this week, complete the following: Create a new class called...

    Using your Dog class from earlier this week, complete the following: Create a new class called DogKennel with the following: Instance field(s) - array of Dogs + any others you may want Contructor - default (no parameters) - will make a DogKennel with no dogs in it Methods: public void addDog(Dog d) - which adds a dog to the array public int currentNumDogs() - returns number of dogs currently in kennel public double averageAge() - which returns the average age...

  • Assignment (to be done in Java): Person Class: public class Person extends Passenger{ private int numOffspring;...

    Assignment (to be done in Java): Person Class: public class Person extends Passenger{ private int numOffspring; public Person() {    this.numOffspring = 0; } public Person (int numOffspring) {    this.numOffspring = numOffspring; } public Person(String name, int birthYear, double weight, double height, char gender, int numCarryOn, int numOffspring) {    super(name, birthYear, weight, height, gender, numCarryOn);       if(numOffspring < 0) {        this.numOffspring = 0;    }    this.numOffspring = numOffspring; } public int getNumOffspring() {   ...

  • For Questions 1-3: consider the following code: public class A { private int number; protected String...

    For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B() {    super();    System.out.println(“B() called”);...

  • For Questions 1-3: consider the following code: public class A { private int number; protected String...

    For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B() {   super();   System.out.println(“B() called”); } public...

  • public class Buildbst { private int data; private Buildbst left; private Buildbst right; //Set the binary...

    public class Buildbst { private int data; private Buildbst left; private Buildbst right; //Set the binary search tree public Buildbst(int data) { this.data = data; this.left = null; this.right =null; } public int getData() { return data; } public void setData(int data) { this.data = data; } public Buildbst getLeft() { return left; } public void setLeft(Buildbst left) { this.left = left; } public Buildbst getRight() { return right; } public void setRight(Buildbst right) { this.right = right; } }...

  • public class Animal {    private String name; //line 1    private int weight; //line 2...

    public class Animal {    private String name; //line 1    private int weight; //line 2    private String getName(){       return name;    } //line 3    public int fetchWeight(){       return weight; } //line 4 } public class Dog extends Animal {    private String food; //line 5    public void mystery(){       //System.out.println("Name = " + name); //line 6            System.out.println("Food = " + food); //line 7    } } I want to know the super...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT