Question

In: Computer Science

Write a class encapsulating the concept of the weather forecast, assuming that it has the following...

Write a class encapsulating the concept of the weather forecast, assuming that it has the following attributes: the temperature and the sky conditions (e.g. sunny, snowy, cloudy, rainy, etc.).  Include a default constructor, an overloaded constructor, accessors and mutators, and the methods, toString() and equals(). Temperature, in Fahrenheit, should be between -50 and +150; the default value is 70, if needed. The default sky condition is sunny. In Addition, include a method that converts Fahrenheit to Celsius. Celsius temperature = (Fahrenheit temperature – 32) * 5/9. Also include a boolean method that checks whether the weather attributes are consistent (there are two cases where they are not consistent: when the temperature is below 32 and it is not snowy, and when the temperature is above 100 and it is not sunny). Write a client class to test all the methods in your class.

Solutions

Expert Solution

public class WeatherForecast
{
private double temperature; // in Fahrenheit
private String sky;
 
/**
* initializes temperature to 70.0 and sky to "sunny"
* calls mutator methods to validate these default values
*/
public WeatherForecast( )
{
temperature = 70;
sky = "sunny";
 
}
 
/**
* Overloaded constructor:
* Allows client to set beginning values for temperature and sky
* This constructor takes two parameters
* Calls mutator methods to validate new values
* @param newTemperature the temperature for the weather forecast
* @param newSky the sky conditions for the weather forecast
*/
public WeatherForecast( double newTemperature, String newSky )
{
temperature = newTemperature;
sky = newSky;
}
 
/** getTemperature method
* @return the temperature
*/
public double getTemperature( )
{
return temperature; // add code here, insted of o.o we should return tempature
}
 
/**
* Mutator method:
* Allows client to set value of temperature
* setTemperature sets the value of temperature
* to 70.0 if newTemperature is less than -50 or greater than 150
* @param newTemperature the new number of temperature
*/
public void setTemperature( double newTemperature )
{
// add code here // make sure falls in range of -50 to 100, need if else
if(temperature >= 0)
temperature = newTemperature;
else
{
System.err.println( "Miles driven cannot be negative." );
System.err.println( "Value not changed."); 
}
} 
 
/** getSky method
* @return the sky conditions
*/
public String getSky( )
{
return sky;
// add code here, returns the string variable that represents your sky. You are going to some error checking and a syst
// system.err make sure equal to sunny snowy cloudy rainy
}
 
/**
* Mutator method:<BR>
* Allows client to set value of sky
* setSky sets the value of sky
* to "sunny" if newSky is neither "sunny", "snowy",
* "cloudy", nor "rainy"
* This method is not case sensitive
* @param newSky the new sky condition
*/
 
// @return the temperature and the sky conditions for the weather forecast
@Override
public String toString( )
{
return( "temperature: " + temperature + "; sky: " + sky );
}
 
/** 
* Compares two WeatherForecast objects for the same field values
* @param wf another WeatherForecast object
* @return a boolean, true if this object
* has the same field values as the parameter wf
*/
public boolean equals( WeatherForecast wf )
{
return ( Math.abs( temperature - wf.temperature ) < 0.0001 
&& sky.equals( wf.sky ) );
}
 
/**
* fahrenheitToCelsius method
* Computes the degrees Celsius temperature
* @return a double, the temperature value in degrees Celsius
*/
public double fahrenheitToCelsius( )
{
     temperature = temperature - 32;
     temperature = temperature * 5;
     temperature = temperature / 9;
     return temperature;
// add code here complete method, do the coversioan return a double data type
}
 
/**
* Checks if the temperature value and the sky conditions are compatible
* @return a boolean, false if the temperature is below 32 and the sky is not snowy,
* or if the temperature is greater than 100 and the sky is not sunny, true otherwise 
*/
public boolean isConsistent( )
{
return !( ( temperature < 32 && !sky.equals( "snowy" ) ) 
|| ( temperature > 100 && !sky.equals( "sunny" ) ) );
}
}

Related Solutions

Write a class encapsulating the concept of a Student, assuming that the Student has the following...
Write a class encapsulating the concept of a Student, assuming that the Student has the following attributes: the name of the student, the average of the student, and the student’s GPA. Include a default constructor, an overloaded constructor, the accessors and mutators, and methods toString() and equals(). Also include a method returning the letter grade base on the following range for the average: Average Range Letter Grade 90-100 A 85-89   B+ 80-84 B 75-79 C+ 70-74 C 65-69 D+ 60-64...
Write a class encapsulating the concept of a Student, assuming that a student has the following...
Write a class encapsulating the concept of a Student, assuming that a student has the following attributes: last name, first name, id, array of grades. Include a constructor, the accessors and mutators, and method toString. Also code the following methods: one returning the GPA using the array of grades (assuming each grade represents a course grade and all courses have the same number of credit hours) and a method to add a course grade to the array of grades (this...
Write a class encapsulating the concept of a student, assuming a student has the following attributes:...
Write a class encapsulating the concept of a student, assuming a student has the following attributes: a name, a Social Security number, and a GPA (for instance, 3.5). Include a constructor, the accessors and mutators, and methods toString and equals. Write a client class (test driver with main method) to test all the methods in your class (create 2 student objects, print the name, social security number and GPA of the 2 students, check if the two student’s GPA is...
Write a class encapsulating the concept of a Student Employee, assuming that a student has the...
Write a class encapsulating the concept of a Student Employee, assuming that a student has the following attributes: last name, first name, id, array of hours worked weekly. Include a constructor, the accessors and mutators, and method toString. Also code the following methods: one returning the average weekly hours and a method to add the hours to the array of hoursWorked (this means creating a large array). Let this array store the weekly hours (not daily hours) worked for each...
In the programming language java: Write a class encapsulating the concept of a telephone number, assuming...
In the programming language java: Write a class encapsulating the concept of a telephone number, assuming a telephone number has only a single attribute: aString representing the telephone number. Include a constructor, the accessor and mutator, and methods 'toString' and 'equals'. Also include methods returning the AREA CODE (the first three digits/characters of the phone number; if there are fewer than three characters in the phone number of if the first three characters are not digits, then this method should...
Write a class encapsulating the concept of a corporate name (for example, IBM), assuming a corporate...
Write a class encapsulating the concept of a corporate name (for example, IBM), assuming a corporate name has the following attribute: the corporate name. Include a constructor, the accessors and mutators, and methods toString() and equals(). Also include and method that returns a potential domain name by adding a www. at the beginning and .com at the end of the corporate name (for instance, if the corporate name is IBM, that method should return www.ibm.com). Write a client class to...
Write a class encapsulating a board game. A board game has the following additional attributes: the...
Write a class encapsulating a board game. A board game has the following additional attributes: the number of players and whether the game can end in a tie. Code the constructor and the toString method of the new class. You also need to include a client class(with the main method) to test your code. code in Python
This is an abstract class to represent a weather sensor. Weather sensors * report weather parameters...
This is an abstract class to represent a weather sensor. Weather sensors * report weather parameters to a weather station. Sensor readings are pulled by * calling the method read. Each sensor has a fixed failure rate that switches * the sensor state to damaged and needs to be fixed. A damaged sensor will * throw an exception when trying to get its reading. * */ public abstract class WSensor {    /*    * Your Task: Declare attributes to...
10. Weather forecast : athe table below indicates the accuracy of a local weather report with...
10. Weather forecast : athe table below indicates the accuracy of a local weather report with respect to rain or no rain over the past year. This table gives the results of 365 consecutive days and compares whether it rained or not to whether or not rain was predicted. Did it actually rain? yes No Report predicted Rain 102 18 Report predicted No Rain 40 205 If one day is randomly selected from these 365 days, what is the probability...
1. Write a superclass encapsulating a rectangle. A rectangle has two attributes representing the width and...
1. Write a superclass encapsulating a rectangle. A rectangle has two attributes representing the width and the height of the rectangle. It has methods returning the perimeter and the area of the rectangle. This class has a subclass, encapsulating a parallelepiped, or box. A parallelepiped has a rectangle as its base, and another attribute, its length; it has two methods that calculate and return its area and volume. You also need to include a client class (with the main method)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT