11. Quiz C
- A
Plant
prefers to live in a certain range of temperatures, known in the US as “hardiness zones” - integers from 1 through 13. Write a constructor for a plant that sets its name and the low and high numbers for the zones the plant tolerates (also see question 2).
public class Plant {
private String name;
private int low, high; // hardiness zones
// 1A. You write a constructor
// 2. The zone method is described below.
public int zone (int min_temp_F) { ... }
// Other methods
public int getLow() { return low; }
public int getHigh() { return high; }
public String getName() { return name; }
}
-
The hardiness zone of a plant is determined by the “average annual extreme minimum temperature”. See the table below.
2a. Write a function
zone
that takes in the average annual extrememe minimum temperature and puts out the zone associated with that temperature. Temperatures on the boundary between two zones can be put in either, but be consistent.public int zone (int min_temp_F) { ... }
2b. Is it appropriate to make this function
static
? Explain. -
The
Succulent
is a subclass ofPlant
. In addition to the information needed byPlant
, the constructor takes in a typical percentage of the plant’s mass that is water. The Succulent also has agetCutting
method that returns a new Succulent with the same name, zone information, and water percent. Write theSucculent
class.
public static void definitely() {
double std_water_pct = 0.45;
Succulent s = new Succulent("Aloe arborescens", 12, 16, std_water_pct);
//(A) Plant p = Main.more(s); // see below
}
- Does the following
more
method work when called on line “A” above? Explain.
public class Main {
public static Plant more (Plant x) {
return x.getCutting();
}
}
- (Fake info.) Succulents are not killed by frostbite, but they are
damaged, causing their minimum zone to increase by one. Explain how
to write a
public void frost()
method.
public class Frosty {
public static void demo_frost() {
Succulent s = new Succulent("Aloe arborescens", 12, 16, 0.45);
int before = s.getLow(); // zone 12
s.frost();
int after = s.getLow(); // zone 13 now
}
}