Midterm: Temp Converter (50 points) 2016 This assignment is a simple temperature
ID: 3579343 • Letter: M
Question
Midterm: Temp Converter (50 points) 2016
This assignment is a simple temperature converter, which can convert Fahrenheit to Celsius or Celsius to Fahrenheit. An added option is to display the degrees Kelvin (absolute zero scale). Please note the ‘required elements’ discussed below in order to have a complete and correct program. This will be done as a command line program, as shown in the sample run.
Welcome to the Temperature Converter!
Would you also like to see degrees Kelvin (K) in the results? (Y/N): Y
Select conversion type (1=F to C, 2=C to F, 3=Stop/Start K, 0=end): 1
Enter your Fahrenheit temperature: 32
A temp of 32 Fahrenheit converted to Celsius = 0 C.
This is also a temperature of: 273.15 K
Select conversion type (1=F to C, 2=C to F, 3=Stop/Start K, 0=end): 2
Enter your Celsius temperature: 100
A temp of 100 Celsius converted to Fahrenheit = 212 F.
This is also a temperature of: 373.15 K
Select conversion type (1=F to C, 2=C to F, 3=Stop/Start K, 0=end): 2
Enter your Celsius temperature: -300
A temp of 100 Celsius converted to Fahrenheit = -508 F.
This temperature is not possible, as it is below 0 K
Select conversion type (1=F to C, 2=C to F, 3=Stop/Start K, 0=end): 3
Would you also like to see degrees Kelvin (K) in the results? (Y/N): N
Select conversion type (1=F to C, 2=C to F, 3=Stop/Start K, 0=end): 1
Enter your Fahrenheit temperature: -40
A temp of 32 Fahrenheit converted to Celsius = -40 C.
Select conversion type (1=F to C, 2=C to F, 3=Stop/Start K, 0=end): 0
Thanks for using the temperature converter!
(the bold and italics above is for emphasis only, you do not have to print your results in a different typeface).
The Formulas for the conversions are as follows:
Co = 5/9(Fo - 32)
Fo = (9/5 Co) + 32
K = Co + 273.15
Thus, the program sample of converting 32 degrees F to C was: (5/9)*(32-32) = 0.
Likewise, the conversion of 100 degrees C to F was: (9 / 5 * 100) + 32 = 212.
Note that at the beginning of the program it asks if degrees K are to be included in the output, but this setting can be changed by selecting option 3 during a session.
Pseudo-code has been supplied for the exercise, and you should follow the program design/structure as shown in the Pseudo-code. Other items that must be present for full credit are:
1)Minimize the code in main, but in that method include the primary loop which allows the user to process multiple conversions.
2)Develop a separate getCType() method to perform data validation on the integer input value which specifies the conversion method (F, C, Start/Stop K, or quit). In this method use a try/catch structure to avoid the program crashing on illegal values (such as string input when a number is requested).
3)Develop separate methods for each of the conversion calculations. These conversion methods do not need to return values, but just calculate and display the proper result. They should be called FtoC(), CtoF() and showDegreesK()
a.Use data type double for the temperature values.
4)Develop a getTemp() method used by both FtoC() and CtoF() which operates like getCType() (discussed above in step 2); it should accept and error trap the temp input value and then return a legal value to the FtoC() or CtoF() calling method. (Validation in this step does not require recognizing a temperature below absolute zero).
5)To accomplish the Degrees K component you must store the original user decision (as to whether or not they want to see K), and, if needed, at the end of each conversion method, call a different method to produce the additional ‘degrees K’ output line. This is the method to be called showDegreesK(). Design note: you should not need two methods for the degrees K result – only a single showDegreesK() method is needed. (why?)
Also, inside the showDegreesK() method write a validation routine which recognizes an illegal temperature and prints an appropriate message. In other words, since the kelvin scale is based on absolute zero, a negative result for degrees K is actually illegal, and a message should be shown instead of the calculated value (see example above).
Call your program TempConverter and zip the full project folder and submit it in the usual way (there will be a ‘midterm’ link in the assignments area).
Explanation / Answer
import java.io.*;
import java.util.*;
import java.lang.*;
class TempConverter
{
public static void main(String args[])
{
char c1;
int type,flag;
double c,f,k;
Scanner s=new Scanner(System.in);
public void GetCType()
{
System.out.println(" Would you like to see degrees Kelvin (K) in the results?(Y/N):");
c1=s.nextChar();
if(c1=='y'||c1=='Y)
flag=1;
else if(c1=='n'||c1=='N')
flag=0;
do
{
System.out.println(" Select conversion type (1=F to C, 2=C to F, 3=Stop/Start K, 0=end):");
type=s.nextInt();
if(type==1)
FtoC();
else if(type==2)
CtoF();
else if(type==3)
showDegreesK();
}while(type!=0);
System.out.println(" Thank you for using temperature converter");
}
public void FtoC()
{
getTemp();
c=5/9*(f-32);
System.out.println(" a temp of"+f+"Farenheit converted to celsius ="+c+"C");
if(flag==1)
{
k=c+273.15;
System.out.println(" this is also a temperature of:"+k);
}
}
public void CtoF()
{
getTemp();
f=(9/5*c)+32;
System.out.println(" a temp of"+c+"Celsius converted to farenheit ="+f+"F");
if(flag==1)
{
k=c+273.15;
System.out.println(" this is also a temperature of:"+k);
}
}
public void showDegreesK()
{
System.out.println(" Would you like to see degrees Kelvin (K) in the results?(Y/N):");
c1=s.next();
if(c1=='y'||c1=='Y)
flag=1;
else if(c1=='n'||c1=='N')
flag=0;
}
public void getTemp()
{
if(type==1)
{
System.out.println(" Enter your farenheit temperature:");
f=s.nextDouble();
}
if(type==2)
{
System.out.println(" Enter your celsius temperature:");
c=s.nextDouble();
}
}
}
}