Difference between revisions of "Personal Greeting"

From CompSciWiki
Jump to: navigation, search
 
(33 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/>
Make sure to use JOptionPane for your input and output
+
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: User inputs "Elmo" and "10" would result in something like "Welcome to COMP 1010 Elmo, you are 10 years old today."
+
<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."
 +
 
 +
 
 +
|SideSectionTitle=Mid-term Review
 +
 
 +
|SideSection=
 +
[[Image:Wiki_trays01.jpg|center]]<BR>
 +
 
 +
|Solution=Start by importing the swing java package. We need this to make calls to the JOptionPane class.
 +
 
 +
{{CodeBlock
 +
|Code=
 +
import javax.swing.*;
 +
import java.util.Scanner;
 +
}}
 +
 
 +
 
 +
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.
 +
{{CodeBlock
 +
|Code=
 +
String name; // storage for the name
 +
int age; // storage for the age
 +
Scanner input = new Scanner(System.in);
 +
}}
 +
 
 +
 
 +
{{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.
 +
 
 +
{{CodeBlock
 +
|Code=
 +
System.out.print("Please enter your name: "); 
 +
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
 +
}}
 +
 
 +
 
 +
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.
 +
}}
  
  
 
|SolutionCode=
 
|SolutionCode=
<pre>
 
 
import javax.swing.*;
 
import javax.swing.*;
  
Line 22: Line 76:
 
public static void main(String[] args)
 
public static void main(String[] args)
 
{
 
{
 +
//local variables
 
String name;
 
String name;
 
int age;
 
int age;
 +
 +
//input
 
name = JOptionPane.showInputDialog("Please enter your name") ;
 
name = JOptionPane.showInputDialog("Please enter your name") ;
 
age = Integer.parseInt(JOptionPane.showInputDialog("Please enter your age"));
 
age = Integer.parseInt(JOptionPane.showInputDialog("Please enter your age"));
  
 
+
//output
 
JOptionPane.showMessageDialog(null, "Welcome to COMP1010 "  + name + ", you are " + age + " years old today.");
 
JOptionPane.showMessageDialog(null, "Welcome to COMP1010 "  + name + ", you are " + age + " years old today.");
 
}
 
}
  
 
}
 
}
</pre>
 
 
 
|SideSectionTitle=Personal Greeting
 
 
|SideSection=
 
[[Image:Wiki_trays01.jpg|center]]
 
<BR>
 
Taken from http://www.flickr.com/photos/daniello/565304023/
 
 
An image or By Students section
 
 
|Solution=Start by importing the swing java package. <br/>
 
 
<pre> import javax.swing.*;</pre><br/>
 
 
Define your variables, we will need a String for your name and an int for age.
 
<pre>
 
String name;
 
int age;
 
</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 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 JOptionPage.showMessageDialog
+
}}
<pre>
+
JOptionPane.showMessageDialog(null, "Welcome to COMP1010 "  + name + ", you are " + age + " years old today.");
+
</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