Question
Hey guys! Can you please help me with this coding assignment? So I've written all the classes, I just get an error when I run
Hey guys! Can you please help me with this coding assignment?
So I've written all the classes, I just get an error when I run the tester my professor provided us.
Here are the assignemnt directions:
"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.
Testing your work
Import WeatherTester.java, which you downloaded along with this document. The easiest way to do this is to just drag-and-drop it into the weather package in the Package Explorer. Run WeatherTester as an app. For this assignment, nothing works unless almost everything works, so there isnt any partial credit. Youll either get 100 points or 0 points. If you get 0 points the tester will tell you exactly what the problem is, so you can fix your bug and get 100."
Here is my CirrusCloud class:
package weather;
//class CirrusCloud is a subclass of Cloud
public class CirrusCloud extends Cloud
{
//constructor that takes top and bottom as parameters
public CirrusCloud(float thatbottom, float thattop)
{
//pass top and bottom to the super class constructor
super(thatbottom, thattop);
}
//override the rain method
public String rain()
{
return "I cannot make rain";
}
}
Here is my Cloud test:
package weather;
public class Cloud
{
//instance variables
private float bottom;
private float top;
//constructor that takes top and bottom as parameters and initializes the instance variables
public Cloud(float thatbottom, float thattop)
{
this.bottom = thatbottom;
this.top = thattop;
}
//method that calculates and returns height
public float getHeight()
{
//compute and return top-bottom
return this.top - this.bottom;
}
//method rain
public String rain()
{
//return a string "It is raining"
return "It is raining";
}
}
Here is my CumulusCloud test:
package weather;
public class Cloud
{
//instance variables
private float bottom;
private float top;
//constructor that takes top and bottom as parameters and initializes the instance variables
public Cloud(float thatbottom, float thattop)
{
this.bottom = thatbottom;
this.top = thattop;
}
//method that calculates and returns height
public float getHeight()
{
//compute and return top-bottom
return this.top - this.bottom;
}
//method rain
public String rain()
{
//return a string "It is raining"
return "It is raining";
}
}
Here is my StratusCloud class:
package weather;
//class StratusCloud is a subclass of Cloud
public class StratusCloud extends Cloud
{
//constructor that takes top and bottom as parameters
public StratusCloud(float thatbottom, float thattop)
{
//pass top and bottom to the super class constructor
super(thatbottom, thattop);
}
}
Here is my Sky class:
package weather;
import java.util.ArrayList;
public class Sky
{
//declare an array list
private ArrayList
//ctor that creates the clouds array list with a capacity 100
public Sky()
{
clouds = new ArrayList
}
//method add that takes variable of type Cloud as parameter and return a boolean
public boolean add(Cloud c)
{
//add c to the arraylist
clouds.add(c);
//always returns true
return true;
}
//method that returns the average height of all the clouds in the array list
public float getMeanHeight()
{
float sum = 0;
//loop through the array list and compute the sum of the heights
for(int i = 0; i < clouds.size(); i++)
{
sum = sum + clouds.get(i).getHeight();
}
//compute and return average height
return sum / clouds.size();
}
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");
}
}
Here is my Sky2 class:
package weather;
import java.util.ArrayList;
public class Sky2 extends ArrayList
{
//ctor calls ArrayList
public Sky2()
{
super(100);
}
//copy getMeanHeight() from Sky class
//change it so that it traverses the correct list of clouds (change clouds to this)
public float getMeanHeight()
{
float sum = 0;
int count = 0;
//loop through the array list and compute the sum of the heights
for(Cloud c: this)
{
count++;
sum += c.getHeight();
}
//compute and return average height
return sum / count;
}
//copy add method from Sky and then change (change clouds to this)
//method add that takes variable of type Cloud as parameter and return a boolean
public boolean add(Cloud c)
{
//add c to the arraylist
super.add(c);
//always returns true
return true;
}
//copy main method from Sky class
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");
//Change the line Sky sky = new Sky() to Sky2 sky = new Sky2()
Sky2 sky = new Sky2();
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");
}
}
Here is the tester typed out:
package weather;
import java.lang.reflect.Field;
import java.util.Set;
import java.util.TreeSet;
public class WeatherTester {
private static boolean testBasicClassesExist()
{
// Collect names of missing classes.
String[] basicNames = { "Cloud", "CirrusCloud", "CumulusCloud", "StratusCloud", "Sky" };
Set missing = new TreeSet<>();
for (String name: basicNames)
{
String fullName = "weather." + name;
try
{
Class.forName(fullName);
}
catch (LinkageError x)
{
System.out.println("Phil says this should never happen.");
}
catch (ClassNotFoundException x)
{
missing.add(name);
}
}
// Nothing missing.
if (missing.isEmpty())
System.out.println("All the basic class are present");
// 1 or more basic (i.e. not Sky2) classes are missing.
else
{
System.out.println("The following class(es) are missing:");
for (String m: missing)
System.out.println( m + " ");
}
return missing.isEmpty();
}
public static boolean testSky2Exists()
{
try
{
Class.forName("weather.Sky2");
}
catch (LinkageError x)
{
System.out.println("This should never happen.");
return false;
}
catch (ClassNotFoundException x)
{
System.out.println("No class Sky2");
return false;
}
System.out.println("Class Sky2 found");
return true;
}
public static boolean testSkyStructure()
{
try
{
Class clazz = Class.forName("weather.Sky");
Field cloudsField = clazz.getDeclaredField("clouds");
if (cloudsField.getType() != java.util.ArrayList.class)
{
System.out.println("Unexpected type for clouds in Sky: " + cloudsField.getType() +
" should be ArrayList");
return false;
}
}
catch (NoSuchFieldException x)
{
System.out.println("No field clouds in class Sky");
return false;
}
catch (ClassNotFoundException x)
{
System.out.println("No class Sky2"); // should be caught in testSky2Exists
return false;
}
System.out.println("Sky2 structure is correct");
return true;
}
public static boolean testSkyBehavior()
{
StratusCloud strat = new StratusCloud(100, 1000);
if (!strat.rain().startsWith("It is raining"))
{
System.out.println("Bad StratusCloud::rain");
return false;
}
CumulusCloud cumu = new CumulusCloud(200, 2000);
if (!cumu.rain().startsWith("It is raining"))
{
System.out.println("Bad CumulusCloud::rain");
return false;
}
CirrusCloud cirr = new CirrusCloud(300, 3000);
if (!cirr.rain().startsWith("I cannot make"))
{
System.out.println("Bad CirrusCloud::rain");
return false;
}
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("In Sky, bad mean height for 3 clouds "
+ "with bottom:top = { 100:1000, 200:2000, 300:3000 }");
return false;
}
else
{
System.out.println("Sky gives correct mean");
return true;
}
}
public static boolean testSky2Structure()
{
try
{
Class.forName("weather.Sky2").getDeclaredField("clouds"); // should throw because clouds should not exist.
System.out.println("Sky2 should not contain field clouds");
return false;
}
catch (NoSuchFieldException|ClassNotFoundException x) { }
try
{
Class superclass = Class.forName("weather.Sky2").getSuperclass();
if (superclass != java.util.ArrayList.class)
{
System.out.println("Sky2 does not extend ArrayList");
return false;
}
}
catch (ClassNotFoundException x) { }
System.out.println("Sky2 structure is correct ");
return true;
}
public static boolean testSky2Behavior()
{
StratusCloud strat = new StratusCloud(100, 1000);
if (!strat.rain().startsWith("It is raining"))
{
System.out.println("Bad StratusCloud::rain");
return false;
}
CumulusCloud cumu = new CumulusCloud(200, 2000);
if (!cumu.rain().startsWith("It is raining"))
{
System.out.println("Bad CumulusCloud::rain");
return false;
}
CirrusCloud cirr = new CirrusCloud(300, 3000);
if (!cirr.rain().startsWith("I cannot make"))
{
System.out.println("Bad CirrusCloud::rain");
return false;
}
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("In Sky2, bad mean height for 3 clouds "
+ "with bottom:top = { 100:1000, 200:2000, 300:3000 }");
return false;
}
else
{
System.out.println("Sky2 gives correct mean");
return true;
}
}
public static boolean testCloudSubclassing()
{
String[] subClouds = { "CirrusCloud", "CumulusCloud", "StratusCloud" };
String message = "";
for (String subCloud: subClouds)
{
try
{
Class superclass = Class.forName("weather." + subCloud).getSuperclass();
if (superclass != Class.forName("weather.Cloud"))
message += subCloud + " does not extend Cloud. ";
}
catch (ClassNotFoundException x) { }
}
message = message.trim();
if(!message.isEmpty())
System.out.println(message);
else
System.out.println("Subclassing for all the cloud classes is correct");
return message.isEmpty();
}
public static boolean testCloudCtors()
{
String[] allClouds = { "Cloud", "CirrusCloud", "CumulusCloud", "StratusCloud" };
String message = "";
Class[] expectedArgTypes = { float.class, float.class };
for (String cloud: allClouds)
{
try
{
Class.forName("weather." + cloud).getDeclaredConstructor(expectedArgTypes);
}
catch (ClassNotFoundException x)
{
message += cloud + " not found.";
}
catch (NoSuchMethodException x)
{
message += cloud + " does not have a (float, float) ctor. ";
}
}
message = message.trim();
if (!message.isEmpty())
System.out.println(message);
else
System.out.println("Ctors for all the cloud classes are correct");
return message.isEmpty();
}
public static boolean testSkyAdd()
{
try
{
Class[] expectedTypes = { Class.forName("weather.Cloud") };
Class.forName("weather.Sky").getMethod("add", expectedTypes);
}
catch (ClassNotFoundException x)
{
System.out.println("No weather.Sky class");
return false;
}
catch (NoSuchMethodException x)
{
System.out.println("No add(Cloud) method in Sky");
return false;
}
return true;
}
public static void main(String[] args)
{
boolean pass = true;
pass &= testBasicClassesExist();
pass &= testSky2Exists();
pass &= testSkyStructure();
pass &= testSkyBehavior();
pass &= testSky2Structure();
pass &= testSky2Behavior();
pass &= testCloudSubclassing();
pass &= testCloudCtors();
pass &= testSkyAdd();
if (pass)
System.out.println(" Everything ok: 100 points");
else
System.out.println(" 0 points");
}
}
Any help is appreciated!!
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