Skip to content

Latest commit

 

History

History
139 lines (95 loc) · 5.8 KB

2015-03-05_input-if.md

File metadata and controls

139 lines (95 loc) · 5.8 KB

Pre-work

Watch the following video before class:

Input

Previously, we learned how to print. Remember the syntax?

System.out.println("I am printing a line!");

But just printing gets a bit boring. Today, we're going to learn how to ask the user for feedback. In programming, we call this reading user input. Let's talk about how.

Scanner

In Java, a simple program that reads user input looks like this:

import java.util.Scanner;

public class Echo {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Say something:");
        String something = input.next();
        System.out.println(something);
    }
}

First, we import the Scanner, meaning we tell the Java compiler to include some code written by another developer. Then to create a new Scanner, we type:

Scanner input = new Scanner(System.in);

Don't worry if you don't know exactly what this does. Think of this like driving: you don't need to know how a car engine works in order to drive a car. In programming, we call this abstraction. We don't care how the Scanner works, but we know what it does. What can you do with a Scanner?

Functions

The Scanner gives you the programmer some magical powers called functions. A function, like the gas and break pedals on a car, allows you to interact with the Scanner. For example, we can ask the Scanner to ask the user for an int like this:

int usersAge = input.nextInt();

This page has a full list of Scanner functions.

Let's go through the first exercise together.

Exercise: Asking Questions

Now try to use the Scanner yourself, but first...

Ground Rules for Exercises

  • Don't just copy and paste! Programming, like playing an instrument or speaking another language, requires muscle memory. It is important that you get used to typing Java for yourself. Also, you will notice things you would not otherwise if you are forced to type them.
  • At the top of every exercise, write a document comment. A document comment is description of the program for another user (or your future self). Get used to documenting your work! Here is an example document comment for the previous exercise:
    /**
     * Access Code 2.1
     * Gregory Gundersen
     * AskingQuestions.java
     * This class prompts the user for some personal data and then repeats it back to them.
     */
  • After every exercise, commit your work to GitHub!

Exercise: Forgetful Machine

Exercise: Name, Age, Salary

  • What happens if the user inputs the wrong type of data?
  • What happens if you don't prompt the user for input but still run the program?

Exercise: A Dumb Calculator

  • Printing the result here can be tricky!

Exercise: BMI Calculator

If-then statement

Ultimately, we want to write our own calculator! But first we need to respond to user input. In Java, we can do this with the if statement. The if statement is a control structure. If programming is driving, then control structures are steering wheels. They allow you to control how the program will execute. Let's look at an example:

if (7 > 3) {
    System.out.println("7 is greater than 3");
}

In the above code, we tell the program to print some text if the expression in parentheses is true. Remember the boolean type? Review the previous lecture for the operations that create booleans by comparing types.

Syntax

  • "if" is lowercase
  • "if" is followed by parentheses
  • The value inside the parentheses is a boolean but it can be created using the boolean operators.

Let's do a simple exercise together:

Exercise: What if

Now you try. Remember to type them yourself, leave comments, and commit your work!

Exercise: How Old Are You?

If-then-else

if can be followed by else, indicating that the block of code inside the else only execute if the original condition is false. Here is an example:

if (7 > 3) {
    System.out.println("7 is greater than 3");
} else {
    System.out.println("or we're in big trouble!");
}

Exercise: Else and If

Exercise: A little quiz

  • Come up with your own questions and answers
  • How can you keep track of the number of correct and incorrect answers?

In-class homework

Write a Reverse Polish notation calculator! Reverse Polish notation is a mathematical notation where the operator comes after the two operands, like this 2 3 +, which evaluates to 5. Your task is to write a class RPNCalculator that prompts the user for two numbers and a mathmetical operator and then returns the correct result. Here is an example:

"Please enter your first number"
3
"Please enter your second number"
7
"Please enter an operator"
-
"Result: -4"

A second example:

"Please enter your first number"
12
"Please enter your second number"
12
"Please enter an operator"
+
"Result: 24"

Your calculator only needs to handle 3 user inputs: 2 ints and 1 operator. You may test an RPN calculator here.