Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Java program: public class PlantCollection { public static void main (String [] args) { System.out.println( Our plant collection has the following: ); System.out.println(A succulent s1
Java program:
public class PlantCollection { public static void main (String [] args) { System.out.println(" Our plant collection has the following: "); System.out.println("A succulent s1 that is a cactus and has red " + "flowers"); System.out.println("A succulent s2 that is not a cactus and has red " + "flowers"); System.out.println("A succulent s3 that is not a cactus and has white " + "flowers"); Plant p1 = new Succulent("red", true); Plant p2 = new Succulent("red", false); Plant p3 = new Succulent(); System.out.println(" The color we have for plant p2 is " + p2.getColor()); System.out.println("It seems that plant s2's color is actually " + "white, so let's change it"); p2.setColor("white"); System.out.println(" Plant p1 is a " + ((Succulent)p1).getType()); System.out.println("Plant p2 is a " + ((Succulent)p2).getType()); System.out.println(" Are succulents p1 and p2 the same type? " + (((Succulent)p1).isSameType((Succulent)p2) ? "Yes" : "No")); System.out.println(" Let's add two ferns to our collection"); Plant p4 = new Fern(); Plant p5 = new Fern(); System.out.println("Comparing watering of ferns:"); int result = ((Fern)p4).compareTo((Fern)p5); System.out.println("Fern one needs to be watered " + ((result == 0) ? "the same as" : (result == 1) ? "less often than" : "more often than") + " fern two"); System.out.println(" There are " + Plant.numberOfPlants() + " plants in our collection"); Plant [] collection = {p1, p2, p3, p4, p5}; System.out.println(" Here's what our collection looks like: "); for (int i = 0; i 1) Create an abstract class Plant with the following features class variable ENERGY_SOURCE with value "carbon dioxide" class variable called plants to count plant objects class method numberofPlants) that returns number of plant objects instance variable for color no-argument constructor that sets flower color to none constructor that takes a user-specified color getter setter toString) abstract method getWatering) that returns integer for number of days after which plant needs to be watered 2) Create a Fern class that extends Plant and implements the Comparable interface and contains the following no argument constructor that assigns default value of none for color (since ferns don't method getWatering) that returns 7 four out of five times and 14 the fifth time (since method compareTo that calls getWatering) for each plant and returns O if the have flowers) ferns need weekly watering unless it is very humid; use Math.random)) numbers are the same, 1 if the first plant's is larger, and -1 if it is smaller => because the numbers generated will be random (and thus are different each time it is run), add the following code to your method f1 = getWatering(); f2 - f.getWatering); (where f is whatever you called the parameter) System.out.printin("first fern + f1); System.out.printIn("second fern +12) 3) Create a Succulent class that extends Plant that contains the following instance variable hasSpines that is set to true if plant is a cactus, false otherwise no-argument constructor that assigns default value of false for hasSpines and purple for color constructor that assigns user-specified values for hasSpines and color getter (call it hasSpines rather than getSpines since it returns a boolean) setter toString) method getType) that returns "cactus" if hasSpines is true, "succulent" otherwise method isSameType that returns true if both plants are the same subtype method getWatering) that returns 28 for cacti, 14 otherwise
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started