Download ICS 4M Introduction to Programming in Java Chapter 6 Notes 6.1

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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.
import java.awt.*;
import java.io.*;
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 + "\"");
} // main method
} /* 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.
import java.awt.*;
import hsa.Console;
public class NextYear
{
static Console c;
// The output console
public static void main (String [] args)
{
c = new Console ();
int age;
c.print ("Please enter your age: ");
age = c.readInt ();
c.print ("Your age next year will be ");
c.println (age + 1);
} // main method
} // NextYear class
// The "FloorArea" class. Input of REAL numbers.
// Find the area of a floor given its length and width.
import java.awt.*;
import hsa.Console;
public class FloorArea
{
static Console c;
// The output console
public static void main (String [] args)
{
c = new Console ();
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");
} // main method
} // FloorArea class
6.4 Input of String Data
// The "NameInOut" class. Input of STRINGS.
// Read a name and output it.
import java.awt.*;
import hsa.Console;
public class NameInOut
{
static Console c;
// The output console
public static void main (String [] args)
{
c = new Console ();
String name;
c.println ("Please enter your full name");
name = c.readLine ();
c.println ("Your name is \"" + name + "\"");
} // main method
} // 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.
import java.awt.*;
import hsa.Console;
public class TenAvg
{
static Console c;
// The output console
public static void main (String [] args)
{
c = new Console ();
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);
} // main method
} // 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.
import java.awt.*;
import hsa.Console;
public class LastLetter
{
static Console c;
// The output console
public static void main (String [] args)
{
c = new Console ();
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");
} // main method
} // 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.
import java.awt.*;
import java.io.*;
import hsa.Console;
public class FileAvg
{
static Console c;
// The output console
public static void main (String [] args) throws IOException
{
c = new Console ();
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);
//Change to an integer.
count++;
sum += number;
line = input.readLine ();
//Read next line.
}
c.println ("Average of " + count + " numbers is " + (double) sum / count);
} // main method
} // 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.
import java.awt.*;
import java.io.*;
import hsa.Console;
public class OddInts
{
static Console c;
// The output console
public static void main (String [] args) throws IOException
{
c = new Console ();
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 ();
} // main method
} // 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".
import java.awt.*;
import java.io.*;
import hsa.Console;
public class DiceData
{
static Console c;
// The output console
public static void main (String [] args) throws IOException
{
c = new Console ();
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' ");
} // main method
} // 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.
import java.awt.*;
import java.io.*;
import hsa.Console;
public class DiceStats
{
static Console c;
// The output console
public static void main (String [] args) throws IOException
{
c = new Console ();
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);
} // main method
} // 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