Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java program: 1) Create an abstract class Plant with the following features clas

ID: 3591602 • Letter: J

Question

Java program:

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

Explanation / Answer

Given below is the code for the question. Please don't forget to rate the answer if it helped. Thank you.

To indent code in eclipse, select code by pressing Ctrl+A and then Ctrl+i

Plant.java


public abstract class Plant {
//class variables and methods
public static String ENERGY_SOURCE = "carbon dioxide" ;
private static int plants = 0;
public static int numberOfPlants()
{
return plants;
}
//instance variables / methods
private String color;
public Plant()
{
color = "none";
plants++;
}
public Plant(String color)
{
this.color = color;
plants++;
}
public void setColor(String color)
{
this.color = color;
}
public String getColor()
{
return color;
}
public String toString()
{
return "color " + color + " ";
}
public abstract int getWatering();
}

Fern.java


public class Fern extends Plant implements Comparable<Fern>{
public Fern()
{
super();
}
@Override
public int getWatering() {
double n = Math.random();
// 4 out of 5 times translates to 4/5 * 100 = 80 %
//when we check if n <= 0.8 , we return 7 other wise return 14
if(n <= 0.8)
return 7;
else
return 14;
}
@Override
public int compareTo(Fern f) {
int w1 = getWatering();
int w2 = f.getWatering();
System.out.println("first fern " + w1);
System.out.println("second fern " + w2);
if(w1 == w2)
return 0;
else if(w1 < w2)
return -1;
else
return 1;
}
}

Succulent.java


public class Succulent extends Plant {
private boolean hasSpines;
public Succulent()
{
this("purple", false);
}
public Succulent(String color, boolean spines)
{
super(color);
hasSpines = spines;
}
public boolean hasSpines()
{
return hasSpines;
}
public void setHasSpines(boolean spines)
{
hasSpines = spines;
}
public String getType()
{
if(hasSpines)
return "cactus";
else
return "succulent";
}
public boolean isSameType(Succulent s2)
{
return hasSpines == s2.hasSpines;
}
@Override
public int getWatering() {
if(hasSpines)
return 28;
else
return 14;
}
public String toString()
{
return super.toString() + "hasSpines " + hasSpines + " ";
}
}

output of PlantCollection.java


Our plant collection has the following:
A succulent s1 that is a cactus and has red flowers
A succulent s2 that is not a cactus and has red flowers
A succulent s3 that is not a cactus and has white flowers
The color we have for plant p2 is red
It seems that plant s2's color is actually white, so let's change it
Plant p1 is a cactus
Plant p2 is a succulent
Are succulents p1 and p2 the same type? No
Let's add two ferns to our collection
Comparing watering of ferns:
first fern 14
second fern 7
Fern one needs to be watered less often than fern two
There are 5 plants in our collection
Here's what our collection looks like:
Succulent
color red
hasSpines true

Succulent
color white
hasSpines false

Succulent
color purple
hasSpines false

Fern
color none

Fern
color none

A plant's energy source is: carbon dioxide

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote