I need to write a class that encapsulates this program. It should supplement the
ID: 3776727 • Letter: I
Question
I need to write a class that encapsulates this program. It should supplement the constructor, accessors, and mutators. As well as methods like toString and equals.The program should not be altered.
public class WeatherForecastClient
{
public static void main( String [] args )
{
WeatherForecast wf1 = new WeatherForecast( 23, "sunny" );
WeatherForecast wf2 = new WeatherForecast( 200, "cloudy" );
System.out.print( "The temperature of weather forecast #1 is " + wf1.getTemperature( ) );
System.out.println( "F or " + wf1.fahrenheitToCelsius( ) + "C");
System.out.println( "The sky condition of weather forecast #1 is " + wf1.getSky( ) );
System.out.println( "Weather forecast #2 is " + wf2.toString( ) + " " );
if ( wf1.isConsistent( ) )
System.out.println( "weather forecast " + wf1 + " is consistent" );
else
System.out.println( "weather forecast " + wf1 + " is not consistent" );
if ( wf2.isConsistent( ) )
System.out.println( "weather forecast " + wf2 + " is consistent" );
else
System.out.println( "weather forecast " + wf2 + " is not consistent" );
System.out.println( );
if ( wf1.equals( wf2 ) )
System.out.println( "Original weather forecasts #1 and #2 are identical" );
else
System.out.println( "Original weather forecasts #1 and #2 are different" );
wf2.setTemperature( 23 );
wf2.setSky( "sunny" );
if ( wf1.equals( wf2 ) )
System.out.println( "Original weather forecast #1 and modified weather forecast #2 are identical" );
else
System.out.println( "Original weather forecast #1 and modified weather forecast #2 are different" );
}
}
Explanation / Answer
package com.temp.cast;
public class WeatherForecast {
private int temperature;
private String sky;
public WeatherForecast(int temperature, String sky) {
this.temperature = temperature;
this.sky = sky;
}
public String fahrenheitToCelsius() {
temperature = ((temperature - 32)*5)/9;
String temp=Integer.toString(temperature);
return temp;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((sky == null) ? 0 : sky.hashCode());
result = prime * result + temperature;
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
WeatherForecast other = (WeatherForecast) obj;
if (sky == null) {
if (other.sky != null)
return false;
} else if (!sky.equals(other.sky))
return false;
if (temperature != other.temperature)
return false;
return true;
}
/**
* @return the temperature
*/
public int getTemperature() {
return temperature;
}
/**
* @param temperature the temperature to set
*/
public void setTemperature(int temperature) {
this.temperature = temperature;
}
/**
* @return the sky
*/
public String getSky() {
return sky;
}
/**
* @param sky the sky to set
*/
public void setSky(String sky) {
this.sky = sky;
}
public boolean isConsistent() {
// TODO Auto-generated method stub
if(temperature<40&&temperature>0)
{
return true;
}else
return false;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "WeatherForecast [temperature=" + temperature + ", sky=" + sky
+ "]";
}
}
package com.temp.cast;
public class WeatherForecastClient
{
public static void main( String [] args )
{
WeatherForecast wf1 = new WeatherForecast( 23, "sunny" );
WeatherForecast wf2 = new WeatherForecast( 200, "cloudy" );
System.out.print( "The temperature of weather forecast #1 is " + wf1.getTemperature( ) );
System.out.println( "F or " + wf1.fahrenheitToCelsius( ) + "C");
System.out.println( "The sky condition of weather forecast #1 is " + wf1.getSky( ) );
System.out.println( "Weather forecast #2 is " + wf2.toString( ) + " " );
if ( wf1.isConsistent( ) )
System.out.println( "weather forecast " + wf1 + " is consistent" );
else
System.out.println( "weather forecast " + wf1 + " is not consistent" );
if ( wf2.isConsistent( ) )
System.out.println( "weather forecast " + wf2 + " is consistent" );
else
System.out.println( "weather forecast " + wf2 + " is not consistent" );
System.out.println( );
if ( wf1.equals( wf2 ) )
System.out.println( "Original weather forecasts #1 and #2 are identical" );
else
System.out.println( "Original weather forecasts #1 and #2 are different" );
wf2.setTemperature( 23 );
wf2.setSky("sunny");
System.out.println( "wf2 temparature in celcius is"+ wf2.fahrenheitToCelsius( ) + "C");
if ( wf1.equals( wf2 ) )
System.out.println( "Original weather forecast #1 and modified weather forecast #2 are identical" );
else
System.out.println( "Original weather forecast #1 and modified weather forecast #2 are different" );
}
}
output
The temperature of weather forecast #1 is 23F or -5C
The sky condition of weather forecast #1 is sunny
Weather forecast #2 is WeatherForecast [temperature=200, sky=cloudy]
weather forecast WeatherForecast [temperature=-5, sky=sunny] is not consistent
weather forecast WeatherForecast [temperature=200, sky=cloudy] is not consistent
Original weather forecasts #1 and #2 are different
wf2 temparature in celcius is-5C
Original weather forecast #1 and modified weather forecast #2 are identical