CS100A Lect' 10, 1 Oct' 1998 - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

CS100A Lect' 10, 1 Oct' 1998

Description:

What we discuss about input in this lecture appears in the CUCS Java Application stationery. ... in the CUCS Java stationery, provides more useful input methods; ... – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 15
Provided by: david148
Category:
Tags: cs100a | lect | oct | stationery

less

Transcript and Presenter's Notes

Title: CS100A Lect' 10, 1 Oct' 1998


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.

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 System.out.print("Please enter an
integer ") System.out.flush( )
b in.readInt( ) System.out.println(a
b (ab))
3
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.
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 cant 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.
5
  • 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

6
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
) g in.readInt( )
7
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 no1 g
in.readInt( ) System.out.println(no. of
grades is no)
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) ? var
in.readInt( ) ?
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.

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) ? g in.readInt( ) ?
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) ? g in.readInt(
) ?
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) ? g in.readInt(
) ?
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) ? g in.readInt(
) ?
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).
Write a Comment
User Comments (0)
About PowerShow.com