Difference between revisions of "Temperature Calculator"

From CompSciWiki
Jump to: navigation, search
 
(18 intermediate revisions by 3 users not shown)
Line 3: Line 3:
 
|ProblemName=Temperature Calculator
 
|ProblemName=Temperature Calculator
  
|Problem=Write a Java program Temp, that converts celcius to fahrenheit or vice versa.<br/><br/>Your program should do the following:
+
|Problem=Write a Java program Temp, that converts celsius to fahrenheit or vice versa.<br><br>Your program should do the following:
*prompt the user for a 1-character string c (celcius) or f (fahrenheit) that tells the program which unit you are entering
+
*prompt the user for a 1-character string c (celsius) or f (fahrenheit) that tells the program which unit you are entering
 
*prompt the user for the temperature
 
*prompt the user for the temperature
*output the solution followed by a c or f to denote the final units
+
*output the solution followed by a 'c' or 'f' to denote the final units
<br/>
+
<br>
 +
This program will cover the following topics:
 +
*[[Common_Primitive_Variables|working with primitive data types]]
 +
*[[Arithmetic_Operators|integer arithmetic]]
 +
*[[Math_Methods|using the Math class]]
 +
*[[Common_Primitive_Variables#Primitive_Type_double|working with real numbers: the double data type]]
 +
*[[Casting|casting]]
 +
<br>
 
Example: User inputs "c" and "10" would result in "50f"
 
Example: User inputs "c" and "10" would result in "50f"
<br/>
+
<br>
 
+
Use the following formula to convert your units<br/>
+
 
+
F = 9C/5 + 32
+
  
 +
Use the following formula to convert your units:<br>
  
 +
<math>F = 9C/5 + 32</math>
  
  
 
|SolutionCode=
 
|SolutionCode=
<pre>
 
 
import javax.swing.*;
 
import javax.swing.*;
  
Line 29: Line 33:
 
double temperature, result;
 
double temperature, result;
 
String unit;
 
String unit;
unit = JOptionPane.showInputDialog("Enter the 1-character temperature you want to convert from c (Celcius) or f (Fahrenheit)") ;
+
unit = JOptionPane.showInputDialog("Enter the 1-character temperature you want to convert from c (Celsius) or f (Fahrenheit)") ;
 
temperature = Integer.parseInt(JOptionPane.showInputDialog("Enter the current temperature in the units you speficied"));
 
temperature = Integer.parseInt(JOptionPane.showInputDialog("Enter the current temperature in the units you speficied"));
  
Line 35: Line 39:
 
{
 
{
 
result = 9 * temperature / 5 + 32;
 
result = 9 * temperature / 5 + 32;
System.out.println(temperature + " degree Celcius = " + result + " degree Fahrenheit");
+
result = Math.round(result*100)/100.0;
 +
System.out.println(temperature + " degree Celsius = " + result + " degree Fahrenheit");
 
}
 
}
 
else if (unit.charAt(0) == 'f')
 
else if (unit.charAt(0) == 'f')
 
{
 
{
 
result = (temperature - 32) * 5 / 9;
 
result = (temperature - 32) * 5 / 9;
System.out.println(temperature + " degree Fahrenheit = " + result + " degree Celcius");
+
result = Math.round(result*100)/100.0;
 +
System.out.println(temperature + " degree Fahrenheit = " + result + " degree Celsius");
 
}
 
}
 
else
 
else
System.out.printline("You entered an incorrect unit, please try again");
+
System.out.println("You entered an incorrect unit, please try again");
  
 
}
 
}
 
 
}
 
}
</pre>
 
 
  
 
|SideSectionTitle=Temperature Calculator
 
|SideSectionTitle=Temperature Calculator
  
 
|SideSection=
 
|SideSection=
[[Image:Wiki_trays01.jpg|center]]
+
[[Image:Wiki_trays01.jpg|center]]<BR>
<BR>
+
  
  
|Solution=Start by importing the swing java package. <br/>
+
|Solution=Start by importing the swing java package. <br>
  
<pre> import javax.swing.*;</pre><br/>
+
{{CodeBlock
 +
|Code= import javax.swing.*;}}<br>
  
 
Define your variables, we will need doubles in case of decimal results, and a string value for the unit
 
Define your variables, we will need doubles in case of decimal results, and a string value for the unit
  
<pre>
+
{{CodeBlock
 +
|Code=
 
double temperature, result;
 
double temperature, result;
 
String unit;
 
String unit;
</pre><br/>
+
}}<br>
  
 
Next start by capturing the user input using JOptionPane. We will need to use Integer.parseInt to cast the String result to an integer for the temperature.  
 
Next start by capturing the user input using JOptionPane. We will need to use Integer.parseInt to cast the String result to an integer for the temperature.  
  
<pre>
+
{{CodeBlock
unit = JOptionPane.showInputDialog("Enter the 1-character temperature you want to convert from c (Celcius) or f (Fahrenheit)") ;
+
|Code=
temperature = Integer.parseInt(JOptionPane.showInputDialog("Enter the current temperature in the units you speficied"));
+
unit = JOptionPane.showInputDialog("Enter the 1-character temperature you want to convert from c (Celsius) or f (Fahrenheit)") ;
</pre><br/>
+
temperature = Integer.parseInt(JOptionPane.showInputDialog("Enter the current temperature in the units you specified"));
 +
}}<br>
  
Now we need to use conditional statements to check for each unit, one for Celcius and one for Fahrenheit. One thing to note is that because unit is a String datatype, we will have to only extract the first character assuming it has be entered in correctly, this is easily accomplished using  
+
Now we need to use conditional statements to check for each unit, one for Celsius and one for Fahrenheit. One thing to note is that because unit is a String data type, we will have to only extract the first character assuming it has be entered in correctly, this is easily accomplished using where index is the position at which the char character is at in the String. <br/><br/>
 +
{{note}} this is one approach to it, there are many others. This also assumes that users enter valid input, invalid input will result in an error. An alternate solution could be to use string comparisons like [[String_Methods#equals|.equals()]] which is defined in your course material.
  
<pre>unit.charAt(int index)</pre>
+
{{CodeBlock
 +
|Code=unit.charAt(int index)}}<br>
  
Where index is the position at which the char character is at in the String. For each case, calculate your results and print the output using System.out. To make your program more robust, you may want to use an else case for all invalid characters.
+
For each case, calculate your results and print the output using System.out. We rounded our answer to make use of the built in Math class. To round it to two decimal places or the nearest one-hundredth you have to multiply by 100 since Math.round returns an int and then divide by 100.0.<br/><br/>
 
+
{{note}}divide by 100.0 in order to keep the result a double, otherwise it will result in an integer.
<pre>
+
{{CodeBlock
 +
|Code=
 
if (unit.charAt(0) == 'c')
 
if (unit.charAt(0) == 'c')
 
{
 
{
 
result = 9 * temperature / 5 + 32;
 
result = 9 * temperature / 5 + 32;
System.out.println(temperature + " degree Celcius = " + result + " degree Fahrenheit");
+
result = Math.round(result*100)/100.0;
 +
System.out.println(temperature + " degree Celsius = " + result + " degree Fahrenheit");
 
}
 
}
 
else if (unit.charAt(0) == 'f')
 
else if (unit.charAt(0) == 'f')
 
{
 
{
 
result = (temperature - 32) * 5 / 9;
 
result = (temperature - 32) * 5 / 9;
System.out.println(temperature + " degree Fahrenheit = " + result + " degree Celcius");
+
result = Math.round(result*100)/100.0;
 +
System.out.println(temperature + " degree Fahrenheit = " + result + " degree Celsius");
 
}
 
}
 +
}}<br>
 +
 +
To make your program more robust, you may want to use an else case for all invalid characters.
 +
{{CodeBlock
 +
|Code=
 
else
 
else
System.out.printline("You entered an incorrect unit, please try again");
+
System.out.println("You entered an incorrect unit, please try again");
</pre>
+
}}
  
 
}}
 
}}

Latest revision as of 03:18, 6 December 2011

Back to the Program-A-Day homepage

Problem

Write a Java program Temp, that converts celsius to fahrenheit or vice versa.

Your program should do the following:

  • prompt the user for a 1-character string c (celsius) or f (fahrenheit) that tells the program which unit you are entering
  • prompt the user for the temperature
  • output the solution followed by a 'c' or 'f' to denote the final units


This program will cover the following topics:


Example: User inputs "c" and "10" would result in "50f"

Use the following formula to convert your units:

<math>F = 9C/5 + 32</math>

 

Temperature Calculator

Wiki trays01.jpg

Solution

Start by importing the swing java package.

 import javax.swing.*; 

Define your variables, we will need doubles in case of decimal results, and a string value for the unit

 double temperature, result;
String unit; 

Next start by capturing the user input using JOptionPane. We will need to use Integer.parseInt to cast the String result to an integer for the temperature.

 unit = JOptionPane.showInputDialog("Enter the 1-character temperature you want to convert from c (Celsius) or f (Fahrenheit)") ;
temperature = Integer.parseInt(JOptionPane.showInputDialog("Enter the current temperature in the units you specified")); 

Now we need to use conditional statements to check for each unit, one for Celsius and one for Fahrenheit. One thing to note is that because unit is a String data type, we will have to only extract the first character assuming it has be entered in correctly, this is easily accomplished using where index is the position at which the char character is at in the String.

Note Note: this is one approach to it, there are many others. This also assumes that users enter valid input, invalid input will result in an error. An alternate solution could be to use string comparisons like .equals() which is defined in your course material.

 unit.charAt(int index) 

For each case, calculate your results and print the output using System.out. We rounded our answer to make use of the built in Math class. To round it to two decimal places or the nearest one-hundredth you have to multiply by 100 since Math.round returns an int and then divide by 100.0.

Note Note: divide by 100.0 in order to keep the result a double, otherwise it will result in an integer.

 if (unit.charAt(0) == 'c')
{
	result = 9 * temperature / 5 + 32;
	result = Math.round(result*100)/100.0;
	System.out.println(temperature + " degree Celsius = " + result + " degree Fahrenheit");
}
else if (unit.charAt(0) == 'f')
{
	result = (temperature - 32) * 5 / 9;
	result = Math.round(result*100)/100.0;
	System.out.println(temperature + " degree Fahrenheit = " + result + " degree Celsius");
} 

To make your program more robust, you may want to use an else case for all invalid characters.

 else
	System.out.println("You entered an incorrect unit, please try again"); 

Code

Solution Code

Back to the Program-A-Day homepage