ICS 4M Introduction to Programming in Java Chapter 6 Notes 6.1 Entering Programs Programs are entered using the environment editor. Syntax errors are reported at compilation time and corrections can be made. When there are no errorsm the program can run. If results do not match what is expected, the program must contain semantic errors. These errors must be found and corrected. 6.2 Standard Input and Output Java provides a way of reading input from the standard input device (the keyboard) and writing to the standard output device (the screen) but its built-in facilities for this are quite primitive. Reading from standard input uses a predefined object called System.in. This is an object of class InputStream. An InputStream can only read in single characters. To be useful, System.in must be converted to a DataInputStream, from which an entire line of input can be read at a time. The statement DataInputStream stdin = new DataInputStream (System.in); performs the conversion. Once, this is done, the readline method is used to read a line of input from the standard input device. The System.out object s print and println methods send data to the standard output device. System.out.print (s)outputs a string s not followed by a Return while System.out.println (i) output an integer I followed by Return. Example: // The "EchoName" class. // Asks for your name and echoes it back.
public class EchoName static public void main (String [] args) throws IOException String name; DataInputStream stdin = new DataInputStream (System.in); System.out.println ("Please type your name: "); name = stdin.readline (); // Output the reply. System.out.println ("Your name is \"" + name + "\""); /* EchoName class */ 6.3 Input of Numerical Data // The "NextYear" class. Input of INTEGER numbers. // This tells you how old you will be next year. public class NextYear int age; c.print ("Please enter your age: "); age = c.readint (); c.print ("Your age next year will be "); c.println (age + 1); // NextYear class
// The "FloorArea" class. Input of REAL numbers. // Find the area of a floor given its length and width. public class FloorArea double len, width; c.print ("Enter length in meters "); len = c.readdouble (); c.print ("Enter width in meters "); width = c.readdouble (); c.print ("Area is "); c.print (len * width); c.println (" square meters"); // FloorArea class 6.4 Input of String Data // The "NameInOut" class. Input of STRINGS. // Read a name and output it. public class NameInOut String name; c.println ("Please enter your full name"); name = c.readline (); c.println ("Your name is \"" + name + "\""); // NameInOut class
6.5 Input of Sequences of Data Reading in a KNOWN number of data. Use a COUNTER loop. // The "TenAvg" class. // Compute the average of 10 marks. public class TenAvg int mark; int sum = 0; c.println ("Enter 10 marks one to a line"); for (int count = 1 ; count <= 10 ; count++) mark = c.readint (); sum += mark; c.println ("Average of 10 marks is " + (double) sum / 10); // TenAvg class Reading in an UNKNOW number of data. Use a CONDITIONAL loop. // The "LastLetter" class. // Read a sequence of words and output the last letter // of each word until the sentinel is read. public class LastLetter
String word; final String SENTINEL = "stop"; c.println ("Enter a sequence of words, end with " + SENTINEL); // Words must have at least one letter. word = c.readline (); while (!word.equals (SENTINEL)) c.println ("Last letter of " + word + " is " + word.charat (word.length () - 1)); word = c.readline (); c.println ("This is the end of the sequence"); // LastLetter class 6.6 Input from a File To read text data from a file, an input object of class BufferedReader is instantiated by a statement such as input = new BufferedReader(new FileReader (filename)); All output to a text file is as strings of characters. Files of strings are terminated by null. // The "FileAvg" class. // Input a sequence of integers entered originally one to a line // from a file whose name is to be read in from the keyboard // and find their average. public class FileAvg throws IOException
String filename, line; int number; int sum = 0, count = 0; c.println ("What is the name of the file of integers? "); filename = c.readline (); BufferedReader input; input = new BufferedReader (new FileReader (filename)); line = input.readline (); //Read a line of characters. while (line!= null) //File is terminated by a null. number = Integer.parseInt (line); count++; sum += number; line = input.readline (); //Change to an integer. //Read next line. c.println ("Average of " + count + " numbers is " + (double) sum / count); // FileAvg class 6.7 Output to a File To write text data to a named file, an object of type PrintWriter is declared and instantiated by chaining to a class FileWriter by statements of this type PrintWriter output; Output = new PrintWriter (new FileWriter(fileName)); // The "OddInts" class. // Produce a file of data consisting of the odd integers // from 1 to 361 inclusive. public class OddInts throws IOException
PrintWriter output; String filename; c.println ("What is the name of the file for the integers? "); filename = c.readline (); output = new PrintWriter (new FileWriter (filename)); for (int number = 1 ; number <= 361 ; number += 2) output.println (number); // Close file. output.close (); // OddInts class 6.8 Generated Data // The "DiceData" class. // Simulate the throw of two dice 300 times // and store the generated data in file "dice". public class DiceData throws IOException int die1, die2, roll; PrintWriter output; output = new PrintWriter (new FileWriter ("dice")); for (int count = 1 ; count <= 300 ; count++) die1 = (int) (Math.random () * 6) + 1; die2 = (int) (Math.random () * 6) + 1; roll = die1 + die2; output.println (roll);
output.close (); c.println ("Simulated data of 300 throws now in file 'dice' "); // DiceData class 6.9 Statistical Analysis of Data // The "DiceStats" class. // Compute the mean and variance for the simulated dice throws // in file "dice" prepared by the data generation program. public class DiceStats throws IOException int roll; int count = 0, sum = 0, sumofsquares = 0; String line; BufferedReader input; input = new BufferedReader (new FileReader ("dice")); line = input.readline (); while (line!= null) roll = Integer.parseInt (line); count++; sum += roll; sumofsquares += roll * roll; line = input.readline (); double average = (double) sum / count; double variance = (double) sumofsquares / count - average * average; c.println ("Average=" + average + " Variance=" + variance); // DiceStats class
6.10 Review Read textbook review. 6.11 Technical Terms Write out technical terms in your notes. Be sure you know them. 6.12 Exercises Pages 147-148 Do questions #1, #2, #3, #4