Question: Please help me fix this Java project. I used Eclipse and it said The method/constructor is undefined for the type. Thanks Write a Weather class
Please help me fix this Java project. I used Eclipse and it said "The method/constructor is undefined for the type". Thanks
Write a Weather class encapsulating the concept of the weather forecast. It has the following attributes: the temperature and the sky conditions. The only valid sky conditions are sunny, snowy, cloudy, or rainy. Include a constructor, the accessors and mutators, and methods toString and equals. Temperature, in Fahrenheit, should be between -50 and +150; the default temperature value is 70. The default sky condition is sunny.
Include a method that converts Fahrenheit to Celsius. The formula to perform this conversion is below.
Celsius temperature = (Fahrenheit temperature - 32) * 5/9
Also include a method that checks whether the weather attributes are consistent. (NOTE: There are two cases where they are not consistent: when the temperature is above 32 and it is snowy; and when the temperature is below 32 and it is rainy.)
NOTE: The Weather class is a new class within your project. It should be defined as a separate file within your Java Project. This new class does not affect the name of your Java Project above.
Write a program that prompts the user to enter the temperature and sky condition. Display the temperature converted to Celsius and whether or not the weather attributes entered are consistent. The temperature should be formatted to exactly two decimal places.
Continuously prompt the user for input until the user chooses to end the program. Be sure to use clear prompts for the user.
Input Validation: Do not accept unreasonable/junk values from the user, which must be rejected and asked for another one.
---------------------------
public class TranWeatherForecast {
public static void main(String[] args) { System.out.println("Temperature cannot be less than -50 or greater than 150"); Weather t1 = new Weather(); t1.setSkyCondition("cloudy"); System.out.println("Temperature set to " + t1.getfahrenheit()); Weather t2 = new Weather(23.0,"sunny"); System.out.println("The Temperature of Weather Forecast #1 is " + t2.getfahrenheit() + "F or " + t2.toCelsius() ); System.out.println("The Sky condition of Weather Forecast #1 is " + t2.getSkyCondition() );
System.out.println("Weather Forecast #2 is Temperature " + t1.getfahrenheit() + "; sky: " + t1.getSkyCondition() );
System.out.println("Weather Forecast Temperature: " + t2.getfahrenheit()); if(t2.isConsistent()== true) System.out.println(t2.getSkyCondition() + " is Consistent"); else System.out.println(t2.getSkyCondition() + " is not Consistent");
System.out.println("Weather Forecast Temperature: " + t1.getfahrenheit()); if(t1.isConsistent()== true) System.out.println(t1.getSkyCondition() + " is Consistent"); else System.out.println(t1.getSkyCondition() + " is not Consistent");
if(t1.equals(t2) == true) System.out.println("Original Weather Forecast #1 and #2 are same"); else System.out.println("Original Weather Forecast #1 and #2 are different");
}
}
----------
import java.util.*;
class Weather {
private double fahrenheit; private String skyCondition; //Include a default constructor, public Weather() { //Temperature, in fahrenheit, should be between -50 and +150; the default value is 70, if needed. fahrenheit = 70; //The default sky condition is sunny. skyCondition = "sunny"; } //an overloaded constructor, public Weather(double f,String s) { fahrenheit = f; skyCondition = s; } //the accessors and public String getSkyCondition() { return skyCondition; } public double getfahrenheit() { return fahrenheit; } //mutators, and methods, public void setfahrenheit(double f) { fahrenheit = f; } public void setSkyCondition(String s) { skyCondition = s; } //toString() and public String toString() { return "Sky Condition is " + skyCondition + " and temperature is " + fahrenheit; } //equals(). public boolean equals(Weather T) { return ((fahrenheit == T.fahrenheit) && (skyCondition.equalsIgnoreCase(T.skyCondition))); } //Include a method that converts fahrenheit to Celsius. Celsius temperature = (fahrenheit temperature 32) * 5/9 public double toCelsius() { return (fahrenheit-32)*5/9; } //. Also include a 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). public boolean isConsistent() { if(fahrenheit <32 && !skycondition.equalsignorecase("snowy")) return false; if(fahrenheit>100 && !skyCondition.equalsIgnoreCase("sunny")) return false; return true; }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
