Difference between revisions of "Personal Greeting"
(14 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/>For all input and output | + | |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 message that displays both name and age gathered from the input | *output a greeting message that displays both name and age gathered from the input | ||
<br/> | <br/> | ||
+ | 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: | + | <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= | ||
− | |||
import javax.swing.*; | import javax.swing.*; | ||
Line 21: | 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."); | ||
} | } | ||
} | } | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
}} | }} |
Latest revision as of 15:15, 3 December 2011
Back to the Program-A-Day homepage
ProblemWrite a Java program PersonalGreeting, that asks a series of questions and returns a response.
|
Mid-term Review | |
---|---|---|
SolutionStart by importing the swing java package. We need this to make calls to the JOptionPane class. import javax.swing.*; import java.util.Scanner;
String name; // storage for the name int age; // storage for the age Scanner input = new Scanner(System.in);
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
System.out.println("Welcome to COMP1010 " + name + ", you are " + age + " years old today."); Welcome to COMP1010 John, you are 19 years old today. | ||
Code |