Today’s topics: I/O (Input/Output). Scribbler Inputs & Outputs  What are the Scribbler’s inputs and outputs?  reset button  motors/wheel  light sensor.

Slides:



Advertisements
Similar presentations
Computer Hardware 4 Main Types.
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Datalogi A 1: 8/9. Book: Cay Horstmann: Big Java or Java Consepts.
18 File handling1June File handling CE : Fundamental Programming Techniques.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
Strings as objects Strings are objects. Each String is an instance of the class String They can be constructed thus: String s = new String("Hi mom!");
© 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.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: IO *Standard Output *Formatting Decimal.
COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.
Scanner & Stepwise Refinement Pepper With credit to Dr. Siegfried.
CS0007: Introduction to Computer Programming Scope, Comments, Code Style, Keyboard Input.
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Computer Programming Lab(5).
Chapter 2 Section 2.2 Console Input Using The Scanner CLASS Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska.
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
C# B 1 CSC 298 Writing a C# application. C# B 2 A first C# application // Display Hello, world on the screen public class HelloWorld { public static void.
Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1.
Sahar Mosleh California State University San MarcosPage 1 System.out.println for console output System.out is an object that is part of the Java language.
Week 2 - Wednesday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Today’s topic:  Arithmetic expressions. Arithmetic expressions  binary (two arguments) arithmetic operators  +, -, *, /, % (mod or modulus)  unary.
Input/Output in Java. Output To output to the command line, we use either System.out.print () or System.out.println() or System.out.printf() Examples.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
CS 106 Introduction to Computer Science I 01 / 31 / 2007 Instructor: Michael Eckmann.
First Programs CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
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.
Miscellaneous topics Chapter 2 Savitch. Miscellaneous topics Standard output using System.out Input using Scanner class.
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.
String and Scanner CS 21a: Introduction to Computing I First Semester,
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
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.
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
Lecture 4 – Scanner & Style. Console Output The console that starts a Java application is typically known as the standard output device. The standard.
E-1 University of Washington Computer Programming I Lecture 5: Input and Output (I/O) © 2000 UW CSE.
Martin T. Press.  Main Method and Class Name  Printing To Screen  Scanner.
Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
COP 2551 Introduction to Object Oriented Programming with Java Topics –Introduction to the Java language –Code Commenting –Java Program Structure –Identifiers.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
Keyboard and Screen I/O (Input/Output). Screen Output  System.out is an object that is part of the Java language. (Don’t worry yet about why there is.
Copyright © Curt Hill Simple I/O Input and Output using the System and Scanner Objects.
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.
Input and Output The system console. In the beginning … When computers were relatively expensive and rare, most users interacted with a terminal –CRT.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 6_1 GEORGE KOUTSOGIANNAKIS Copyright: FALL 2015 Illinois Institute of Technology- George Koutsogiannakis 1.
I/O in java and Revision. 2 I/O in Java Many packages and libraries associated with Java provide sophisticated ways to input and output information, e.g.:
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
Introduction to programming in java
Introduction to Computer Science and Object-Oriented Programming
Lecture 4 – Scanner & Style
CompSci 230 S Programming Techniques
User Input ICS2O.
Today’s topic: Arithmetic expressions.
Input/Output.
Chapter 2 More on Math More on Input
What Actions Do We Have Part 1
TemperatureConversion
CSC1401 Input and Output (and we’ll do a bit more on class creation)
Introduction to Computer Science / Procedural – 67130
Maha AlSaif Maryam AlQattan
Something about Java Introduction to Problem Solving and Programming 1.
INPUT STATEMENTS GC 201.
Scope, Comments, Code Style, Keyboard Input
Program Style Console Input and Output
CSS 161 Fundamentals of Computing Introduction to Computers & Java
Introduction to Classes and Methods
Introduction to Computer Science and Object-Oriented Programming
MSIS 655 Advanced Business Applications Programming
The keyboard is the standard input device.
Computer Programming-1 CSC 111
Presentation transcript:

Today’s topics: I/O (Input/Output)

Scribbler Inputs & Outputs  What are the Scribbler’s inputs and outputs?  reset button  motors/wheel  light sensor  speaker  LEDs  stall sensor  obstacle sensor = emitter + detector  line sensor

Scribbler Inputs

Scribbler Outputs

Is the serial port an input, an output, or both?

What outputs does our computer have?

1. write / print / draw to monitor / screen / display 2. speaker for sound 3. write information to disks (hard, CD, and DVD) 4. write information to network interface card (NIC)

What outputs does our computer have? How do we perform output to the display in Java?

What outputs does our computer have? How do we perform output to the display in Java?  System.out.print  System.out.println

What inputs does our computer have?

1. mouse (movement and buttons) 2. keyboard 3. read information from disks (hard, CD, and DVD) 4. read information from network interface card (NIC)

What inputs does our computer have? We will now focus on reading information from the keyboard.

Input/Output  Input from the keyboard import java.util.Scanner; … Scanner in = new Scanner( System.in ); //wait until the user types something and // presses the return/enter key. in.nextLine();

Input/Output  An I/O example //declare variable(s) Scanner in = new Scanner( System.in ); //perform some i/o System.out.println( "hello. Press 'return' to continue." ); in.nextLine(); System.out.println( "That’s all. Press 'return' again." ); in.nextLine(); //finish up in.close();

Input/Output  Another I/O example //this program echoes what you type //declare variables Scanner in = new Scanner( System.in ); //perform some i/o System.out.print( "Enter something & press 'return' to continue." ); String what = in.nextLine(); System.out.print( "You typed: " + what + ". Press 'return' again." ); in.nextLine(); //finish up in.close(); String concatenation. Example of operator overloading.

String input  We’ve already discussed nextLine().  Reads an entire line (including blanks and/or tabs) up to but not including return/enter.  Additionally, we have next().  Skips leading whitespace (blanks) including return.  In effect, next() reads words including punctuation.  Skips consecutive blanks (if any).  Note: “blanks” include space, tab, and return/newline.

String input String s; Scanner in = new Scanner( System.in ); System.out.print( "Enter input: " ); s = in.nextLine(); You enter: Oh! That is terrible! Oh! That is terrible! What is the value of s?

String input String s; Scanner in = new Scanner( System.in ); System.out.print( "Enter input: " ); s = in.nextLine(); You enter: Oh! That is terrible! Oh! That is terrible! What is the value of s? s = “ Oh! That is terrible! ”

String input String s1, s2, s3; Scanner in = new Scanner( System.in ); System.out.print( "Enter input: " ); s1 = in.next (); s2 = in.next(); s3 = in.nextLine(); You enter: Oh! That is terrible! Oh! That is terrible! What are the values of s1, s2, and s3?

String input String s1, s2, s3; Scanner in = new Scanner( System.in ); System.out.print( "Enter input: " ); s1 = in.next (); s2 = in.next(); s3 = in.nextLine(); You enter: Oh! That is terrible! Oh! That is terrible! What are the values of s1, s2, and s3? s1 = “Oh!”s2 = “That”s3 = “ is terrible! ”

Reading Strings Note: When reading from the keyboard (not from a file), you must type in your string (or strings) and then you must hit.

String input summary 2 methods (associated w/ string input): 1.next() – Skips leading blanks, tabs, and newlines. – Reads a string of letters, digits, and underscores. 2.nextLine() – Doesn’t skip leading blanks and tabs. – reads an entire line up to but not including \n (newline)

Input/Output  Can we read other things (ints, doubles) besides strings?

Input/Output  Can we read other things (ints, doubles) besides strings? Yes. //calculate F = M A //get values System.out.print( "enter mass: " ); double mass = in.nextDouble(); System.out.print( "enter acceleration: " ); double acc = in.nextDouble(); //calculate result double force = mass * acc; System.out.println( "force = " + force );

Input/Output  Can we read other things (ints, doubles) besides strings? Yes. //calculate F = M A //get values System.out.print( "enter mass: " ); double mass = in.nextDouble(); System.out.print( "enter acceleration: " ); double acc = in.nextDouble(); //calculate result double force = mass * acc; System.out.println( "force = " + force ); Can you convert this to use ints instead of doubles?

Input/Output  Can we read other things (ints, doubles) besides strings? Yes. //calculate F=MA //get values System.out.print( "enter mass: " ); int mass = in.nextInt(); System.out.print( "enter acceleration: " ); int acc = in.nextInt(); //calculate result int force = mass * acc; System.out.println( "force = " + force ); Can you convert this to use ints instead of doubles?

Complete program “skeleton” or “template.” Java is an object oriented programming language. All code must be associated with an object. We declare an object as follows: /* introductory comment(s) */ import java.util.Scanner; // comment(s) about this class. public class Assignment5 { … //comment(s) about this method (function). public static void main ( String args[] ) { Scanner in = new Scanner( System.in ); System.out.println( "Computer is ready." ); … } //end main … } //end class Assignment5 definition