Download LECTURE 8

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Lecture 8
Instructor:
Craig Duckett
Assignments
Assignment 2 Due TONIGHT Lecture 8
Wednesday, April 19th
Assignment 1 Revision due Lecture 10
Wednesday, April 26th
Assignment 2 Revision Due Lecture 12
Wednesday, May 3rd
We'll Have a look at Assignment 3 at end of Lecture
Assignment Dates (By Due Date)
• Assignment 2 (LECTURE 8) DUE TONIGHT
Wednesday, April
19th
The Fickle
Finger of Fate
• Assignment 1 Revision (LECTURE 10)
Wednesday, April 276th
• Assignment 2 Revision (LECTURE 12)
•
•
•
•
Wednesday, May 3rd
Assignment 3 (LECTURE 13)
Monday, May 8th
Assignment 3 Revision (LECTURE 16)
Wednesday, May 17th
Assignment 4 (LECTURE 19)
Wednesday, May 31st
Assignment 4 Revision(LECTURE 20)
Monday, June 5th
3
MID-TERM
Mid-Term is LECTURE 9, is
next Monday, April 24th
• Please be prompt, and bring a pencil … don’t worry, I’ll supply the paper
Lecture 8 and Going Forward
LECTURE 8 ENDS THE FIRST PHASE OF THE QUARTER --WHAT THIS MEANS, AFTER THE MID-TERM:
• Less Theory, More Hands-On Work
(Less means Less, not No)
• Less Hand-Holding, More Trial-and-Error
• Less Explanation, More Research & Investigation, More
Poking Around For Code, More “Googling It” and More
(Occasionally) Aggravation
-----------------------------------------------------------------------
Becker – Chapters 9.4, 9.5: Input
• System.in
• The Scanner Class
But First…
The Quiz!
Input
Input
Output
Getting Input
 When creating and using computer programs, we can get input
in several different ways, either through a mouse, a keyboard, a
touch pad, a touch screen, cable (network, serial), wireless
mechanism, or a combination of several of these.
 For this class, we will be generating most of our input using the
keyboard.
 Now, how do we tell the java program that we want to use a
keyboard to get input from the user?
 How do we actually get the input from the user once the
program knows what to do with it?
Chapter 9.4, 9.5: Input
The Scanner Class
 To read input from the keyboard we use the Scanner class.
 Like Random, the Scanner class is defined in the Java Library
package called java.util, so we must add the following statement at
the top of our programs that require input from the user:
import java.util.*; // <-- I usually do this
or
import java.util.Scanner;
https://docs.oracle.com/javase/7/docs/api/java/util/package-summary.html
The Scanner Class
 Like print and println, Scanner objects work with Java’s
System class, but instead of .out it works with .in,
and is set up a bit differently
 To create a Scanner object:
Scanner keyboard = new Scanner(System.in);
NOTE:
Like any other object, keyboard here is just a name “made up” by the coder and can
be called anything instead—input, feedIine, keyIn, data, stuffComingFromTheUser,
etc.—although it should represent a word most apt to its purpose.
In this case I am using the name keyboard since it seems apt as I’ll be using the
keyboard to enter data (i.e., do the input)
Simple Input Logic
Create Variable Bucket to Hold Input
Get Input and Put in Variable Bucket
Do Something with Input in Variable Bucket
This is accomplished by the Scanner object using Scanner input methods
which we’ll look at in a moment. But first, let’s look at a simple input
program that demonstrates this simple input logic.
import java.util.*; // <-- Import library to use Scanner class
public class ReadConsoleBasic extends Object
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in); // Scanner object
System.out.println("Please enter an integer:");
int userInput = 0;
 Create Variable
userInput = keyboard.nextInt();
 Get Input
System.out.println("You entered " + userInput);  Do Something with Input
}
}
ReadConsoleBasic.java
Scanner Input Methods for Integers
 These are int (integer) methods. There are also
Scanner methods available for floats, etc, which we'll
see later on in the quarter 
 nextInt()
 Assumes there is an int and does something with it
 hasNextInt()
 Checks to see if there is an int (boolean true or false)
 nextLine()
 Replaces the int in the keyboard buffer with a newline
character (Enter) so the program won't use the int again
Example: ReadConsole.java
import java.util.*; // Or import java.util.Scanner;
public class ReadConsole
{
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
// kb is the Scanner object
System.out.print("Enter an integer: ");
int a = kb.nextInt();
// nextInt() is a Scanner method
System.out.print("Enter an integer: ");
int b = kb.nextInt();
// nextInt() is a Scanner method
System.out.println(a + " * " + b + " = " + a * b);
}
}
A QUICK NOTE about Integer Division
Integer Division
 Division can be tricky.
In a Java program, what is the value of X = 1 / 2?
 You might think the answer is 0.5…
 But, that’s wrong.
 The answer is simply 0.
 Integer division will truncate any decimal remainder.
 If you are going to divide and need a decimal, then your
must use either the float or double types.
Let’s look at ReadConsole.java again but this time replacing multiplication *
with division /
Scanner Input Methods for Integers
 These are int (integer) methods. There are also
Scanner methods available for floats, etc, which we'll
see later on in the quarter 
 nextInt()
 Assumes there is an int and does something with it
 hasNextInt()
 Checks to see if there is an int (boolean true or false)
 nextLine()
 Replaces the int in the keyboard buffer with a newline
character (Enter) so the program won't use the int again
There are several demo programs to look at, so let’s look!
A Closer Look: Basic_Keyboard_IO.java
A Look at Assignment 3 "The Maze"
You’ll have everything you need to successfully complete Assignment 3 "The
Maze“ AFTER Lecture 11 (May 2nd) lecture on Instance Variables
A Closer Look: The ICE Exercises
else
{
System.out.println("You have not input a valid integer");
keyboard.nextLine();
}