Difference between revisions of "Personal Greeting"

From CompSciWiki
Jump to: navigation, search
 
(36 intermediate revisions by 4 users not shown)
Line 3: Line 3:
 
|ProblemName=Personal Greeting
 
|ProblemName=Personal Greeting
  
|Problem=Write a Java program PersonalGreeting, that asks a series of questions and returns a response.<br/>The program should:
+
|Problem=Write a Java program PersonalGreeting, that asks a series of questions and returns a response.<br/>For all input and output 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
 
*prompt the user for their name
 
*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 message that displays both name and age gathered from the input
 
<br/>
 
<br/>
Example: User inputs "Elmo" and "10" would result in something like "Welcome to COMP 1010 Elmo, you are 10 years old today."
+
This program will cover the following topics:
 +
*[[Input using Scanner|Scanner]]
 +
*[[Comments|Commnets]]
 +
*[[Strings|String]]
 +
*[[Common_Primitive_Variables#Primitive_Type_int|introduction to ints]]
 +
<br/>
 +
<br/>
 +
Example: If a user inputs "Elmo" for their name and "10" for their age, then the result would look something like "Welcome to COMP 1010 Elmo, you are 10 years old today."
  
  
|SolutionCode=
+
|SideSectionTitle=Mid-term Review
<pre>
+
import javax.swing.*;
+
  
public class  PersonalGreeting
+
|SideSection=
{
+
[[Image:Wiki_trays01.jpg|center]]<BR>
  
public static void main(String[] args)
+
|Solution=Start by importing the swing java package. We need this to make calls to the JOptionPane class.
{
+
String name;
+
int age;
+
name = JOptionPane.showInputDialog("Please enter your name") ;
+
age = Integer.parseInt(JOptionPane.showInputDialog("Please enter your age"));
+
  
 +
{{CodeBlock
 +
|Code=
 +
import javax.swing.*;
 +
import java.util.Scanner;
 +
}}
  
System.out.println("Welcome to COMP1010 "  + name + ", you are " + age + " years old today.");
 
}
 
  
}
+
Define your variables, we will need a [[Strings|String]] for your name and an [[Common_Primitive_Variables#Primitive_Type_int|int]] for age and [[Input using Scanner|Scanner]] for input.
</pre>
+
{{CodeBlock
 +
|Code=
 +
String name; // storage for the name
 +
int age; // storage for the age
 +
Scanner input = new Scanner(System.in);
 +
}}
  
  
|SideSectionTitle=...by students
+
{{note}} To avoid confusion, we will assume users will enter valid input. An unfortunate disadvantage of .nextInt() is that if the user enters non numeric values the program will throw an error. You may wish to make the program more robust if you like. In a future computer science course you will learn how to handle such situations.
  
|SideSection=
+
{{CodeBlock
[[Image:Wiki_trays01.jpg|center]]
+
|Code=
<BR>
+
System.out.print("Please enter your name: "); 
Taken from http://www.flickr.com/photos/daniello/565304023/
+
name = input.next(); 
 +
System.out.print("Please enter your age: ");
 +
age = input.nextInt();
 +
}}
 +
{{OutputBlock
 +
|Code=
 +
Please enter your name: John
 +
Please enter your  age: 19
 +
}}
  
An image or By Students section
 
  
|Solution=Start by importing the swing java package. <br/>
+
Now output your message .
 +
{{CodeBlock
 +
|Code=
 +
System.out.println("Welcome to COMP1010 "  + name + ", you are " + age + " years old today.");
 +
}}
 +
{{OutputBlock
 +
|Code=
 +
Welcome to COMP1010 John, you are 19 years old today.
 +
}}
  
<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
+
|SolutionCode=
 +
import javax.swing.*;
  
<pre>
+
public class  PersonalGreeting
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.
+
public static void main(String[] args)
 +
{
 +
//local variables
 +
String name;
 +
int age;
  
<pre>
+
//input
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/>
+
  
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
+
//output
 +
JOptionPane.showMessageDialog(null, "Welcome to COMP1010 "  + name + ", you are " + age + " years old today.");
 +
}
  
<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>
 
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");
 
</pre>
 
  
 
}}
 
}}

Latest revision as of 15:15, 3 December 2011

Back to the Program-A-Day homepage

Problem

Write a Java program PersonalGreeting, that asks a series of questions and returns a response.
For all input and output use JOptionPane to help review the first chapter's material.

Your program should do the following:

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


This program will cover the following topics:



Example: If a user inputs "Elmo" for their name and "10" for their age, then the result would look something like "Welcome to COMP 1010 Elmo, you are 10 years old today."

 

Mid-term Review

Wiki trays01.jpg

Solution

Start by importing the swing java package. We need this to make calls to the JOptionPane class.

 import javax.swing.*;
import java.util.Scanner; 


Define your variables, we will need a String for your name and an int for age and Scanner for input.

 String name; // storage for the name
int age; // storage for the age
Scanner input = new Scanner(System.in); 


Note Note: To avoid confusion, we will assume users will enter valid input. An unfortunate disadvantage of .nextInt() is that if the user enters non numeric values the program will throw an error. You may wish to make the program more robust if you like. In a future computer science course you will learn how to handle such situations.

 System.out.print("Please enter your name: ");  
name = input.next();   
System.out.print("Please enter your age: ");
age = input.nextInt(); 
 Please enter your name: John
Please enter your  age: 19 


Now output your message .

 System.out.println("Welcome to COMP1010 "  + name + ", you are " + age + " years old today."); 
 Welcome to COMP1010 John, you are 19 years old today. 

Code

Solution Code

Back to the Program-A-Day homepage