stdin
. When
the TAs test the program, the numbers will be piped to stdin
from a text file (e.g., the command line will read
java Proj1 < input-file
input-file
is a text file that you enter
using a word processor, and store it on disk.
More information about the test procedure is posted below under
Example Test Procedure.
stdin
(standard input
stream) as a stream of integers, with spaces, newline
characters, or commas between them (i.e., standard delimiters).
You should implement this as a method in a class called
P1Sequence
.
Hint: You might want to use a one-dimensional array class to store the input data for later processing. Array classes are described in Dr. Crummer's Java programming tutorial.
Step 2. Use the algorithm you developed in Homework #1 and
which we discussed in class, to find the maximum of the
sequence of integers input in Step 1. Code the algorithm as a
method maximum
within the class
P1Sequence
.
Step 3. Modify the maximum
method to compute
the minimum of the input sequence, and call this method
minimum
. Include this method in the class
P1Sequence
.
Note: This method should return an int
value.
Step 4. Modify the minimum
method to compute
the mean of the input sequence, and call this method
mean
. Include this method in the class
P1Sequence
.
Note: This method should return a
float
value, since the mean of the
sequence will likely be a decimal number. This also implies
that you should keep count of the number of input values, in
order to compute the mean.
Step 5. Modify the output statement in the classes
HelloWorld
and ForLoop
in Dr. Crummer's Java programming
tutorial to output your name, SSN, class code, as
well as the number of integers read in, minimum, maximum,
and mean of the input sequence, as shown in the example
listed after Step 7, below. Include this as a method called
writereport
in the class P1Sequence
.
Step 6. Use different sequences of positive and negative integers (with different numbers of integers per sequence) to test your program. Your program should work on a sequence of any finite length (i.e., one or more integers).
Step 7. After you have tested and completed your program,
use the submission procedure outlined below to submit your
project. You must pipe your input into the
program from a file, not use command-line parameters for the
data!!! The piping command is shown below under Example
Test Procedure. The following relevant Java code fragment
is taken from Dr. Crummer's tutorial, with the statement that
reads Instead of the variable --------- This method returns the next integer from the input.
If there are no more integers, it returns -1. Please
put this method into class P1Sequence.
Works only with positive numbers. The test will be
performed only on positive numbers.
Make sure that the method that uses this also throws
IOException.
--------- Note that this code handles only one-digit integers, but the test
would be made with two and 3 digit numbers also. Thus, it is better to
use Lloyd's code, shown above, and we provide Sean's code as an illustration
of exceptions only. Here follow a few comments from Sean:
Thank you, Sean, for contributing this code.
stdin
shown in green
typeface:
public class Count2 {
public static void main(String[] args)
throws java.io.IOException
{
System.out.println("Enter some chars,Ill count them (on UNIX, End with ^D):");
int count = 0;
System.out.println("Ill echo chars back to the screen (counting chars):");
System.out.println("and Ill show you what the chars unicode values are.");
System.out.println("Type, ending with (on UNIX, End with ^D):");
count = 0;
int valueTyped = 0;
while (valueTyped != -1)
{ valueTyped = System.in.read() ;
count++;
System.out.println(": you entered:" + valueTyped + " = "
+ (char)valueTyped ) ;
}
System.out.println("Input has " + count + " chars.");
}
valueTyped
, you need to put
the data into an array location (say,
arrayInt[i]
, just to pick an array name).
TA Lloyd Noronha writes the following example:
int v;
int nextInt=0;
//read the input until eof
while ( (v=System.in.read()) != -1) {
if ((v>'9') || v<'0') {
if (nextInt != 0)
//return the integer value when you hit the first
// non-digit
return nextInt;
else
//ignore multiple whitespaces
continue;
}
//build the integer from the digit
nextInt=nextInt*10+(v-'0');
}
return v;
}
On Wed 26 May 1999, Sean Denney contributed the following code,
which demonstrates reading of integers, and handles exceptions:
import java.io.*;
class Example throws IOException
{
int[] elements = new int[100];
public Example(/**/)
{/*constructor*/}
public void readElem()
{
int i = System.in.read();
element[0] = i;
int j = 1;
while(i != -1)
{
i = System.in.read();
element[j] = i;
j++;
}
}
}
System.in.read
method, import java.io.* .
throws IOException
.
while
loop, use System.in.read
to read in data from stdin
. The loop should
be exited when a -1 is encountered (indicates end of file).
try
or catch
command is needed.
Example Output. Given the following input sequence, where brackets enclose comments not included in the sequence:
23 [first number in sequence] 41 16 05 [4th number in sequence]your program output should look like this:
John Smith SSN: 123-345-6789 28 May 1999 COP3530-C99-Proj1 Numbers read in: 4 Minimum = 5 Maximum = 41 Mean = 21.25 - End -Example Test Procedure. Type up a text file "inFile" in either Dos or Unix with values like this: (As-of 5-26-99: use one integer per line of the file, to avoid I/O problems.)
25 35 57 92 38 56 44 101 38Now run your program this way:
java Proj1 < inFile
John Smith SSN: 123-345-6789 28 May 1999 COP3530-C99-Proj1 Numbers read in: 9 Minimum = 25 Maximum = 101 Mean = 54.0
Required Programming Procedure. Define each class in a
separate .java
file, with your master file as
Proj1.java
, where Proj1, which has a main method,
is the class name that the TAs will use to run your program. (It is
o.k. to have a main method in each class - the TAs are primarily
interested in the main
method in the Proj1
class.)
Other Hints on Programming Procedure. Use the following steps to guide your implementation of the Java version of the preceding program.
Hint 1. Get started early!!! Don't wait until the last two or three days, especially if you are new to Java programming.
Hint 2. Decide what classes you will use (Hint: An Array or Vector object might be useful, with Reader class or objects, System class, etc.) After you have figured out what classes and objects to use, design your Java class specification(s) on paper. Then (and only then) should you start coding.
Hint 3. Code your class(es) and methods, testing them incrementally (i.e., one at a time). Get each one working before you go on to the next one. If you write your program all in one blob of code, you will assuredly have difficulty debugging it.
Hint 4. Try your program on different kinds of output (e.g., large and small inputs, with both negative and positive integers. If your code breaks, fix it until it works on all inputs. Use the pipe construct on the command line to get the input into your application.
Hint 5. The TAs are here to help you with your problems. We have scheduled office hours throughout the week, with the exception of Friday, when the instructor and TAs will be working on grading homework assignments. Please feel free to ask the TAs any questions you may have about your Java program.
Documentation. Illustrate your program with in-line comments, so the TAs know what you are doing. Consult your textbook and other Java programming references provided on the course Web page and elsewhere (e.g., Java books in library) for style guides.
Evaluation. Maximum score = 50 points, as follows (modified 5-19-99 to allow for partial completion):
Code Present & Clear 10 points max. In-line Comments & Documentation 5 points max. Code compiles correctly 10 points max. Data read in correctly 5 points max. Correct input count displayed 5 points max. Maximum computed correctly 5 points max. Minimum computed correctly 5 points max. Mean computed correctly 5 points max. ------------------------------------------------- TOTAL POINTS 50 points max.For example, if you have only half the code needed, and it doesn't compile and isn't documented, you would get half of the 10 points allocated for the code being present. If the code is present, well documented, compiles, reads in data, and displays the input count correctly (but doesn't do anything else), then you could get as many as 35 points.
Submission Procedure. (Inserted Wed 5-19-99) Please submit only your *.java files. You must be logged in to your CISE account to perform the submission procedure. This procedure is described in detail in this link and is summarized below.
Important Note: In case you have developed your programs on any environment other than the CISE Unix system, please test your program on a CISE Unix machine before submission. We will only be testing programs for grading purposes on the CISE Unix machines.
Type in the following commands within your directory to submit all the java files for Project-1:
turnin -c cop3530 -p proj1 *.java
{Return}
Note: The turnin command has been activated, as-of late Friday 5-21-99.
This concludes the description of Project #1. Use the E-mail link at the top of this Web page to ask the TAs, if you have any questions about programming or grading.