Transcript (ppt)

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.
CS100A, Fall 1997, Lecture 9
1
Input/Output
Transmitting information between the
computer and the outside world.
34
System.out.println(a);
a
b
34
17
a = 2*b;
Microprocessor
Memory
b = in.readInt();
17
CS100A, Fall 1997, Lecture 9
2
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.
CS100A, Fall 1997, Lecture 9
3
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));
}
CS100A, Fall 1997, Lecture 9
4
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.
CS100A, Fall 1997, Lecture 9
5
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.
CS100A, Fall 1997, Lecture 9
6
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
CS100A, Fall 1997, Lecture 9
7
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( );
}
}
CS100A, Fall 1997, Lecture 9
8
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);
}
CS100A, Fall 1997, Lecture 9
9
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( );
}

}
CS100A, Fall 1997, Lecture 9
10
•
•
•
•
•
•
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.
CS100A, Fall 1997, Lecture 9
11
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( );
}

}
CS100A, Fall 1997, Lecture 9
12
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( );
}
:
}
CS100A, Fall 1997, Lecture 9
13
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( );
}
:
}
CS100A, Fall 1997, Lecture 9
14
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( );
}
:
}
CS100A, Fall 1997, Lecture 9
15
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.
CS100A, Fall 1997, Lecture 9
16