Difference between revisions of "Temperature Calculator"
From CompSciWiki
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 | + | *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 | + | 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
ProblemWrite a Java program Temp, that converts celcius to fahrenheit or vice versa.
Use the following formula to convert your units F = 9C/5 + 32 |
...by students
An image or By Students section | |
---|---|---|
SolutionStart 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;
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 |