III. (10 points) Write a complete Java program to handle weather forecasts. The
ID: 3719719 • Letter: I
Question
III. (10 points) Write a complete Java program to handle weather forecasts. The program should prompt the user for the number of days they would like forecasted. It should read weather data from forecast.txt, which you may assume is in the following format:
Tuesday 75 windy
Wednesday 70 sunny
Thursday 68 breezy …
The program should print the weather forecast only for the number of days requested by the user.
VI. (10 points) Suppose a ?le named “song.txt” contains a carol, beginning with the following lines:
Chestnuts roasting on an open fire
Jack Frost nipping at your nose
Yuletide carols being sung by the fire …
Write a main method to print the contents of the ?le backwards. You may assume all necessary imports, but be sure to handle any possible exceptions. Each letter should be reversed, as well as each line. That is, the last line should print as follows:
erif nepo na no gnitsaor stuntsehC
Explanation / Answer
========================================================================
Ist part:
=========================================================================
package weather;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.InputMismatchException;
import java.util.Scanner;
public class ShowWeather {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int numberDays=0;
try {
numberDays=sc.nextInt();
}catch (InputMismatchException e) {
System.out.println("Enter valid Number");
System.exit(0);
}
if(numberDays<=0)
try {
throw new InvalidInputException("No. of days should be greater than 0");
} catch (InvalidInputException e1) {
System.out.println(e1.getMessage());
}
String path="Enter the Path of forecast.txt file";
BufferedReader reader=null;
BufferedReader dataReader=null;
try {
reader=new BufferedReader(new FileReader(path));
dataReader=new BufferedReader(new FileReader(path));
for(int i=0; i<numberDays; i++) {
if(dataReader.readLine()==null) {
throw new DataNotAvailableException("Data Not Available for the specified days");
}
}
for(int i=0; i<numberDays; i++) {
System.out.println(reader.readLine());
}
} catch (FileNotFoundException e) {
// e.printStackTrace();
System.out.println("File Not Found");
} catch (IOException e) {
// e.printStackTrace();
System.out.println("Something went wrong!!!");
}
catch (Exception e) {
System.out.println(e.getMessage());
}
finally {
try {
reader.close();
dataReader.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
}
class DataNotAvailableException extends Exception{
public DataNotAvailableException(String msg) {
super(msg);
}
}
class InvalidInputException extends Exception {
public InvalidInputException(String msg) {
super(msg);
}
}
=====================================================================================
Part 2:
=====================================================================================
package song;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class BackwardPrint {
public static void main(String[] args) {
//path is a string representation of the location of song.txt file
String path="C:\Users\Nasir Bhat\eclipse-workspace\WeatherForecast\src\song\song.txt";
BufferedReader reader=null;
try {
reader=new BufferedReader(new FileReader(path));
String readLine=null;
String text="";
while ((readLine = reader.readLine()) != null) {
readLine.trim();
for(int i=0; i< readLine.length(); i++) {
text+=readLine.charAt(i);
}
text+=" ";
}
text=text.substring(0, text.length()-1);
StringBuffer sb=new StringBuffer(text.toString());
sb.reverse();
System.out.println(sb.toString());
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
finally {
try {
if(reader!=null)
reader.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
}