BIT115: Introduction to Programming

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.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Console Input Using the Scanner Class Starting with version 5.0, Java includes a class for doing.
Exception Handling (Chapter 8) CS 180 Recitation - February 29, 2008 Department of Computer Science Purdue University.
Chapter 2 Section 2.2 Console Input Using The Scanner CLASS Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska.
Conditional If Week 3. Lecture outcomes Boolean operators – == (equal ) – OR (||) – AND (&&) If statements User input vs command line arguments.
Lecture 10 Instructor: Craig Duckett Lecture 10 is in Lecture 11 Folder.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Lecture 2: Classes and Objects, using Scanner and String.
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.
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.
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
CHAPTER 5 GC 101 Input & Output 1. INTERACTIVE PROGRAMS  We have written programs that print console output, but it is also possible to read input from.
Introduction Chapter 1 8/31 & 9/1 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Lesson 4 Input. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are using. The reason it is not.
Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Programming Fundamentals I Java Programming Spring 2009 Instructor: Xuan Tung Hoang TA: Tran Minh Trung Lab 03.
Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
Lecture 11 Instructor: Craig Duckett. Announcements Assignment 1 Revision DUE TONIGHT February 10 th In StudentTracker by midnight If you have not yet.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
Primitive Data Types int is a primitive data type A primitive data type is one that stores only a single piece of data. TypeStorageDescription int 4 bytes+ve.
© 2007 Lawrenceville Press Slide 1 Chapter 4 Review Assignment Statement An assignment statement gives a value to a variable. Assignment can take several.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
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.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Lecture 11 Instructor: Craig Duckett Instance Variables.
Introduction to programming in java
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
BIT115: Introduction to Programming
Lecture 4 – Scanner & Style
CompSci 230 S Programming Techniques
BIT 115: Introduction To Programming
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
Chapter 2 Clarifications
BIT 115: Introduction To Programming
Input/Output.
Chapter 2 More on Math More on Input
BIT115: Introduction to Programming
CSC 1051 – Data Structures and Algorithms I
Computer Programming Methodology Input and While Loop
Something about Java Introduction to Problem Solving and Programming 1.
INPUT STATEMENTS GC 201.
BIT115: Introduction to Programming
Building Java Programs
Building Java Programs
BIT115: Introduction to Programming
Building Java Programs
CSS 161 Fundamentals of Computing Introduction to Computers & Java
Chapter 2 Edited by JJ Shepherd
Building Java Programs
MSIS 655 Advanced Business Applications Programming
BIT115: Introduction to Programming
Building Java Programs
Building Java Programs
Building Java Programs
Introduction to Java Brief history of Java Sample Java Program
BIT115: Introduction to Programming
Building Java Programs
Primitive Types and Expressions
Unit 3: Variables in Java
Building Java Programs
Presentation transcript:

BIT115: Introduction to Programming Lecture 8 " INPUT " Instructor: Craig Duckett

Assignments Assignment 1 Revision due Lecture 10 Assignment 2 Due TONIGHT Lecture 8 Assignment 1 Revision due Lecture 10 Assignment 2 Revision Due Lecture 12 We'll Have a closer look at Assignment 3 at end of Lecture 10. IT IS RECOMMENDED YOU DO NOT TRY TO COMPLETE ASSIGNMENT 3 UNTIL AFTER LECTURE 11.

Assignment Dates (By Due Date) Assignment 1 (LECTURE 5) GRADED! Section 1: Monday, January 22nd Assignment 2 (LECTURE 8) Section 1: Wednesday, January 31st Assignment 1 Revision (LECTURE 10) Section 1: Wednesday, February 7th Assignment 2 Revision (LECTURE 12) Section 1: Wednesday, February 14th Assignment 3 (LECTURE 13) Section 1: Wednesday, February 21st Assignment 3 Revision (LECTURE 16) Section 1: Monday, March 5th Assignment 4 (LECTURE 18) NO REVISION AVAILABLE! Section 1: Monday, March 12th The Fickle Finger of Fate

MID-TERM Mid-Term is NEXT LECTURE 9 Please be prompt, and bring a pencil … don’t worry, I’ll supply the paper

Lecture 10 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?

The Scanner Class Chapter 9.4, 9.5: Input 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; userInput = keyboard.nextInt(); System.out.println("You entered " + userInput); } }  Create Variable  Get Input  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 12 and the discussion of Instance Variables. You can start working on the navigation and thing-putting portion of the maze before Lecture 12, but you need Instance Variables for the move-counting and printing portion.

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