Difference between revisions of "Temperature Calculator"

From CompSciWiki
Jump to: navigation, search
Line 5: Line 5:
 
|Problem=Write a Java program Temp, that converts celcius to fahrenheit or vice versa.<br/>The program should:
 
|Problem=Write a Java program Temp, that converts celcius to fahrenheit or vice versa.<br/>The program should:
 
*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 (celcius) or f (fahrenheit) that tells the program which unit you are entering
*prompt the user for the temperature (2-digits max)
+
*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/>
Line 29: Line 29:
 
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 (Celcius) or f (Fahrenheit)") ;
temperature = Integer.parseInt(JOptionPane.showInputDialog("Enter the current temperature in the units you speficied (2-digits max)"));
+
temperature = Integer.parseInt(JOptionPane.showInputDialog("Enter the current temperature in the units you speficied"));
  
 
if (unit.charAt(0) == 'c')
 
if (unit.charAt(0) == 'c')
Line 58: Line 58:
 
An image or By Students section
 
An image or By Students section
  
|Solution=Start by importing the swing java package. <br/><pre> import javax.swing.*;</pre><br/><br/>Define your variables<pre>double temperature, result;
+
|Solution=Start by importing the swing java package. <br/>
String unit;</pre>
+
 
 +
<pre> import javax.swing.*;</pre>
 +
<br/><br/>
 +
 
 +
Define your variables, we will need doubles in case of decimal results, and a string value for the unit
 +
 
 +
<pre>
 +
double temperature, result;
 +
String unit;
 +
</pre>
 +
 
 +
<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.
 +
 
 +
<pre>
 +
unit = JOptionPane.showInputDialog("Enter the 1-character temperature you want to convert from c (Celcius) or f (Fahrenheit)") ;
 +
temperature = Integer.parseInt(JOptionPane.showInputDialog("Enter the current temperature in the units you speficied"));
 +
</pre>
  
 
}}
 
}}

Revision as of 12:07, 6 April 2010

Back to the Program-A-Day homepage

Problem

Write a Java program Temp, that converts celcius to fahrenheit or vice versa.
The program should:

  • 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 the temperature
  • output the solution followed by a c or f to denote the final units


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

Use the following formula to convert your units

F = 9C/5 + 32

 

...by students

float
Taken from http://www.flickr.com/photos/daniello/565304023/

An image or By Students section

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 (Celcius) or f (Fahrenheit)") ;
temperature = Integer.parseInt(JOptionPane.showInputDialog("Enter the current temperature in the units you speficied"));

Code

Solution Code

Back to the Program-A-Day homepage