Presentation is loading. Please wait.

Presentation is loading. Please wait.

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,

Similar presentations


Presentation on theme: "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,"— Presentation transcript:

1 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, & Rules of thumb for developing programs. Schema notes and rules of thumb based on CS100 notes by Tim Teitelbaum, Fall 1996.

2 CS100A, Fall 1997, Lecture 92 Input/Output Transmitting information between the computer and the outside world. Microprocessor Memory a b 17 b = in.readInt(); 17 34 a = 2*b; 34 System.out.println(a);

3 CS100A, Fall 1997, Lecture 93 Basic Input Classes and Operations A simple program to read two numbers and print their sum (beginning). import java.io.*; public static void main(String args[]) {int a, b; // create System.in window & initialize // Text variable in to read from it. SystemInput sysIn = new SystemInput(); Text in = new Text(System.in);... Notes: Class SystemInput creates a small text input window. Once initialized, variable sysIn may be ignored. Class Text provides simple input methods for basic data types.

4 CS100A, Fall 1997, Lecture 94 A simple program to read two numbers and print their sum (continued). // Read two numbers and print their sum System.out.print ("Please enter an integer: "); System.out.flush( ); a = in.readInt( ); System.out.print ("Please enter another integer: "); System.out.flush( ); b = in.readInt( ); System.out.println(a + " + " + b + " = " + (a+b)); }

5 CS100A, Fall 1997, Lecture 95 System.in and Class Text The standard Java variable System.in represents the keyboard, but is very limited. The only available methods read either a single character or an entire line. Class Text is included in the CUCS Java stationery. It extends input classes like System.in to allow simple input of variables of type int, double, char, and String. The initialization Text in = new Text(System.in); creates a variable, in, of class Text and connects it to System.in.

6 CS100A, Fall 1997, Lecture 96 Methods in Class Text Most methods in class Text skip over any “whitespace” (blanks, tabs, and newlines), then read the next non-blank chunk of input and return it as a value of the specified type. Available methods: readInt( ) — read int value readDouble( ) — read double (floating pt) readString( ) — read non-blank string An additional method readLine( ) discards the rest of the input on the current line and returns the entire next line as a single string. See the comments at the beginning of Text.java for more detailed documentation.

7 CS100A, Fall 1997, Lecture 97 A Data Processing Problem Input data: Zero or more exam grades in the range 0 to 100, followed by a –1. Sample input data: 90 85 93 40 89 -1 Empty input sequence: –1 General task: Read grades and print some information about them. Possible tasks: –Print the grades –Print the number of grades –Print the average grade –Print the highest grade –Print the grades sorted descending –Combinations of the above

8 CS100A, Fall 1997, Lecture 98 Example: Print the Grades public static void main(String args[]) {SystemInput sysIn = new SystemInput(); Text in = new Text(System.in); int grade;// current grade // Read grades until -1 and print them grade = in.readInt( ); while (grade != -1) { System.out.println(grade); grade = in.readInt( ); }

9 CS100A, Fall 1997, Lecture 99 Example: Print the Number of Grades public static void main(String args[]) {SystemInput sysIn = new SystemInput(); Text in = new Text(System.in); int grade; // current grade int nGrades; // # grades read so far // Read grades until -1 and print how many grade = in.readInt( ); nGrades = 0; while (grade != -1) { nGrades = nGrades + 1; grade = in.readInt( ); } System.out.println(“# grades = ” + nGrades); }

10 CS100A, Fall 1997, Lecture 910 Program Schema A program scheme is a pattern of code that has general utility. Learn it, then use it. Example: public static void main(String args[]) {(SystemInput and Text deleted) // “Process” list of input values up to, // but not including, a stopping value int variable; declarations;  variable = in.readInt( ); while (variable != stoppingValue) {  variable = in.readInt( ); }  }

11 CS100A, Fall 1997, Lecture 911 Placeholders in the Program Schema variable, declarations, stoppingValue, , , and  are placeholders. variable contains the most recently read input value. It should be replaced with a name meaningful for the given problem. stoppingValue is the value that signals the end of the input.  is the initialization: statements that initialize any necessary variables before any input value is “processed”.  is the “processing” step for each input value (but not for stoppingValue).  is the finalization: any necessary operations after all input values have been processed.

12 CS100A, Fall 1997, Lecture 912 Program scheme instantiated for grading public static void main(String args[]) {(SystemInput and Text deleted) int grade; // current grade declarations;  grade = in.readInt( ); while (grade != -1) {  grade = in.readInt( ); }  }

13 CS100A, Fall 1997, Lecture 913 Example: Print the Number of Grades public static void main(String args[]) {(SystemInput and Text omitted) int grade; // current grade  : grade = in.readInt( ); while (grade != -1) {  : grade = in.readInt( ); }  : }

14 CS100A, Fall 1997, Lecture 914 Example: Print the Average Grade public static void main(String args[]) {(SystemInput and Text omitted) int grade; // current grade  : grade = in.readInt( ); while (grade != -1) {  : grade = in.readInt( ); }  : }

15 CS100A, Fall 1997, Lecture 915 Example: Print the Average Grade (Boundary Conditions) public static void main(String args[]) {(SystemInput and Text omitted) int grade; // current grade  : grade = in.readInt( ); while (grade != -1) {  : grade = in.readInt( ); }  : }

16 CS100A, Fall 1997, Lecture 916 Programming Rules of Thumb Learn program schemes (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. Ask yourself “what am I doing?” Declare a variable for each piece of information (state) you keep track of while working the problem by hand. Write a comment that precisely describes the contents of each variable. Remember to take care of boundary conditions. Check your program by hand tracing it on simple test data.


Download ppt "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,"

Similar presentations


Ads by Google