1 / 16

BIT115: Introduction to Programming

BIT115: Introduction to Programming. Lecture 8. Instructor: Craig Duckett. Assignments 2, 3. Assignment 2 Due TONIGHT Lecture 8 by midnight Monday, October 20 th Assignment 1 Revision due Lecture 11 by midnight Wednesday, October 29 th

tatum
Télécharger la présentation

BIT115: Introduction to Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. BIT115: Introduction to Programming Lecture 8 Instructor: Craig Duckett

  2. Assignments 2, 3 • Assignment 2Due TONIGHTLecture 8 by midnight • Monday, October 20th • Assignment 1 Revisiondue Lecture 11 by midnight • Wednesday, October 29th • Assignment 2 RevisionDue Lecture 13 by midnight • Wednesday, November 5th • Assignment 3 Due Lecture 14 by midnight • Wednesday, November 12th(1, 2 or 3-Person Team) • We'll Have a Look at Assignment 3 at the End of Lecture

  3. MID-TERM and Buffer Day Mid-Term is LECTURE 10, Monday, October 27th • Please be prompt, and bring a pencil … don’t worry, I’ll supply the paper Buffer Day LECTURE 9, Wednesday, October 22nd • NO LECTURE – Study Session / Homework Session / Q & A / Class Optional

  4. Lecture 8 and Going Forward • Lecture 8 ENDSTHE 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

  5. But First… The Quiz!

  6. Chapter 9.4, 9.5: Input The Scanner Class • To read input from the keyboard we can use the Scannerclass. • Like Random, the Scannerclass 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;

  7. The Scanner Class • Scanner objects work with System.in • To create a Scannerobject: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.

  8. Example: ReadConsole.java importjava.util.Scanner; // Or import java.util.*;public classReadConsole{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

  9. New Scanner Methods • These 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

  10. 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 truncateany decimal remainder. • If you are going to divide and need a decimal, then your must use either the floator doubletypes.

  11. importjava.util.Scanner; // Or import java.util.*;public classReadConsoleChecked{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 flushSystem.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 flushbreak; } else { String next = keyboard.nextLine(); // newline flushSystem.out.println(next + " is not an integer such as 10 or -3."); } }System.out.println(a + " * " + b + " = " + a * b); }}

  12. New Scanner Methods • These 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

  13. A Closer Look: Basic_Keyboard_IO.java

  14. A Look at Assignment 3 "The Maze"

  15. A Closer Look: The ICE Exercises else { System.out.println("You have not input a valid integer"); keyboard.nextLine(); }

More Related