Skip to main content
It’s now time to pick up the pace a bit. I’ve got you doing a lot of printing so that you get used to typing simple things, but those simple things are fairly boring. What we want to do now is get you getting data into your programs. This though is a little tricky so we have to have you learn to do two things that may not make sense right away, but if you stick with it they should click suddenly a few exercises from now.Most of what software does is the following:
  1. Take some kind of input from a person.
  2. Change it.
  3. Print out something to show how it changed.
So far you’ve only been printing, but you haven’t been able to get any input from a person, or change it. You may not even know what “input” means, so rather than talk about it, let’s have you do some and see if you get it. Next exercise we’ll do more to explain it.
AskingQuestions.java
import java.util.Scanner;

public class AskingQuestions
{
    public static void main( String[] args )
    {
        Scanner keyboard = new Scanner(System.in);

        int age;
        String height;
        double weight;

        System.out.print( "How old are you? " );
        age = keyboard.nextInt();

        System.out.print( "How tall are you? " );
        height = keyboard.next();

        System.out.print( "How much do you weigh? " );
        weight = keyboard.nextDouble();

        System.out.println( "So you're " + age + " old, " + height + " tall and " + weight + " heavy." );
    }
}
Notice that we used print instead of println. This is so that the program doesn’t end the line with a newline and go to the next line.
Ask the user for two words and two numbers, and let the person at the keyboard type in some values, but don’t bother storing their responses into any variables.Again, there is no need to create any variables, except for the Scanner variable typically named keyboard.
Give me a word!
lorry!
Give me a second word!
deoxyribonucleic?

Great, now your favorite number?
42
And your second-favorite number...
1

Whew!  Wasn't that fun?
Ask the user for their name. Then display their name to prove that you can recall it. Ask them for their age. Then display that. Finally, ask them for how much they make and display that. You should use the most appropriate data type for each variable.
Hello.  What is your name?
Dennis 

Hi, Dennis!  How old are you?
37 

So you're 37, eh?  That's not old at all!
How much do you make, Dennis?
8.50 

8.5!  I hope that's per hour and not per year! LOL!
Hello.  What is your name?
Catsup 

Hi, Catsup!  How old are you?
12 

So you're 12, eh?  That's not old at all!
How much do you make, Catsup?
99.9 

99.9!  I hope that's per hour and not per year! LOL!
Ask the user for several pieces of information, and display them on the screen afterward as a summary.
  • first name
  • last name
  • grade (classification)
  • student id number
  • login name
  • GPA (0.0 to 4.0) You must use the most appropriate type for each variable and not just Strings for everything.
Please enter the following information so I can sell it for a profit!

First name: Helena
Last name: Bonham-Carter
Grade (9-12): 12
Student ID: 453916
Login: bonham_453916
GPA (0.0-4.0): 3.73

Your information:
    Login: bonham_453916
    ID:    453916
    Name:  Bonham-Carter, Helena
    GPA:   3.73
    Grade: 12
Ask the user for their name. Then display their name to prove that you can recall it. Ask them for their age. Then display what their age would be five years from now. Then display what their age would be five years ago.
Hello.  What is your name? Percy_Bysshe_Shelley

Hi, Percy_Bysshe_Shelley!  How old are you? 34

Did you know that in five years you will be 39 years old?
And five years ago you were 29! Imagine that!
Hello.  What is your name? Gramps

Hi, Gramps!  How old are you? 87

Did you know that in five years you will be 92 years old?
And five years ago you were 82! Imagine that!
Make a simple numeric calculator. It should prompt the user for three numbers. Then add the numbers together and divide by 2. Display the result. Your program must support numbers with decimals and not just integers.
U:\>java DumbCalculator

What is your first number? 1.1
What is your second number? 2.2
What is your third number? 5.5

( 1.1 + 2.2 + 5.5 ) / 2 is... 4.4
(This assignment was suggested by Joel H in 2012.)The body mass index (BMI) is commonly used by health and nutrition professionals to estimate human body fat in populations.It is computed by taking the individual’s weight (mass) in kilograms and dividing it by the square of their height in meters.
Your height in m: 1.75
Your weight in kg: 73

Your BMI is 23.83673