Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java I need a Shrub class Extend the parent class Define two more additional unique private data members each Contain two constructors - no-arg and

Java I need a Shrub class

    1. Extend the parent class

    2. Define two more additional unique private data members each

    3. Contain two constructors - no-arg and all-arg constructors for each (the all-arg must utilize super so that the parent class values get set as well)

    4. Include setters and Getters for each new data member

    5. A toString() method for each that includes super.toString() along with the unique information for each child class

    6. The implementation of the abstract method that you created in the parent class. For simplicitys sake, just have it print something specific to each child type.

  1. You will not implement Comparable in either of your child classes. This isnt necessary since I just want a sorting mechanism that can be applied to any and all child types.

  • Part 4: MainApp

  1. Create a main application and inside create an ArrayList that stores your parent type.

  2. Add three objects from each child type for a total of six objects in the ArrayList.

  3. Use a for each loop to loop through the ArrayList. For each object in the ArrayList print the toString() and call the extra method (the one that overrode the abstract method in the parent class.)

  4. Use Collections.sort() to sort your ArrayList. (This does not go in a loop.)

  5. Use a for loop to loop through the ArrayList again, printing the same items in step 3. Make sure your sorting worked as defined in your compareTo() method.

  6. Note the different looping requests above. Make sure the first one is a for each loop and the second one is a for loop.

My Example:

Parent Class: Plant

  • Data Members: name, maxHeight

  • Abstract Method: getUpkeepInformation()

  • Comparable compareTo() Method: Will sort alphabetically by name.

Child Class 1: Flower

  • Data Members: color, cutFlower? (boolean indicating if its a flower meant for cutting/bouquets/florists)

  • The Overridden getUpkeepInformation() Method: Will print a statement indicating if a plant requires full sun or not. (This is just a hard-coded print statement. If we wanted to fully flesh this class out, this would make more sense. For the purpose of the lab, just come up with something semi-relevant.)

Child Class 2: Shrub

  • Data Members: type, dateForPruning

  • The Overridden getUpkeepInformation() Method: Will print a statement indicating how often pruning should occur.

MainApp: I will create an ArrayList of Plant objects and then create three Flower objects (with all four pieces of information passed in) and three Shrub objects (also with all four pieces of information passed in). I will print the ArrayList as requested, then sort, then print again. Shown below is the toString() followed by my extra void method print statement for each object. My final output might look something like this.

Final Output:

Dwarf Sunflower has a max height of 12.0 inches. It is Yellow/Black and is a cut flower.

A Dwarf Sunflower requires full sun.

Nasturtium has a max height of 12.0 inches. It is Orange/Red/Yellow and is not a cut flower.

A Nasturtium requires full sun.

State Fair Zinnia has a max height of 16.0 inches. It is Multi-colored and is a cut flower.

A State Fair Zinnia requires full sun.

A Boxwood is a(n) evergreen shrub with a maximum height of 30 feet. It should be pruned in the spring.

Pruning should occur once a year.

A Japanese Spirea is a(n) deciduous shrub with a maximum height of 4 feet. It should be pruned in the late winter.

Pruning should occur once a year.

A Flaky Juniper is a(n) evergreen shrub with a maximum height of 3 feet. It should be pruned in the early spring.

Pruning should occur once a year.

*************Sort using Collections.sort()********************

Boxwood is a(n) evergreen shrub with a maximum height of 30 feet. It should be pruned in the spring.

Pruning should occur once a year.

Dwarf Sunflower has a max height of 12.0 inches. It is Yellow/Black and is a cut flower.

A Dwarf Sunflower requires full sun.

Flaky Juniper is a(n) evergreen shrub with a maximum height of 3 feet. It should be pruned in the early spring.

Pruning should occur once a year.

Japanese Spirea is a(n) deciduous shrub with a maximum height of 4 feet. It should be pruned in the late winter.

Pruning should occur once a year.

Nasturtium has a max height of 12.0 inches. It is Orange/Red/Yellow and is not a cut flower.

A Nasturtium requires full sun.

State Fair Zinnia has a max height of 16.0 inches. It is Multi-colored and is a cut flower.

A State Fair Zinnia requires full sun.

MainApp

import java.util.ArrayList; import java.util.Collections; public class MainApp { public static void main(String[] args) { ArrayList plants = new ArrayList<>(); plants.add(new Flower("Four O'Clock", 18, "Pink/Yellow/White", false)); plants.add(new Flower("Nasturtium", 12, "Orange/Red/Yellow", false)); plants.add(new Flower("State Fair Zinnia", 16, "Multi-colored", true)); plants.add(new Flower("Dwarf Sunflower", 12, "Yellow/Black", true)); /* call the .getInfo() method on each object in the loop. */ for (Plant p :plants ) { System.out.print(p); p.getInfo(); } //Collections.sort() System.out.println(" After Sort: "); Collections.sort(plants); for (Plant p :plants ) { System.out.print(p); p.getInfo(); } } }

Flower

public class Flower extends Plant{ private String color; private boolean cutFlower; public Flower() { this("Plant", -1, "No Color", false); } public Flower(String name, double maxHeight, String color, boolean cutFlower) { super(name, maxHeight); this.color = color; this.cutFlower = cutFlower; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public boolean isCutFlower() { return cutFlower; } public void setCutFlower(boolean cutFlower) { this.cutFlower = cutFlower; } @Override public String toString() { String cut; if(cutFlower){ cut = "a cut flower"; } else { cut = "not a cut flower"; } return super.toString()+" " + "It is " + color + " and is " + cut +" "; } /* override the getInfo() method */ /** * add a print statement that says The  * should be planted in full sun. */ @Override public void getInfo() { System.out.println(getName()+" should be planted in full sun."); } }

Plant

public abstract class Plant implements Comparable{ private String name; private double maxHeight; /* a void abstract method to this class called getInfo() with no parameters. */ public abstract void getInfo(); public Plant(){ this("Plant", -1); } public Plant(String name, double maxHeight) { this.name = name; this.maxHeight = maxHeight; } public String getName() { return name; } public double getMaxHeight() { return maxHeight; } public void setName(String name) { this.name = name; } public void setMaxHeight(double maxHeight) { this.maxHeight = maxHeight; } public String toString(){ return name + " has a max height of " + maxHeight + " inches."; } //Add the compareTo() method /** * compareTo() method to first compare by maxHeight and then by name * (in case multiple objects have the same maxHeight). * @param o * @return */ @Override public int compareTo(Plant o) { if(this.maxHeight== o.maxHeight) return this.name.compareTo(o.name); else return (int)(this.maxHeight-o.maxHeight); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases A Beginners Guide

Authors: Andy Oppel

1st Edition

007160846X, 978-0071608466

More Books

Students also viewed these Databases questions

Question

Provide examples of KPIs in Human Capital Management.

Answered: 1 week ago

Question

What are OLAP Cubes?

Answered: 1 week ago