Question: Java Program- need to fix to prompt user imput and also Add a method to your Forecast class that calls print() and prints the result
Java Program- need to fix to prompt user imput and also
Add a method to your Forecast class that calls print() and prints the result to a file. You will need to add a try/catch block around your file I/O code; your try/catch block will include exception handling. At a minimum, catch errors dealing with (1) FileNotFoundException, (2) IOException, and (3) all other exceptions. Its fine if you are handling these errors simply by printing some information about the error that is caught.
Include in your main method a method call to your new file output method.
public class Forecast {
private double degreesInFahrenheit; private String skyCondition; private int rainChance; public Object setDegreesInFahrenheit; public Forecast() { this.skyCondition = "clear"; this.degreesInFahrenheit = 72.0; this.rainChance = 0; } public Forecast(double degreesInFahrenheit, String skyCondition, int rainChance) { setDegreesInFahrenheit(degreesInFahrenheit); setRainChance(rainChance); setSkyCondition(skyCondition); } public double getDegreesInFahrenheit() { return degreesInFahrenheit; }
public void setDegreesInFahrenheit(double degreesInFahrenheit) { if(degreesInFahrenheit < -100 || degreesInFahrenheit > 150) { this.degreesInFahrenheit = 72.0; } else { this.degreesInFahrenheit = degreesInFahrenheit; } }
public String getSkyCondition() { return skyCondition; }
public void setSkyCondition(String skyCondition) { if (skyCondition == null || skyCondition == "") { this.skyCondition = "clear"; } else { this.skyCondition = skyCondition; } } public int getRainChance() { return rainChance; } public void setRainChance(int rainChance) { if (rainChance < 0 || rainChance > 100) { this.rainChance = 0; } else { this.rainChance = rainChance; } } public double FahrenheitToCelsius (double temp) { return (temp - 32) * 5.0/9.0; } public double CelsiusToFahrenheit(double temp) { return (temp * 9.0/5.0) + 32; } public double FahrenheitToKelvin (double temp) { return (temp - 273.15 - 32) * 5.0/9.0; } public double KelvinToFahrenheit(double temp) { return (temp - 273.15) * 9.0/5.0 + 32; } public boolean willItRain() { if (rainChance > 50) return true; return false; } public void print() { System.out.println("Forecast [degrees in Fahrenheit = " + degreesInFahrenheit + ", sky condition = " + skyCondition + ", chance of rains = " + rainChance + "]"); } }
import java.util.Scanner;
public class Main {
public static void main(String[] args) { Scanner scnr = new Scanner(System.in);
Forecast f = new Forecast(); f.getDegreesInFahrenheit(); f.getRainChance(); f.getSkyCondition(); f.print(); System.out.println("Celsius to Farenheit: "); System.out.println("100 Celsius = " + f.CelsiusToFahrenheit(100)); System.out.println("Farenheit to Celsius: "); System.out.println("100 Farenheit = " + f.FahrenheitToCelsius(100)); System.out.println("Kelvin to Farenheit: "); System.out.println("350 Lelvin" + f.KelvinToFahrenheit(100)); System.out.println("Farenheit to Kelvin: "); System.out.println("100 Farenheit = " + f.FahrenheitToKelvin(100)); }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
