Lecture 10 Instructor: Craig Duckett Lecture 10 is in Lecture 11 Folder.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Lecture 8 Instructor: Craig Duckett. Assignments TONIGHT Lecture 8 Assignment 2 Due TONIGHT Lecture 8 by midnight Monday, February 2 nd Lecture 10 Assignment.
Computer Programming Lab 8.
Lecture 17 Instructor: Craig Duckett Passing & Returning Arrays.
CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
18 File handling1June File handling CE : Fundamental Programming Techniques.
COMP 110 Branching Statements and Boolean Expressions Tabitha Peck M.S. January 28, 2008 MWF 3-3:50 pm Philips
If statements Chapter 3. Selection Want to be able to do a statement sometimes, but not others if it is raining, wear a raincoat. Start first with how.
LAB 10.
Computer Programming Lab(4).
Announcements Quiz 2 Grades Posted on blackboard.
Computer Programming Lab(5).
The for-statement. Different loop-statements in Java Java provides 3 types of loop-statements: 1. The for-statement 2. The while-statement 3. The do-while-statement.
JAVA Control Structures: Repetition. Objectives Be able to use a loop to implement a repetitive algorithm Practice, Practice, Practice... Reinforce the.
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
Lecture 6a Instructor: Craig Duckett. Upcoming Assignments & Mid-Term Assignment 1 Revision Assignment 1 Revision is due NEXT Wednesday, July 29 th, by.
Conditional If Week 3. Lecture outcomes Boolean operators – == (equal ) – OR (||) – AND (&&) If statements User input vs command line arguments.
 The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might.
Week 2 - Wednesday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
1 Fencepost loops “How do you build a fence?”. 2 The fencepost problem Problem: Write a class named PrintNumbers that reads in an integer called max and.
CSCI S-1 Section 6. Coming Soon Homework Part A – Friday, July 10, 17:00 EST Homework Part B – Tuesday, July 14, 17:00 EST Mid-Term Quiz Review – Friday,
Boolean expressions, part 2: Logical operators. Previously discussed Recall that there are 2 types of operators that return a boolean result (true or.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
The if-else statement. The if-else statement in Java The if-else statement is the second conditional statement in Java The if-else statement selects one.
COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson
Java 1.5 The New Java Mike Orsega Central Carolina CC.
1 Reasoning about assertions Readings: Assertions assertion: A statement that is either true or false. Examples:  Java was created in (true)
Output Programs These slides will present a variety of small programs. Each program has a control structure that was introduced in this chapter. Our concern.
SELF STUDY. IF STATEMENTS SELECTION STRUCTURE if selection statement if … else selection statement switch selection statement.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
COMP 110 Branching Statements and Boolean Expressions Luv Kohli September 8, 2008 MWF 2-2:50 pm Sitterson
Lecture 11 Instructor: Craig Duckett. Announcements Assignment 1 Revision DUE TONIGHT February 10 th In StudentTracker by midnight If you have not yet.
Java Scanner Class Keyboard Class. User Interaction So far when we created a program there was no human interaction Our programs just simply showed one.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Switch Statement.
© 2007 Lawrenceville Press Slide 1 Chapter 4 Review Assignment Statement An assignment statement gives a value to a variable. Assignment can take several.
Computer Programming Lab 9. Exercise 1 Source Code package excercise1; import java.util.Scanner; public class Excercise1 { public static void main(String[]
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Interactive Programs Programs that get input from the user 1 In PowerPoint, click on the speaker icon then click the "Play" button to hear the narration.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
A.P. Computer Science Input is NOT tested on the AP exam, but if we want to do any labs, we need input!!
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Introduction to programming in java
CompSci 230 S Programming Techniques
BIT 115: Introduction To Programming
BIT 115: Introduction To Programming
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
CSC1401 Input and Output (and we’ll do a bit more on class creation)
Computer Programming Methodology Input and While Loop
Something about Java Introduction to Problem Solving and Programming 1.
Building Java Programs
BIT115: Introduction to Programming
Building Java Programs
Building Java Programs
Building Java Programs
Introduction to Classes and Methods
Java so far Week 7.
Building Java Programs
Building Java Programs
Self study.
Lecture Notes – Week 2 Lecture-2
BIT115: Introduction to Programming
Building Java Programs
Building Java Programs
Building Java Programs
Presentation transcript:

Lecture 10 Instructor: Craig Duckett Lecture 10 is in Lecture 11 Folder

2 Assignment 2 (LECTURE 8) GRADED! RETURNED! Wednesday, April 29 Assignment 1 Revision (LECTURE 10) Wednesday, May 6 Assignment 2 Revision (LECTURE 12) Wednesday, May 13 Assignment 3 (LECTURE 13) Monday, May 18 Assignment 3 Revision (LECTURE 16) Monday, June 1 Assignment 4 (LECTURE 20) Wednesday, June 10 Assignment 4 Revision (LECTURE 21) Monday, June 15 Assignment Dates DUE NEXT

Lecture 10 Announcements TODAY BEGINS THE SECOND HALF OF THE QUARTER --- WHAT THIS MEANS: Less Theory, More Hands-On Work Less Theory, More Hands-On Work (Less means Less, not No) Less Hand-Holding, More Trial-and-Error Less Hand-Holding, More Trial-and-Error Less Explanation, More Research & Investigation, More Poking Around For Code, More “Googling It” and More (Occasional) Aggravation Less Explanation, More Research & Investigation, More Poking Around For Code, More “Googling It” and More (Occasional) Aggravation Becker – Chapters 9.4, 9.5: Input System.in The Scanner Class

Lecture 10 Announcements What We Will Be Going Over Today Becker – Chapters 9.4, 9.5: Input System.in The Scanner Class

But First… The Quiz!

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 again we will use the following statement at the top of our programs: import java.util.*; or import java.util.Scanner;

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.

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

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.

import java.util.Scanner; // Or import java.util.*; public class ReadConsoleChecked { public static void main(String[] args) { Scanner cin = new Scanner(System.in); int a = 0; while (true) // <-- A new kind of while loop { System.out.print("Enter an integer: "); if (cin.hasNextInt()) // Checks to see whether an int has been typed in keyboard { a = cin.nextInt(); cin.nextLine(); // newline flush break; } else { String next = cin.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 (cin.hasNextInt()) { b = cin.nextInt(); cin.nextLine(); // newline flush break; } else { String next = cin.nextLine(); // newline flush System.out.println(next + " is not an integer such as 10 or -3."); } } System.out.println(a + " * " + b + " = " + a * b); } }

A Closer Look: Basic_Keyboard_IO.java

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