Output needs to be shown Program 4. File Input and Output 1.0 Using the temps.tx
ID: 3745073 • Letter: O
Question
Output needs to be shownProgram 4. File Input and Output 1.0 Using the temps.txt data file (Located on the Data Canvas Page) which has temperatures in degrees Celsius create a new data file (table.txt) that consists of a table of temperatures in Celsius, Fahrenheit and Kelvin. The equation to convert Celsius to Fahrenheit is: F-9.0/5.0 Celsius+32.0 To convert Celsius to Kelvin add 273.15 degrees. The table should have headings for each temperature as shown below. Celsius Fahrenheit 0 212 Kelvin 32.0 100 Upload the output file to Canvas 305.15 405.15
Explanation / Answer
Note: As you didnt mentioned in which language i have to develop code,i developed in java.Plz tell me if u want in c++ and also plz provide temps.txt file .Thank You
_______________
temps.txt
5.5
15.2
25.4
35.8
45.1
55.9
65.3
75.7
85.6
95.0
-999.99
______________
Lab7.java
//This program will read in from a file a series of numbers in degree Celsius
//until it reads sentinel values, convert them to degree Fahrenheit and
//print out a table of those values.
//The file input is handled using the command line's redirection mechanism.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Scanner;
public class Lab7 {
public static void main(String[] args) {
// DecimalFormat class Object is used to format number
DecimalFormat df = new DecimalFormat("#.##");
// Creating FileWriter reference
FileWriter fileWriter = null;
// Declaring constant
final double SENT = -999.99;
// Declaring variables
double degreeCelsius = 0;
double degreeFahrenheit,kelvin;
// Creating the Scanner class reference.
Scanner scanner = null;
try {
// Creating Scanner object by passing the file Object as input
scanner = new Scanner(new File("temps.txt"));
// Checking whether the number read from the file is double type or
// not
if (scanner.hasNextDouble()) {
/*
* if the read number is of double type then assign that value
* to degreeCelsius
*/
degreeCelsius = scanner.nextDouble();
} else {
/*
* if the read number is not of double type then program exit
*/
System.exit(0);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// Creating an output file
File output = new File("table.txt");
try {
// Creating FileWrite Object
fileWriter = new FileWriter(output);
// Writing the data to the file
fileWriter.write("Celsius Fahrenheit Kelvin ");
fileWriter.write(String.valueOf(degreeCelsius) + " ");
degreeFahrenheit = degreeCelsius * (9.0 / 5.0) + 32.0;
kelvin=degreeCelsius+271.15;
fileWriter.write(df.format(degreeFahrenheit) +" "+kelvin+" ");
/*
* This loop continue to execute until there is no value to be read
* from file
*/
while (scanner.hasNext()) {
degreeCelsius = scanner.nextDouble();
if (degreeCelsius != SENT) {
fileWriter.write(String.valueOf(degreeCelsius) + " ");
// Converting celsius to Fahrenheit
degreeFahrenheit = degreeCelsius * (9.0 / 5.0) + 32.0;
kelvin=degreeCelsius+271.15;
fileWriter.write(df.format(degreeFahrenheit) +" "+df.format(kelvin)+" ");
} else {
// Closing the file
fileWriter.close();
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
} // End of main method
} // End class
________________
Output file:
table.txt
Celsius Fahrenheit Kelvin
5.5 41.9 276.65
15.2 59.36 286.35
25.4 77.72 296.55
35.8 96.44 306.95
45.1 113.18 316.25
55.9 132.62 327.05
65.3 149.54 336.45
75.7 168.26 346.85
85.6 186.08 356.75
95.0 203 366.15
_____________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.