Question: 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
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.
import java.util.*;
public class Weath
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
0System.out.println(new Forecast());
Forecast f=new Forecast(66.0f,"cloudy",50 );
System.out.println("Testing all the methods in forecast");
System.out.println("Fahrenheit:60.0 celsius is "+f.farhenToCelsi(60.0f));
System.out.println("celsius:60.0 Fahrenheit is "+f.celsiToFarhen(60.0f));
System.out.println("Fahrenheit:60.0 kelvin is "+f.farhenToKelvi(60.0f)) ;
System.out.println("kelvin:600.0 Fahrenheit is "+f.kelviToFarhen(600.0f));
}
}
class Forecast
{
float temparature=72.0f;
String skycondition="clear";
int rain=0;
Forecast(float temp,String sky,int rain)
{
if(temp<-100 && temp>150)
this.temparature=72.0f;
else
this.temparature=temp;
if(sky=="")
this.skycondition="clear";
else
this.skycondition=sky;
if(rain<0 || rain>100)
this.rain=0;
else
this.rain=rain;
System.out.println("Printing the modified values");
System.out.println("-----------------------------");
print();
System.out.println("-----------------------------");
; }
Forecast()
{
System.out.println("Initial values in variables") ;
System.out.println("-----------------------------");
print();
System.out.println("-----------------------------");
System.out.println("-----------------------------");
System.out.println();
}
void print()
{
System.out.println("Displaying the Forecast information");
System.out.println("Temparature is "+this.temparature);
System.out.println("sky condition is "+this.skycondition);
System.out.println("rain is "+this.rain);
}
float farhenToCelsi(float ftemp)
{
return (float)(ftemp-32)*((float)5/9);
}
float celsiToFarhen(float ctemp)
{
return (float)(ctemp*(9/5))+32;
}
float farhenToKelvi(float ftemp)
{
return (float)(ftemp+459.67)*((float)5/9);
}
float kelviToFarhen(float ktemp)
{
return (ktemp*(9/5))-459.67f;
}
boolean isItRain()
{
if(this.rain>0 && this.rain<=100)
return true;
else
return false;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
