Difference between revisions of "Personal Greeting"

From CompSciWiki
Jump to: navigation, search
Line 7: Line 7:
 
*prompt the user for their age
 
*prompt the user for their age
 
*output a greeting including both the name and age gathered from input
 
*output a greeting including both the name and age gathered from input
 +
<br/>
 +
Make sure to use JOptionPane for your input and output
 
<br/>
 
<br/>
 
Example: User inputs "Elmo" and "10" would result in something like "Welcome to COMP 1010 Elmo, you are 10 years old today."
 
Example: User inputs "Elmo" and "10" would result in something like "Welcome to COMP 1010 Elmo, you are 10 years old today."
Line 26: Line 28:
  
  
System.out.println("Welcome to COMP1010 "  + name + ", you are " + age + " years old today.");
+
JOptionPane.showMessageDialog(null, "Welcome to COMP1010 "  + name + ", you are " + age + " years old today.");
 
}
 
}
  
Line 33: Line 35:
  
  
|SideSectionTitle=...by students
+
|SideSectionTitle=Personal Greeting
  
 
|SideSection=
 
|SideSection=
Line 46: Line 48:
 
<pre> import javax.swing.*;</pre><br/>
 
<pre> import javax.swing.*;</pre><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 a String for your name and an int for age.
 
+
 
<pre>
 
<pre>
double temperature, result;
+
String name;
String unit;
+
int age;
 
</pre><br/>
 
</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.  
+
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 your age.  
  
 
<pre>
 
<pre>
unit = JOptionPane.showInputDialog("Enter the 1-character temperature you want to convert from c (Celcius) or f (Fahrenheit)") ;
+
name = JOptionPane.showInputDialog("Please enter your name") ;
temperature = Integer.parseInt(JOptionPane.showInputDialog("Enter the current temperature in the units you speficied"));
+
age = Integer.parseInt(JOptionPane.showInputDialog("Please enter your age"));
 
</pre><br/>
 
</pre><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 output your message using JOptionPage.showMessageDialog
 
+
<pre>unit.charAt(int index)</pre>
+
 
+
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.
+
 
+
 
<pre>
 
<pre>
if (unit.charAt(0) == 'c')
+
JOptionPane.showMessageDialog(null, "Welcome to COMP1010 " + name + ", you are " + age + " years old today.");
{
+
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");
+
 
</pre>
 
</pre>
 
}}
 

Revision as of 12:40, 6 April 2010

{{1010PrAD

|ProblemName=Personal Greeting

|Problem=Write a Java program PersonalGreeting, that asks a series of questions and returns a response.
The program should:

  • prompt the user for their name
  • prompt the user for their age
  • output a greeting including both the name and age gathered from input


Make sure to use JOptionPane for your input and output
Example: User inputs "Elmo" and "10" would result in something like "Welcome to COMP 1010 Elmo, you are 10 years old today."


|SolutionCode=

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.");
	}

}


|SideSectionTitle=Personal Greeting

|SideSection= center;
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 a String for your name and an int for age.

String name;
int age;

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 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

JOptionPane.showMessageDialog(null, "Welcome to COMP1010 "  + name + ", you are " + age + " years old today.");