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,
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 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 dont know how to do this, ask on Piazza. DONT 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. Thats 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. 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.