Correct the following issues with the below java code. Once corrected, highlight
ID: 3737571 • Letter: C
Question
Correct the following issues with the below java code. Once corrected, highlight where the code was corrected.
The java program converts temperatures to and from Kelvin/Farenheit/Celsius. The program asks for 3 inputs then outputs
The value of (input format) in (output format) is (output temp). For example an input of F,89,C yields the out put of
"The value of 89.0 F in celsius is 31.666666666666668 C"
1. When converting temp from K to F, C to F or C to K the output statement is follwed by another statement. Update the code so the statement after the "The value..." does not print.
For example: converting 89 celsius to F yields
"The value of 89.0 C in farenheit is 192.20000000000002 K
Enter the value in Farenheit:" <<< This line should not print. Correct code to not print when converting temp from K to F, C to F or C to K.
2. Round the output temp to the nearest 10th. ex. 192.20 K
import java.io.*;
import java.util.*;
class Kelvin{
private double data;
public Kelvin(double a){
data = a;
}
public double convertToCelsius(){
return (data - 273.15);
}
public double convertToFarenheit(){
return (data * (9.0/5.0) - 459.67);
}
}
class Celsius{
private double data;
public Celsius(double a){
data = a;
}
public double convertToKelvin(){
return (data + 273.15);
}
public double convertToFarenheit(){
return (data * (9.0/5.0) + 32);
}
}
class Farenheit{
private double data;
public Farenheit(double a){
data = a;
}
public double convertToCelsius(){
return (data-32) * (5.0/9.0);
}
public double convertToKelvin(){
return (data + 459.67) * (5.0/9.0);
}
}
public class TemperatureControl{
public static void print(String str){
try {
FileOutputStream fout = new FileOutputStream("output.txt");
PrintWriter pw = new PrintWriter(fout);
pw.println(str);
pw.close();
fout.close();
}
catch (FileNotFoundException e){
System.out.println("File Not Found");
}
catch (IOException e){
System.out.println("File Not Found");
}
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the input format of temperature (C-Celsius, F-Farenheit, K-Kelvin):");
String ch = sc.next();
if (ch.charAt(0) == 'C'){
System.out.println("Enter the value in Celsius:");
double data = Double.parseDouble(sc.next());
Celsius c = new Celsius(data);
System.out.println("Enter the output format of temperature (C-Celsius, F-Farenheit, K-Kelvin):");
ch = sc.next();
if (ch.charAt(0) == 'K'){
String str = "The value of " + Double.toString(data) + " C in kelvin is " + Double.toString(c.convertToKelvin()) + " K";
System.out.println(str);
print(str);
}
if (ch.charAt(0) == 'F'){
String str = "The value of " + Double.toString(data) + " C in farenheit is " + Double.toString(c.convertToFarenheit()) + " K";
System.out.println(str);
print(str);
}
}
if (ch.charAt(0) == 'K'){
System.out.println("Enter the value in Kelvin:");
double data = Double.parseDouble(sc.next());
Kelvin k = new Kelvin(data);
System.out.println("Enter the output format of temperature (C-Celsius, F-Farenheit, K-Kelvin):");
ch = sc.next();
if (ch.charAt(0) == 'C'){
String str = "The value of " + Double.toString(data) + " K in celsius is " + Double.toString(k.convertToCelsius()) + " C";
System.out.println(str);
print(str);
}
if (ch.charAt(0) == 'F'){
String str = "The value of " + Double.toString(data) + " K in farenheit is " + Double.toString(k.convertToFarenheit()) + " F";
System.out.println(str);
print(str);
}
}
if (ch.charAt(0) == 'F'){
System.out.println("Enter the value in Farenheit:");
double data = Double.parseDouble(sc.next());
Farenheit f = new Farenheit(data);
System.out.println("Enter the output format of temperature (C-Celsius, F-Farenheit, K-Kelvin):");
ch = sc.next();
if (ch.charAt(0) == 'K'){
String str = "The value of " + Double.toString(data) + " F in kelvin is " + Double.toString(f.convertToKelvin()) + " K";
System.out.println(str);
print(str);
}
if (ch.charAt(0) == 'C'){
String str = "The value of " + Double.toString(data) + " F in celsius is " + Double.toString(f.convertToCelsius()) + " C";
System.out.println(str);
print(str);
}
}
}
}
Explanation / Answer
import java.io.*; import java.text.DecimalFormat; import java.util.*; class Kelvin{ private double data; public Kelvin(double a){ data = a; } public double convertToCelsius(){ return (data - 273.15); } public double convertToFarenheit(){ return (data * (9.0/5.0) - 459.67); } } class Celsius{ private double data; public Celsius(double a){ data = a; } public double convertToKelvin(){ return (data + 273.15); } public double convertToFarenheit(){ return (data * (9.0/5.0) + 32); } } class Farenheit{ private double data; public Farenheit(double a){ data = a; } public double convertToCelsius(){ return (data-32) * (5.0/9.0); } public double convertToKelvin(){ return (data + 459.67) * (5.0/9.0); } } public class TemperatureControl{ public static void print(String str){ try { FileOutputStream fout = new FileOutputStream("output.txt"); PrintWriter pw = new PrintWriter(fout); pw.println(str); pw.close(); fout.close(); } catch (FileNotFoundException e){ System.out.println("File Not Found"); } catch (IOException e){ System.out.println("File Not Found"); } } public static void main(String[] args){ DecimalFormat df = new DecimalFormat("##.##"); Scanner sc = new Scanner(System.in); System.out.println("Enter the input format of temperature (C-Celsius, F-Farenheit, K-Kelvin):"); String ch = sc.next(); if (ch.charAt(0) == 'C'){ System.out.println("Enter the value in Celsius:"); double data = Double.parseDouble(sc.next()); Celsius c = new Celsius(data); System.out.println("Enter the output format of temperature (C-Celsius, F-Farenheit, K-Kelvin):"); ch = sc.next(); if (ch.charAt(0) == 'K'){ String str = "The value of " + Double.toString(data) + " C in kelvin is " + df.format(c.convertToKelvin()) + " K"; System.out.println(str); print(str); } if (ch.charAt(0) == 'F'){ String str = "The value of " + Double.toString(data) + " C in farenheit is " + df.format(c.convertToFarenheit()) + " K"; System.out.println(str); print(str); } } else if (ch.charAt(0) == 'K'){ System.out.println("Enter the value in Kelvin:"); double data = Double.parseDouble(sc.next()); Kelvin k = new Kelvin(data); System.out.println("Enter the output format of temperature (C-Celsius, F-Farenheit, K-Kelvin):"); ch = sc.next(); if (ch.charAt(0) == 'C'){ String str = "The value of " + Double.toString(data) + " K in celsius is " + df.format(k.convertToCelsius()) + " C"; System.out.println(str); print(str); } if (ch.charAt(0) == 'F'){ String str = "The value of " + Double.toString(data) + " K in farenheit is " + df.format(k.convertToFarenheit()) + " F"; System.out.println(str); print(str); } } else if (ch.charAt(0) == 'F'){ System.out.println("Enter the value in Farenheit:"); double data = Double.parseDouble(sc.next()); Farenheit f = new Farenheit(data); System.out.println("Enter the output format of temperature (C-Celsius, F-Farenheit, K-Kelvin):"); ch = sc.next(); if (ch.charAt(0) == 'K'){ String str = "The value of " + Double.toString(data) + " F in kelvin is " + df.format(f.convertToKelvin()) + " K"; System.out.println(str); print(str); } if (ch.charAt(0) == 'C'){ String str = "The value of " + Double.toString(data) + " F in celsius is " + df.format(f.convertToCelsius()) + " C"; System.out.println(str); print(str); } } } }