Problem 14

Asking Questions

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.

import java.util.Scanner;

public class AskingQuestions {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        
        System.out.print("How old are you? ");
        int age = input.nextInt();
        
        System.out.print("How tall are you? ");
        String height = input.next();
        
        System.out.print("How much do you weigh? ");
        double weight = input.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.

What You Should See

~/.../pbd-solutions/14$ java AskingQuestions
How old are you? 35
How tall are you? 6'2"
How much do you weigh? 180
So, you're 35 old, 6'2" tall and 180 heavy.
~/.../pbd-solutions/14$

What You Should Do on Your Own

Assignments turned in without these things will not receive any points.

  1. Change the program so that it reads in the height in two parts.
    • The first part should read in an int for the number of feet.
    • Then read in a second int for the number of inches.
    • Try to make the output look the same, though.
How old are you? 35
How many feet tall are you? 6
And how many inches? 2
How much do you weigh? 180
So, you're 35 old, 6'2" tall and 180 heavy.

◄ 13: Your Schedule 15: The Forgetful Machine ►



Adapted from ProgrammingByDoing.com
©2013 Graham Mitchell

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United States License.