Difference between revisions of "Temperature Calculator"
Line 1: | Line 1: | ||
+ | {{1010PrAD | ||
+ | |||
+ | |ProblemName=Personal Greeting | ||
+ | |||
+ | |Problem=Write a Java program PersonalGreeting, that asks a series of questions and returns a response.<br/>For all input and output please use [[Input/Output_using_JOptionPane|JOptionPane]] to help review the first chapter's material.<br/><br/>Your program should do the following: | ||
+ | *prompt the user for their name ([[Strings|String]]) | ||
+ | *prompt the user for their age ([[Common_Primitive_Variables#Primitive_Type_int|int]]) | ||
+ | *output a greeting message that displays both name and age gathered from the input | ||
+ | <br/> | ||
+ | <br/> | ||
+ | Example: User inputs "Elmo" for their name and "10" for age would result in something like "Welcome to COMP 1010 Elmo, you are 10 years old today." | ||
+ | |||
+ | |||
+ | |SolutionCode= | ||
+ | <pre> | ||
+ | import javax.swing.*; | ||
+ | |||
+ | public class PersonalGreeting | ||
+ | { | ||
+ | |||
+ | public static void main(String[] args) | ||
+ | { | ||
+ | String name; | ||
+ | int age; | ||
+ | name = JOptionPane.showInputDialog("Please enter your name") ; | ||
+ | age = Integer.parseInt(JOptionPane.showInputDialog("Please enter your age")); | ||
+ | |||
+ | JOptionPane.showMessageDialog(null, "Welcome to COMP1010 " + name + ", you are " + age + " years old today."); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | |||
+ | |SideSectionTitle=Personal Greeting<br/>This is more pleasent looking in person | ||
+ | |||
+ | |SideSection= | ||
+ | [[Image:Wiki_trays01.jpg|center]] | ||
+ | <BR> | ||
+ | |||
+ | |Solution=Start by importing the swing java package. This is in order to call the JOptionPane class.<br/> | ||
+ | |||
+ | <pre> import javax.swing.*;</pre><br/> | ||
+ | |||
+ | Define your variables, we will need a [[Strings|String]] for your name and an [[Common_Primitive_Variables#Primitive_Type_int|int]] for age. | ||
+ | <pre> | ||
+ | String name; | ||
+ | int age; | ||
+ | </pre><br/> | ||
+ | |||
+ | Next start by capturing the user input using [[JOptionPane_Methods#showInputDialog.28.29|JOptionPane.showInputDialog]]. We will need to use Integer.parseInt to cast the string result to an integer for your age. | ||
+ | |||
+ | <pre> | ||
+ | name = JOptionPane.showInputDialog("Please enter your name") ; | ||
+ | age = Integer.parseInt(JOptionPane.showInputDialog("Please enter your age")); | ||
+ | </pre><br/> | ||
+ | |||
+ | Now output your message using [[JOptionPane_Methods#showMessageDialog.28.29|JOptionPage.showMessageDialog]]. Make sure first parameter is null. Don't forget to use the + operator when appending strings also sometimes referred to as [[Strings#String_Expressions|concatenation]]. | ||
+ | <pre> | ||
+ | JOptionPane.showMessageDialog(null, "Welcome to COMP1010 " + name + ", you are " + age + " years old today."); | ||
+ | </pre> | ||
+ | }} | ||
+ | |||
+ | |||
{{1010PrAD | {{1010PrAD | ||
Revision as of 01:11, 7 April 2010
Back to the Program-A-Day homepage
ProblemWrite a Java program PersonalGreeting, that asks a series of questions and returns a response.
|
Personal Greeting
| |
---|---|---|
SolutionStart by importing the swing java package. This is in order to call the JOptionPane class. import javax.swing.*; Define your variables, we will need a String for your name and an int for age. String name; int age; Next start by capturing the user input using JOptionPane.showInputDialog. We will need to use Integer.parseInt to cast the string result to an integer for your age. name = JOptionPane.showInputDialog("Please enter your name") ; age = Integer.parseInt(JOptionPane.showInputDialog("Please enter your age")); Now output your message using JOptionPage.showMessageDialog. Make sure first parameter is null. Don't forget to use the + operator when appending strings also sometimes referred to as concatenation. JOptionPane.showMessageDialog(null, "Welcome to COMP1010 " + name + ", you are " + age + " years old today."); | ||
Code |
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 |
Temperature Calculator
| |
---|---|---|
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; 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")); 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 unit.charAt(int index) 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. if (unit.charAt(0) == 'c') { result = 9 * temperature / 5 + 32; System.out.println(temperature + " degree Celcius = " + result + " degree Fahrenheit"); } else if (unit.charAt(0) == 'f') { result = (temperature - 32) * 5 / 9; System.out.println(temperature + " degree Fahrenheit = " + result + " degree Celcius"); } else System.out.printline("You entered an incorrect unit, please try again"); | ||
Code |