Lecture 8 Instructor: Craig Duckett. Assignments TONIGHT Lecture 8 Assignment 2 Due TONIGHT Lecture...

17
BIT115: Introduction to Programming Lecture 8 Instructor: Craig Duckett

Transcript of Lecture 8 Instructor: Craig Duckett. Assignments TONIGHT Lecture 8 Assignment 2 Due TONIGHT Lecture...

Page 1: Lecture 8 Instructor: Craig Duckett. Assignments TONIGHT Lecture 8 Assignment 2 Due TONIGHT Lecture 8 by midnight Monday, February 2 nd Lecture 10 Assignment.

BIT115: Introduction to

Programming

Lecture 8

Instructor:Craig Duckett

Page 2: Lecture 8 Instructor: Craig Duckett. Assignments TONIGHT Lecture 8 Assignment 2 Due TONIGHT Lecture 8 by midnight Monday, February 2 nd Lecture 10 Assignment.
Page 3: Lecture 8 Instructor: Craig Duckett. Assignments TONIGHT Lecture 8 Assignment 2 Due TONIGHT Lecture 8 by midnight Monday, February 2 nd Lecture 10 Assignment.

Assignments

Assignment 2 Due TONIGHT Lecture 8 by midnightMonday, February 2nd

Assignment 1 Revision due Lecture 10 by midnightNext Monday, February 9th

Assignment 2 Revision Due Lecture 12 by midnightWednesday, February 18th

We'll Have a Look at Assignment 3 at the End of Lecture

Page 4: Lecture 8 Instructor: Craig Duckett. Assignments TONIGHT Lecture 8 Assignment 2 Due TONIGHT Lecture 8 by midnight Monday, February 2 nd Lecture 10 Assignment.

4

Assignment 2 (LECTURE 8) DUE TONIGHTMonday, February 2

Assignment 1 Revision (LECTURE 10) Monday, February 9

Assignment 2 Revision (LECTURE 12) Wednesday, February 18

Assignment 3 (LECTURE 13) Monday, February 23

Assignment 3 Revision (LECTURE 16) Wednesday, March 4

Assignment 4 (LECTURE 18) Wednesday, March 11

Assignment 4 Revision (LECTURE 19) Monday, March 16

Assignment Dates

Page 5: Lecture 8 Instructor: Craig Duckett. Assignments TONIGHT Lecture 8 Assignment 2 Due TONIGHT Lecture 8 by midnight Monday, February 2 nd Lecture 10 Assignment.

MID-TERMMid-Term is LECTURE 9, this Wednesday, February 4th • Please be prompt, and bring a pencil … don’t worry, I’ll supply the paper

Page 6: Lecture 8 Instructor: Craig Duckett. Assignments TONIGHT Lecture 8 Assignment 2 Due TONIGHT Lecture 8 by midnight Monday, February 2 nd Lecture 10 Assignment.

Lecture 8 and Going ForwardLecture 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

Page 7: Lecture 8 Instructor: Craig Duckett. Assignments TONIGHT Lecture 8 Assignment 2 Due TONIGHT Lecture 8 by midnight Monday, February 2 nd Lecture 10 Assignment.

But First…

The Quiz!

Page 8: Lecture 8 Instructor: Craig Duckett. Assignments TONIGHT Lecture 8 Assignment 2 Due TONIGHT Lecture 8 by midnight Monday, February 2 nd Lecture 10 Assignment.

Chapter 9.4, 9.5: Input

The Scanner ClassTo read input from the keyboard we can use

the Scanner class.Like Random, the Scanner class is defined in java.util, so again we will use the following statement at the top of our programs:

import java.util.*;orimport java.util.Scanner;

Page 9: Lecture 8 Instructor: Craig Duckett. Assignments TONIGHT Lecture 8 Assignment 2 Due TONIGHT Lecture 8 by midnight Monday, February 2 nd Lecture 10 Assignment.

The Scanner ClassScanner objects work with System.inTo create a Scanner object:

Scanner keyboard = new Scanner(System.in);

NOTE: Like any other object, keyboard here is a name “made up” by the coder and can be called anything—input, feedIine, keyIn, data, stuffComingFromTheUser, etc.—although it should represent a word most apt to its purpose.

In this case we are using keyboard since it seems most apt.

Page 10: Lecture 8 Instructor: Craig Duckett. Assignments TONIGHT Lecture 8 Assignment 2 Due TONIGHT Lecture 8 by midnight Monday, February 2 nd Lecture 10 Assignment.

Example: ReadConsole.java

import java.util.Scanner; // Or import java.util.*;

public class ReadConsole{ public static void main(String[] args) { Scanner cin = new Scanner(System.in); System.out.print("Enter an integer: "); int a = cin.nextInt(); System.out.print("Enter an integer: "); int b = cin.nextInt(); System.out.println(a + " * " + b + " = " + a * b); }}

A NOTE about Integer Division

Page 11: Lecture 8 Instructor: Craig Duckett. Assignments TONIGHT Lecture 8 Assignment 2 Due TONIGHT Lecture 8 by midnight Monday, February 2 nd Lecture 10 Assignment.

New Scanner MethodsThese are for ints (integers). There are also

Scanner methods available for floats, etc, which we'll see later on in the quarter

nextInt() Does something with the int

hasNextInt() Checks to see if there is an int

nextLine() Replaces the int in the keyboard buffer with a

newline character so the program won't use the int again

Page 12: Lecture 8 Instructor: Craig Duckett. Assignments TONIGHT Lecture 8 Assignment 2 Due TONIGHT Lecture 8 by midnight Monday, February 2 nd Lecture 10 Assignment.

Integer DivisionDivision 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.

Page 13: Lecture 8 Instructor: Craig Duckett. Assignments TONIGHT Lecture 8 Assignment 2 Due TONIGHT Lecture 8 by midnight Monday, February 2 nd Lecture 10 Assignment.

import java.util.Scanner; // Or import java.util.*;

public class ReadConsoleChecked{ public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); int a = 0; while (true) // <-- A new kind of while loop we haven’t talked about yet { System.out.print("Enter an integer: "); if (keyboard.hasNextInt()) // Checks to see whether an int has been typed in keyboard { a = keyboard.nextInt(); keyboard.nextLine(); // newline flush to “clear the buffer” break; // <-- We haven’t talked about break yet either } else { String next = keyboard.nextLine(); // newline flush System.out.println(next + " is not an integer such as 10 or -3."); } } int b = 0; while (true) // <-- A new kind of while loop { System.out.print("Enter an integer: "); if (keyboard.hasNextInt()) { b = keyboard.nextInt(); keyboard.nextLine(); // newline flush break; } else { String next = keyboard.nextLine(); // newline flush System.out.println(next + " is not an integer such as 10 or -3."); } } System.out.println(a + " * " + b + " = " + a * b); }}

Page 14: Lecture 8 Instructor: Craig Duckett. Assignments TONIGHT Lecture 8 Assignment 2 Due TONIGHT Lecture 8 by midnight Monday, February 2 nd Lecture 10 Assignment.

New Scanner MethodsThese are for ints (integers). There are also

Scanner methods available for floats, etc, which we'll see later on in the quarter

nextInt() Does something with the int

hasNextInt() Checks to see if there is an int

nextLine() Replaces the int in the keyboard buffer with a

newline character so the program won't use the int again

Page 15: Lecture 8 Instructor: Craig Duckett. Assignments TONIGHT Lecture 8 Assignment 2 Due TONIGHT Lecture 8 by midnight Monday, February 2 nd Lecture 10 Assignment.

A Closer Look: Basic_Keyboard_IO.java

Page 16: Lecture 8 Instructor: Craig Duckett. Assignments TONIGHT Lecture 8 Assignment 2 Due TONIGHT Lecture 8 by midnight Monday, February 2 nd Lecture 10 Assignment.

A Look at Assignment 3 "The Maze"

Page 17: Lecture 8 Instructor: Craig Duckett. Assignments TONIGHT Lecture 8 Assignment 2 Due TONIGHT Lecture 8 by midnight Monday, February 2 nd Lecture 10 Assignment.

A Closer Look: The ICE Exercises

else { System.out.println("You have not input a valid integer"); keyboard.nextLine(); }