Presentation is loading. Please wait.

Presentation is loading. Please wait.

BIT 115: Introduction To Programming

Similar presentations


Presentation on theme: "BIT 115: Introduction To Programming"— Presentation transcript:

1 BIT 115: Introduction To Programming
Professor: Dr. Baba Kofi Weusijana Pronounced Bah-bah Co-fee Way-ou-see-jah-nah Call him “Baba” or “Dr. Weusijana”

2 2nd Half of Quarter Begins
Lecture 10 Log into Canvas Put your name sticker on your computer Announcements: Midterm Exam Post Mortem No quiz Getting input ICE10 (10 Points) 2nd Half of Quarter Begins

3 But First… The Quiz! Mid-Term Post-Mortem

4 BIT 115: Introduction To Programming
Midterm Exam Really good, overall Canvas will release answers on November 4th Use it as part of studying for final exam Expect feedback via the Java Code Critic after November 4th Then we’ll go over things I think people had trouble with These are likely to show up on final BIT 115: Introduction To Programming

5 TODAY BEGINS THE SECOND PART OF THE QUARTER!
WHAT DOES THIS MEAN IN THE COSMIC SCHEME OF THINGS? 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 (on occasion) Aggravation. Grrrr! When You Do Get Aggravated: Remember to STEP AWAY from your code occasionally, take a break, walk around, go and eat, contemplate the great outdoors, then come back. If it is late at night, go to bed. You may discover that bizarre thing that sometimes can happen, where you DREAM IN CODE, and wake up in the morning refreshed and with the beginnings of a solution!

6 TODAY BEGINS THE SECOND PART OF THE QUARTER!
WHAT DOES THIS MEAN IN THE COSMIC SCHEME OF THINGS? Less tracing! We will write move(); instead of this.move(); where we can When we are writing methods in our own Robot types/classes

7 Chapter 9.4, 9.5: Input The Scanner Class
To read input from the keyboard we can use the Scanner class. Like Random, the Scanner class is defined in java.util, so we will use the following statement at the top of our programs: import java.util.Scanner;

8 The Scanner Class Scanner objects work with System.in
To 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.

9 Example: ReadConsole.java
import java.util.Scanner; // Or 'import java.util.*;' but that’s overkill public class ReadConsole { public static void main(String[] args) { Scanner fromUser = new Scanner(System.in); System.out.print("Enter an integer: "); int a = fromUser.nextInt(); System.out.print("Enter an integer: "); int b = fromUser.nextInt(); System.out.println(a + " * " + b + " = " + a * b); } } We will cover integer division later

10 New Scanner Methods nextInt is for ints (integers). There are also Scanner methods available for floats, etc, which we'll see later on in the quarter nextInt() Returns an int for usage hasNextInt() Checks to see if there is an int (returns a boolean) nextLine() Replaces the int in the keyboard buffer with a newline character so the program won't use the int again

11 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.

12 import java.util.Scanner; 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); } } Note that you don’t have to explicitly write that ReadConsoleChecked extends Object

13 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

14 A Closer Look: Basic_Keyboard_IO.java
Use link to the file near the beginning of the ICE10 Instructions Demo

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

16 A Look at Assignment 3 "The Maze"


Download ppt "BIT 115: Introduction To Programming"

Similar presentations


Ads by Google