CS100A Lect. 10, 1 Oct Input/Output & Program Schema

Slides:



Advertisements
Similar presentations
I/O Basics 12 January 2014Smitha N. Pai, CSE Dept.1.
Advertisements

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Lecture 2 Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
File I/O and Exceptions File I/O Exceptions Throwing Exceptions Try statement and catch / finally clauses Checked and unchecked exceptions Throws clause.
Java review and more. Class Header  Public class Welcome  Case sensitive  Body of the class must be enclosed by braces.
CS100A, Fall 1997, Lecture 111 CS100A, Fall 1997 Lecture 11, Tuesday, 7 October Introduction to Arrays Concepts: Array declaration and allocation Subscripting.
03 Data types1June Data types CE : Fundamental Programming Techniques.
1 LECTURE#7: Console Input Overview l Introduction to Wrapper classes. l Introduction to Exceptions (Java run-time errors). l Console input using the BufferedReader.
CMT Programming Software Applications
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
CS 100Lecture 41 CS100J Lecture 4 n Previous Lecture –Programming Concepts n iteration n programming patterns (templates) –Java Constructs n while-statements.
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 Wanda M. Kunkle.
© 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.
H2-1 University of Washington Computer Programming I Lecture 10: Loop Development and Program Schemas © 2000 UW CSE.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Console Input. So far… All the inputs for our programs have been hard-coded in the main method or inputted using the dialog boxes of BlueJ It’s time to.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
CS100A, Fall 1997, Lecture 91 CS100A, Fall 1997 Lecture 9, Tuesday, 30 September Input/Output & Program Schema System.in, class Text, Some basic data processing,
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.
CSC Programming I Lecture 6 September 4, 2002.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
CS 100Lecture 21 CS100J: Lecture 2 n Previous Lecture –Programming Concepts n problem, algorithm, program, computer, input, output, sequential execution,
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
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.
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.
I/O Basics 26 January Aside from print( ) and println( ), none of the I/O methods have been used significantly. The reason is simple: most real.
CSCI 1100/1202 January 23, Class Methods Some methods can be invoked through the class name, instead of through an object of the class These methods.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
CSC 110 – Intro to Computing - Programming
CS 100Lecture 11 Introduction to Programming n What is an algorithm? n Input and output n Sequential execution n Conditional execution Reading: Chapter.
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
Introduction to programming in java Lecture 21 Arrays – Part 1.
CSC111 Quick Revision.
Introduction to Arrays
Input/Output.
Java Course Review.
Introduction to Computer Science / Procedural – 67130
Yanal Alahmad Java Workshop Yanal Alahmad
2.5 Another Java Application: Adding Integers
Building Java Programs
Revision Lecture
Getting Started with C.
I/O Basics.
Java Programming: From Problem Analysis to Program Design, 4e
INPUT STATEMENTS GC 201.
CSS 161 Fundamentals of Computing Introduction to Computers & Java
Introduction to Classes and Methods
MSIS 655 Advanced Business Applications Programming
CS100J Lecture 11 Previous Lecture This Lecture
CS100J Lecture 7 Previous Lecture This Lecture Java Constructs
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Introduction to Arrays
CS100J Lecture 16 Previous Lecture This Lecture Programming concepts
CS100J Lecture 16 Previous Lecture This Lecture Programming concepts
Introduction to C Programming
loops revisited I/O arrays
Presentation transcript:

CS100A Lect. 10, 1 Oct. 1998 Input/Output & Program Schema Topics in this lecture: System.in Class TokenReader Some basic data processing Rules of thumb for developing programs Classes that provide facilities for input and output (or i/o) appear in package java.io. To make these classes available, place the phrase import java.io.*; at the top of the Java source file. What we discuss about input in this lecture appears in the CUCS Java Application stationery. CS100A, Lecture 10, 1 October 1998

Basic input classes and operations Simple method main to read two numbers and print their sum (explanation on next slide). public static void main (String arg[]) { int a, b; // initialize TokenReader object in to read // from standard input. TokenReader in = new TokenReader(System.in); // Read one number into a System.out.print("Please enter an integer: "); System.out.flush( ); a = in.readInt( ); // Read one number into b b = in.readInt( ); System.out.println(a + “ + “ + b + “ = “ + (a+b)); } CS100A, Lecture 10, 1 October 1998

TokenReader in = new TokenReader(System.in); Static field System.in (i.e. static field in of class System) is a variable of class file-name that is connected to the keyboard and to the execution window with the title JVIEW CUCSApplication. But this class is very limited --the only available methods read either a single character or an entire line. Class TokenReader, which is in the CUCS Java stationery, provides more useful input methods; it allows simple input of variables of type int, double, char, and String. The statement TokenReader in = new TokenReader(System.in); stores in variable in a new instance of class TokenReader, which is connected to the input file given by file name System.in. Thereafter, one can use several methods of class TokenReader to read values in. CS100A, Lecture 10, 1 October 1998

Methods (functions) in class TokenReader // read and return a value of type int public int readInt( ) // read and return a value of type double public double readDouble( ) // read and return a value of class String public String readString( ) These three methods above ignore beginning whitespace --blanks, tabs, new-lines, carriage returns. The value is terminated by whitespace. Thus, the string read in by readString can’t contain blanks. // Skip the rest of the input line and return as a string // all the characters on the next line public String readLine( ) See the comments at the beginning of TokenReader for more information. CS100A, Lecture 10, 1 October 1998

Data processing problem Input: zero or more exam grades in the range 0..100. The exam grades are followed by -1. Sample input: 90 85 93 40 89 -1 Sample input: -1 Task: Read grades and print some information about them: print grades print the number of grades print the average grade print the highest grade print grades in descending order combinations of the above CS100A, Lecture 10, 1 October 1998

Example: Print the grades // Read in and print grades. Grades are in range 0..100 // and are ended by -1 public static void main (String arg[]) { TokenReader in = new TokenReader(System.in); int g; // the last number read in g= in.readInt( ); // invariant: g is the last number read in, and // all grades before it have been printed while (g != -1) { System.out.println(g); } CS100A, Lecture 10, 1 October 1998

Example: Print the number of grades // Read in grades in range 0..100, which are ended // by -1. Print the number of grades public static void main (String arg[]) { TokenReader in = new TokenReader(System.in); int g; // the last number read in int no; // no of grades appearing before g g= in.readInt( ); // invariant: g is the last number read in, and // no is the number of grades before g while (g != -1) { no= no+1; } System.out.println(“no. of grades is ” + no); CS100A, Lecture 10, 1 October 1998

Program Schemes A program scheme is a pattern of code that has general utility --it can be used many times. Learn it, then use it extensively. Example: public static void main (String arg[]) { TokenReader in = new TokenReader(System.in); // Process a list of input values up to, but not including // a stopping-value. int var; declarations  var= in.readInt( ); // Invariant: var contains last value read and // … while (var != stopping-value) {  }  CS100A, Lecture 10, 1 October 1998

Placeholders in the program schema var, declarations, stopping-value var contains the most recently read input value. stopping-value is the value that signals the end of the input  is the initialization statements that truthify the invariant  is the “processing” step for each input value (except the stopping-value)  is the finalization --these statements perform any necessary operations after all input values have been processed. CS100A, Lecture 10, 1 October 1998

Program Scheme Instantiated for Grading public static void main (String arg[]) { TokenReader in = new TokenReader(System.in); // Process a list of input values up to, but not including // a stopping-value. int g; declarations  g= in.readInt( ); // Invariant: g contains last value read and // … while (g != -1) {  }  CS100A, Lecture 10, 1 October 1998

Example: print number of grades public static void main (String arg[]) { TokenReader in = new TokenReader(System.in); // Process a list of input values up to, but not including // a stopping-value. int g; declarations (none)  g= in.readInt( ); // Invariant: g contains last value read and // … while (g != -1) {  }  CS100A, Lecture 10, 1 October 1998

Example: print the average grade public static void main (String arg[]) { TokenReader in = new TokenReader(System.in); // Process a list of input values up to, but not including // a stopping-value. int g; declarations (none)  g= in.readInt( ); // Invariant: g contains last value read and // … while (g != -1) {  }  CS100A, Lecture 10, 1 October 1998

Example: print the average grade (but dealing with the boundary condition -no grades) public static void main (String arg[]) { TokenReader in = new TokenReader(System.in); // Process a list of input values up to, but not including // a stopping-value. int g; declarations (none)  g= in.readInt( ); // Invariant: g contains last value read and // … while (g != -1) {  }  CS100A, Lecture 10, 1 October 1998

Programming Rules of Thumb Learn program schema (patterns) of general utility. If you know a relevant program scheme for the problem at hand, use it. Work the problem at hand to gain insight into its solution --think about it with respect to some particular example data and see what you think the answer would be. Ask yourself, “What am I doing?” Declare a variable for each piece of information (state) you keep track of while working the problem at hand.. Write a comment that describes precisely the contents of each variable. Remember to take care of boundary conditions Check your program by hand tracing it on simple test data (or trace it using the debugger). CS100A, Lecture 10, 1 October 1998