Question: Using the following code, create a mathematical function to pull statistics from a file for the past 7 , 1 4 , and 3 0

Using the following code, create a mathematical function to pull statistics from a file for the past 7,14, and 30 days. Also in the code, create the file needed to save the data.
-------
import java.util.ArrayList;
import java.util.Scanner;
class WeatherData {
protected String date;
protected int highTemperature;
protected int lowTemperature;
protected String weatherCondition;
public WeatherData(String date, int highTemperature, int lowTemperature, String weatherCondition){
this.date = date;
this.highTemperature = highTemperature;
this.lowTemperature = lowTemperature;
this.weatherCondition = weatherCondition;
}
}
class DailyWeatherData extends WeatherData {
public DailyWeatherData(String date, int highTemperature, int lowTemperature, String weatherCondition){
super(date, highTemperature, lowTemperature, weatherCondition);
}
}
class WeeklyWeatherData extends WeatherData {
private double averageTemperature;
private String predominantWeatherCondition;
public WeeklyWeatherData(String date, int highTemperature, int lowTemperature, String weatherCondition,
double averageTemperature, String predominantWeatherCondition){
super(date, highTemperature, lowTemperature, weatherCondition);
this.averageTemperature = averageTemperature;
this.predominantWeatherCondition = predominantWeatherCondition;
}
}
class FortnightlyWeatherData extends WeatherData {
private int highestTemperature;
private int lowestTemperature;
public FortnightlyWeatherData(String date, int highTemperature, int lowTemperature, String weatherCondition,
int highestTemperature, int lowestTemperature){
super(date, highTemperature, lowTemperature, weatherCondition);
this.highestTemperature = highestTemperature;
this.lowestTemperature = lowestTemperature;
}
}
class MonthlyWeatherData extends WeatherData {
public MonthlyWeatherData(String date, int highTemperature, int lowTemperature, String weatherCondition){
super(date, highTemperature, lowTemperature, weatherCondition);
}
}
class WeatherDataAnalyzer {
private ArrayList weatherDataList;
public WeatherDataAnalyzer(){
weatherDataList = new ArrayList<>();
}
public void addWeatherData(WeatherData weatherData){
weatherDataList.add(weatherData);
}
// Placeholder methods for retrieving statistical information
public void showStatsForLast7Days(){
System.out.println("Statistics for the last 7 days.");
// Implement your statistics computation logic here
}
public void showStatsForLast14Days(){
System.out.println("Statistics for the last 14 days.");
// Implement your statistics computation logic here
}
public void showStatsForLast30Days(){
System.out.println("Statistics for the last 30 days.");
// Implement your statistics computation logic here
}
}
public class Main {
public static void main(String[] args){
WeatherDataAnalyzer analyzer = new WeatherDataAnalyzer();
Scanner scanner = new Scanner(System.in);
while (true){
System.out.println("Enter the date (YYYY-MM-DD):");
String date = scanner.nextLine();
System.out.println("Enter the high temperature:");
int highTemp = scanner.nextInt();
System.out.println("Enter the low temperature:");
int lowTemp = scanner.nextInt();
scanner.nextLine(); // consume the newline character
System.out.println("Enter the weather condition (sunny, rainy, cloudy, windy):");
String condition = scanner.nextLine();
DailyWeatherData dailyData = new DailyWeatherData(date, highTemp, lowTemp, condition);
analyzer.addWeatherData(dailyData);
System.out.println("Do you want to quit? (yes/no)");
String quit = scanner.nextLine();
if ("yes".equalsIgnoreCase(quit)){
break;
}
System.out.println("Would you like to pull statistics? (7,14,30 days or no)");
String statsOption = scanner.nextLine();
switch (statsOption){
case "7":
analyzer.showStatsForLast7Days();
break;
case "14":
analyzer.showStatsForLast14Days();
break;
case "30":
analyzer.showStatsForLast30Days();
break;
default:
System.out.println("Continuing data collection...");
break;
}
}
scanner.close();
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!