Java - Intro 3. User Input
A3.4 Volume and Surface Area of a Rectangular Prism
A3.5 Adding and Multiplying User Input
3. User Input
3.1 Overview
- This section takes some practice and you have to go on faith. You won’t really understand it all, just learn how to put the pieces in place.
- “hard coding” vs user input: Hard coding is what we have been doing when we put values (numbers and Strings) right in the program. Now we are going to take things in from the user.
3.2 The Big Picture
- There are four key pieces that need to be in the program to take in user input. They are highlighted in purple in the sample program below.
- The “userInput” variable (or any identifier name that you want), highlighted in red below, can be used as many times as needed as long as you don’t close it using the last purple highlighted line.
- We will work with the following, which can be put in place of the highlighted blue portion below:
- .nextInt() - Takes the input as an integer
- .next() - Takes the input as a String
- .nextDouble() - Takes the input as a double
- .nextBoolean() - Takes the input as a boolean
Sample Program:
import java.util.Scanner;
public class HelloWorld
{
public static void main(String[] arg)
{
int age;
// In the line below, “userInput” can be anything you want
Scanner userInput = new Scanner(System.in);
System.out.print("How old are you?");
age = userInput.nextInt();
System.out.println("You are a " + age + " year old!!");
userInput.close();
}
}
SAMPLE RUN:
How old are you? 35
You are a 35 year old!!
Chapter 3: Assignments
A3.1 Gather Basic Information
Create a program that asks a user for their first name, last name and age. Each of these values should be assigned to a different variable.
PROGRAM STRUCTURE:
import java.util.Scanner;
public class Prog33
{
public static void main(String[ ] arg)
{
// Declare a String called first but don’t initialize it
// Declare a String called last but don’t initialize it
// Declare an int called age but don’t initialize it
Scanner info = new Scanner(System.in);
// Create a print statement asking for a first name
first = info.next();
// Create a print statement asking for a last name
last = info.next();
// Create a print statement asking for the age
age = info.nextInt();
// Print the desired output
info.close();
}
}
SAMPLE RUN:
What is your first name?
Peter
What is your last name?
Bigelow
How old are you?
32
Peter Bigelow is 32 years old!
A3.2 Adding Machine
Take the user’s name and three doubles. Add together the numbers and print out the sum.
PROGRAM STRUCTURE:
import java.util.Scanner;
public class Prog34
{
public static void main(String[ ] arg)
{
// Declare a String and three doubles
// Create the Scanner with the input variable
// Ask the user for their name and three numbers and assign the input to
the proper variables.
// Print the desired output
// Close the Scanner
}
}
SAMPLE RUN:
What is your name?
Betty
Please give me your first number:
7.2
Please give me your second number:
1.3
Please give me your third number:
10.3
Betty, the sum of your numbers is: 18.8
A3.3 Short Mad-Lib
Ask the user for at least two nouns, two verbs, two adjectives and two adverbs. They should be stored with the following variable names: noun1, noun2, verb1, verb2, adj1, adj2, adv1, adv2. Then have the program print a story to the screen based on these words.
PROGRAM STRUCTURE:
import java.util.Scanner;
public class Prog35
{
public static void main(String[ ] arg)
{
// Declare all the String variables
// Create the Scanner with the input variable
// Ask the user for all of the words
// Print the desired output
// Close the Scanner
}
}
SAMPLE RUN:
Please give me a noun: house
Another noun: key
Please give me a verb: stick
Another verb (past tense): kicked
Please give me an adjective: fantastic
Another adjective: deathly
Please give me an adverb: quickly
Another adverb: smoothly
Here is the story that you generated:
Once there was a fantastic house. It liked to stick quickly. Yesterday, the house
smoothly kicked a deathly key.
A3.4 Volume and Surface Area of a Rectangular Prism
Ask the user for three dimensions of a rectangular prism, the height, width and length as well as the unit of the measurements. The volume and the surface area should each be calculated. The program should run like this:
PROGRAM STRUCTURE:
import java.util.Scanner;
public class Prog36
{
public static void main(String[ ] arg)
{
// Declare all variables with appropriate names
// Create the Scanner with the input variable
// Ask the user for values for all variables
// Print the desired output
// Close the Scanner
}
}
SAMPLE RUN:
What is the height of the prism?
5
What is the length of the prism?
7
What is the width of the prism?
2
What units are the length measured in?
feet
The volume of the prism is 70 feet cubed and the surface area is 118 feet squared.
A3.5 Adding and Multiplying User Input
Practice collecting more information from the user, processing it, and returning information.
PROGRAM STRUCTURE:
import java.util.Scanner;
public class Prog37
{
public static void main(String[ ] arg)
{
// See the program structure from 3.5
}
}
SAMPLE RUN:
Hello! What is your name?
Jen
Hi Jen. Please give me a number:
14
Thank you. Please give me another number:
10
OK.
If I add those numbers together, I get 24.
If I multiply those numbers together, I get 140.