Create a Java project titled \"hw2project\", click the src icon and create a pac
ID: 3883515 • Letter: C
Question
Create a Java project titled "hw2project", click the src icon and create a package called "weather".Create 5 classes within the weather package: Sky, Cloud, StratusCloud, CumulusCloud, and CirrusCloud. The class names, as well as the package name, must be spelled and capitalized exactly as shown here
Cloud.java
This class needs:
1) Private float instance variables: bottom and top.
2) A ctor that takes bottom and top as args and stores them in the instance
variables. NOTE: “ctor” is an abbreviation for “constructor”.
3) A public method getHeight() that returns a float, whose value is top minus
bottom.
4) A public String method rain() that returns “It is raining”
StratusCloud.java and CumulusCloud.java
For these classes:
1) Change the class declarations so that they are subclasses of Cloud.
2) Fix the ctors so that they pass bottom and top to the superclass ctor.
CirrusCloud.java
Subclasses of Cloud inherit a rain() method that is not appropriate for cirrus clouds.
Override rain() so that it returns “I cannot make rain”
Sky.java
This class holds lots of cloud instances of various types, and computes the average
cloud height. It needs:
1) A private ArrayList<Cloud> called clouds.
2) A ctor that creates the clouds array list with an initial capacity of 100. Look up
the javadoc API page for ArrayList to see what ArrayList ctor to call. If you don’t
know how to do this, ask on Piazza. DON’T ask Piazza for the answer, just ask
how to find the javadoc API page.
3) A public method called add(Cloud c) that takes a cloud and adds it to the array
list. The return type of this method should be boolean, and the method should
always return true. That’s a little weird but it will make more sense later.
4) A public float method called getMeanHeight(), which returns the average height
of all the clouds in the array list.
5) A main() method. Paste in the following:
public static void main(String[] args)
{
StratusCloud strat = new StratusCloud(100, 1000);
if (!strat.rain().startsWith("It is raining"))
System.out .println("Bad StratusCloud::rain");
CumulusCloud cumu = new CumulusCloud(200, 2000);
if (!cumu.rain().startsWith("It is raining"))
System.out .println("Bad CumulusCloud::rain");
CirrusCloud cirr = new CirrusCloud(300, 3000);
if (!cirr.rain().startsWith("I cannot make"))
System.out .println("Bad CirrusCloud::rain");
Sky sky = new Sky();
sky.add(strat);
sky.add(cumu);
sky.add(cirr);
float mean = sky.getMeanHeight();
if (mean < 1799 || mean > 1801)
System.out .println("Bad mean height: expected 1800, saw " + mean);
System.out .println("Everything (else) is ok");
}
To test your work so far, run Sky as an application. The only output
should be “Everything (else) is ok”.
Sky2.java
Lastly, implement Sky in a somewhat different way. When one class holds many
references to instances of another class, we say (for example) that Sky “aggregates”
clouds. Sky aggregates by owning an array list. Sky2 will aggregate by being (in a
sense) an array list. That is, Sky2 will extend ArrayList<Cloud>.
Create class Sky2 in the weather. The Sky2 ctor will need to explicitly call the correct
superclass ctor, so that the initial capacity is 100.
Copy getMeanHeight() from Sky. You need to change it so that it traverses the
correct list of clouds, which is the instance of Sky2 that is executing
getMeanHeight(). Huge hint: its name is “this”.
What about adding clouds to Sky2? You could just copy add() from Sky and then
make a simple change. But there is an even simpler way.
Paste in the main() method from the Sky class. Change the line “Sky sky = new
Sky()” to “Sky2 sky = new Sky2()” To test Sky2, run it as an application.
Finally, export your work as a jar file called “CS46BHW2.jar”.
Explanation / Answer
I am not able to see option to upload jar here
so pasting all the classes .
Cloud.java
package weather;
public class Cloud {
private float bottom;
private float top;
Cloud(float bottom, float top) {
this.bottom = bottom;
this.top = top;
}
public float getHeight() {
return top - bottom;
}
public String rain() {
return "It is raining";
}
}
CirrusCloud.java
package weather;
public class CirrusCloud extends Cloud{
CirrusCloud(float bottom, float top) {
super(bottom, top);
}
public String rain() {
return "I cannot make rain";
}
}
StratusCloud.java
package weather;
public class StratusCloud extends Cloud{
StratusCloud(float bottom, float top) {
super(bottom, top);
}
}
CumulusCloud.java
package weather;
public class CumulusCloud extends Cloud{
CumulusCloud(float bottom, float top) {
super(bottom, top);
}
}
Sky.java
package weather;
import java.util.ArrayList;
public class Sky {
private ArrayList<Cloud> clouds;
Sky(){
clouds = new ArrayList<Cloud>(100);
}
public boolean add(Cloud c){
clouds.add(c);
return true;
}
public float getMeanHeight(){
float totalHeight = 0;
int totalClouds = clouds.size();
for(Cloud c: clouds){
totalHeight += c.getHeight();
}
return totalHeight/totalClouds;
}
public static void main(String[] args){
StratusCloud strat = new StratusCloud(100, 1000);
if (!strat.rain().startsWith("It is raining"))
System.out .println("Bad StratusCloud::rain");
CumulusCloud cumu = new CumulusCloud(200, 2000);
if (!cumu.rain().startsWith("It is raining"))
System.out .println("Bad CumulusCloud::rain");
CirrusCloud cirr = new CirrusCloud(300, 3000);
if (!cirr.rain().startsWith("I cannot make"))
System.out .println("Bad CirrusCloud::rain");
Sky sky = new Sky();
sky.add(strat);
sky.add(cumu);
sky.add(cirr);
float mean = sky.getMeanHeight();
if (mean < 1799 || mean > 1801)
System.out .println("Bad mean height: expected 1800, saw " + mean);
System.out .println("Everything (else) is ok");
}
}
Sky2.java
package weather;
import java.util.ArrayList;
public class Sky2 extends ArrayList<Cloud> {
private static final long serialVersionUID = 192989469958086558L;
Sky2() {
new ArrayList<Cloud>(100);
}
public float getMeanHeight() {
float totalHeight = 0;
int totalClouds = this.size();
for (Cloud c : this) {
totalHeight += c.getHeight();
}
return totalHeight / totalClouds;
}
public static void main(String[] args) {
StratusCloud strat = new StratusCloud(100, 1000);
if (!strat.rain().startsWith("It is raining"))
System.out.println("Bad StratusCloud::rain");
CumulusCloud cumu = new CumulusCloud(200, 2000);
if (!cumu.rain().startsWith("It is raining"))
System.out.println("Bad CumulusCloud::rain");
CirrusCloud cirr = new CirrusCloud(300, 3000);
if (!cirr.rain().startsWith("I cannot make"))
System.out.println("Bad CirrusCloud::rain");
Sky2 sky2 = new Sky2();
sky2.add(strat);
sky2.add(cumu);
sky2.add(cirr);
float mean = sky2.getMeanHeight();
if (mean < 1799 || mean > 1801)
System.out.println("Bad mean height: expected 1800, saw " + mean);
System.out.println("Everything (else) is ok");
}
}