Question: import java.io . File; import java.io . FileNotFoundException; import java.util.Scanner; public class TemperatureProcessor { / / this method converts a single Kelvin temperature to Celsius

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class TemperatureProcessor {
// this method converts a single Kelvin temperature to Celsius
private static double k2c(double temp){
return temp + Celsius.ZEROK_IN_CELSIUS;
}
// this method converts a single Celsius temperature to Kelvin
private static double c2k(double temp){
return temp - Celsius.ZEROK_IN_CELSIUS;
}
// this method converts a single Celsius temperature to Fahrenheit
private static double f2c(double temp){
return (temp -32)*5/9;
}
// this method converts a single Fahrenheit temperature to Celsius
private static double c2f(double temp){
return temp *9/5+32;
}
public static void main(String[] args){
System.out.println("This program processes temperature data from a file and reports statistics");
try (Scanner170 input = new Scanner170(System.in)){
System.out.print("Enter the name of the temperature file: ");
String fileName = input.nextLine();
Scanner170 fileScanner = new Scanner170(new File(fileName));
Celsius[] temps = new Celsius[100];
int validTempCount = readTemperatureFile(fileScanner, temps);
printTemperatureData(fileName, temps, validTempCount);
fileScanner.close();
} catch (FileNotFoundException e){
System.err.println("File not found. Please check the file name and try again.");
}
}
public static int readTemperatureFile(Scanner170 reader, Celsius[] temps){
int count =0;
while (reader.hasNextLine() && count < temps.length){
String line = reader.nextLine();
double celsiusTemp = processTempLine(line);
if (celsiusTemp >= Celsius.ZEROK_IN_CELSIUS){
temps[count++]= new Celsius(celsiusTemp);
}
}
return count;
}
public static double processTempLine(String line){
try {
String[] parts = line.trim().split("\\s+");
if (parts.length !=2){
return Celsius.ZEROK_IN_CELSIUS -1;
}
double value = Double.parseDouble(parts[0]);
String unit = parts[1].toLowerCase();
switch (unit){
case "k":
return k2c(value);
case "c":
return value;
case "f":
return f2c(value);
default:
return Celsius.ZEROK_IN_CELSIUS -1;
}
} catch (NumberFormatException e){
return Celsius.ZEROK_IN_CELSIUS -1;
}
}
public static void printTemperatureData(String fileName, Celsius[] temps, int count){
if (count ==0){
System.out.println("No valid temperatures found in this file: "+ fileName);
return;
}
double totalC =0, totalK =0, totalF =0;
int aboveAverageCount =0;
for (int i =0; i < count; i++){
double c = temps[i].getC();
totalC += c;
totalK += c2k(c);
totalF += c2f(c);
}
double avgC = totalC / count;
double avgK = totalK / count;
double avgF = totalF / count;
for (int i =0; i < count; i++){
if (temps[i].getC()> avgC){
aboveAverageCount++;
}
}
System.out.println("Temperature data from the file: "+ fileName);
System.out.printf("Average in Celsius: %.2f%n", avgC);
System.out.printf("Average in Kelvin: %.2f%n", avgK);
System.out.printf("Average in Fahrenheit: %.2f%n", avgF);
System.out.println("Number of temperatures above average: "+ aboveAverageCount);
}
}

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!