Download presentation
Presentation is loading. Please wait.
Published byΠαναγιώτα Δουμπιώτης Modified over 5 years ago
1
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
2
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
3
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
4
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
5
Data processing problem
Input: zero or more exam grades in the range The exam grades are followed by -1. Sample input: 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
6
Example: Print the grades
// Read in and print grades. Grades are in range // 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
7
Example: Print the number of grades
// Read in grades in range , 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
8
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
9
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
10
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
11
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
12
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
13
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
14
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.