| CS 261 | University of Puget Sound | Spring, 2020 |
|
Computer Science II
|
||
|
Abstract Data Types and their Implementations,
Some Basic Algorithms,
Object-oriented Problem Solving, and Efficiency |
||
Warning:
This course is under development.
Although the basic structure of this course is largely established,
nothing on this Web site should be considered official or even possibly
correct.
DO NOT MAKE PLANS BASED ON THE CONTENTS OF THIS SITE UNTIL JANUARY, 2020.
This laboratory provides practice in reading data from the keyboard and from text files.
Before working on this lab, be sure you have read the reading on stream input in Java.
Parts of this lab are edited versions of materials written by Samuel Rebelsky and revised by Jerod Weinman.
Create a testScores package within Eclipse, and import the programs TestScanner1.java, TestScanner2.java, TestScanner3.java, TestBufferedReader1.java, and TestBufferedReader2.java.
Also store a copy of test.data to your account. Then modify the file name in the above programs to reflect your new file name.
Run TestScanner1.java (using your file name), and be sure you can explain how this program works.
Now try these experiments with TestScanner1.java.
What happens to program execution if you misspell the file or path name?
Delete the try and catch details, so the various steps of reading are completed without a try..catch block. Describe what happens.
Instead of the try..catch syntax, adjust the main method by adding the phrase, throws Exception after the signature. Check that the program now runs successfully. (The point here is that either the possible exception will be handled within main in a try..catch block, or main will send processing outside (in this case to the operating system) if an exception occurs.
Edit your test.data file so that it contains only two test scores instead of three. What happens when you run TestScanner1.java? Briefly explain your answer.
Edit your test.data file so that it contains four test scores instead of three. What happens when you run TestScanner1.java? Briefly explain your answer.
Repeat Step 2 with program TestBufferedReader1.java.
Today's reading discusses reading input from the keyboard in two ways, using Java's Scanner class and using BufferedReader with InputStreamReader.
Write a Java program Greet.java that prompts the user for a name, reads a name from the keyboard, and prints a greeting to the screen. Your output should resemble the following:
What is your name? Terry Have a nice day Terry!
In reading a line (using readLine) with a BufferedReader, the operation operation may fail, and the BufferedReader may throw an exception. Thus, either you will need to place the readLine statement within a try..catch block, or you will need to add throws Exception to the declaration of the main method.
After you have written a program, you can run it in Eclipse in the usual way. To enter input from the keyboard, type in the "Console" window. Your input will appear in green.
Write a Java program Add.java that reads two [real] numbers from the keyboard and computes (and prints) their sum. (For simplicity, your program may assume that you will enter each number on a separate line.)
As with Step 1, do this work in two ways, first with the Scanner class and then with a BufferedReader with InputStreamReader.
Modify the previous step, so that the program continues to add your entered numbers until you enter a 0. Once zero is entered, the program should show the total of your numbers.
Create a class file called QuadraticRoot.java.
QuadraticRoot.java should contain a method
public static double smallQuadraticRoot(double a, double b, double c)
that computes the smaller of the two roots of a quadratic.
Note that you can use the following formula to compute that root:
(-b - sqrt(b2 - 4ac))/2a
You will, of course, have to translate that mathematical expression into Java code.
For this problem, you should assume that a is not zero.
Write a main method that reads three real numbers from the keyboard, calls smallerQuadraticRoot to determine the root, and prints the result.
Modify smallerQuadraticRoot, so that it tests whether coefficient a is zero. If so, the method should throw an exception.
Extend smallerQuadraticRoot to indicate that it may throw an exception. Note that you'll need to change the method head for smallerQuadraticRoot to something like the following
public static double smallerQuadraticRoot(double a, double b, double c)
throws Exception
if (a == 0)
{
throw new Exception("Cannot compute quadratic roots of linear functions.");
}
try
{
double root = f.smallerQuadraticRoot(a,b,c);
System.out.println("The smaller root of the polynomial is: " + root);
}
catch (Exception e)
{
System.err.println("Sorry, I could not compute a root.");
System.err.println("Error reported: " + e);
}
Then extend the catch clause in main by adding your new ArithmeticException before the catch clause for the generic Exception. For example,
try
{
...
}
catch (ArithmeticException arithE)
{
System.err.println("Cannot compute a result because the coefficient of the quadratic term is 0.");
}
catch (Exception e)
{
...
}
File Grinnell-rainfall.dat contains daily rainfall totals for each day over several years. For example, the start of the current file begins:
Year Day Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec 2016 1 0.00 0.00 0.06 0.00 0.09 0.00 0.00 0.00 0.01 0.00 0.00 0.00 2016 2 0.11 0.00 0.00 0.15 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 2016 3 0.01 0.00 0.00 0.84 0.00 0.00 0.00 0.53 0.00 0.23 0.01 0.00 2016 4 0.00 0.00 0.00 0.01 0.00 0.00 0.00 0.00 0.00 0.12 0.01 0.10 2016 5 0.00 0.00 0.00 0.59 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 2016 6 0.00 0.00 0.42 0.00 0.00 0.00 0.00 0.00 0.00 1.02 0.00 0.00 2016 7 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.23 0.00 0.00 2016 8 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 2016 9 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 2016 10 0.46 0.00 0.00 0.05 1.52 0.00 0.04 0.00 0.00 0.85 0.00 0.00 2016 11 0.00 0.00 0.00 0.00 0.00 0.00 0.89 0.00 0.00 0.01 0.05 0.00 2016 12 0.00 0.00 0.00 0.00 0.00 0.00 0.52 0.00 0.00 0.01 0.19 0.00 2016 13 0.00 0.00 0.00 0.02 0.00 0.00 0.00 0.00 0.00 0.00 0.01 0.05 2016 14 0.00 0.00 0.00 0.16 0.00 0.14 0.00 2.47 0.00 1.12 0.03 0.00 2016 15 0.00 0.00 0.00 0.56 0.00 0.00 0.00 0.05 0.00 0.00 0.01 0.00 2016 16 0.47 0.00 0.00 0.00 0.00 0.31 0.00 0.05 0.70 0.00 0.00 0.00 2016 17 0.00 0.00 0.00 0.00 0.41 1.42 0.00 0.00 0.60 0.00 0.02 0.00 2016 18 0.00 0.00 0.00 0.01 0.00 0.00 0.48 0.00 0.21 0.00 0.15 0.00 2016 19 0.32 0.00 0.00 0.00 0.96 0.23 0.00 0.01 0.01 0.00 0.00 0.00 2016 20 0.04 0.34 0.00 0.00 0.22 0.00 1.15 0.21 0.00 0.00 0.00 0.00 2016 21 0.11 0.00 0.00 0.00 0.01 0.48 0.06 1.27 0.00 0.98 0.00 0.00 2016 22 0.01 0.00 0.00 0.00 0.00 0.09 0.00 0.05 0.00 0.27 0.00 0.00 2016 23 0.00 0.08 0.00 0.00 0.08 0.01 0.00 0.00 0.00 0.01 0.00 0.00 2016 24 0.10 0.15 0.33 0.00 0.00 0.00 0.00 0.03 0.00 0.00 0.00 0.00 2016 25 0.08 0.00 0.49 0.00 0.01 0.00 0.00 0.08 0.00 0.00 0.00 0.00 2016 26 0.00 0.00 0.14 0.30 0.20 0.00 0.00 0.00 0.58 0.00 0.00 0.00 2016 27 0.00 0.00 0.01 0.00 0.17 0.00 0.00 0.07 0.00 0.00 0.00 0.00 2016 28 0.00 0.00 0.00 0.28 0.00 0.25 0.00 0.01 0.00 0.00 0.00 0.00 2016 29 0.00 --- 1.06 0.51 0.00 0.00 0.00 0.01 0.00 0.09 0.00 0.00 2016 30 0.00 --- 0.03 0.77 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 2016 31 0.00 --- 0.00 --- 0.00 --- 0.00 0.00 --- 0.00 --- 0.00
In this file, data are given for some number of full years, largely copied from www.grinnellweather.com. As this printout indicates,
The particular file contains rainfall for 2016, 2017, and 2018, but the file might be updated at any time to include more or fewer years.
Write a Java program that reads this file, determines the total rainfall for a year, and finds the two dates on which the largest rainfall occurred. (If more than two dates have the same maximum rainfall amount, the program may print any two of these dates. If one date has the maximum rainfail, and several dates are tied for second highest, then the program should print the date with the highest rainfall and any of the dates with the second highest.
If file, Grinnell-rainfall.dat, does not exist, is empty, or contains only the 1-line header, main processing should throw an exception that is caught to print that no rainfall data are available.
Reading data from a file usually is quite slow relatively to main-memory storage, so the program should make only one pass through the file data—no need to read the data several times.
Since only a maximum and second highest value must be determined, the program need not retain rainfall values beyond the maximum, second highest, and most recently-read rainfall amount. For example, it would be quite inefficient and wasteful of space to read all of the data into a 2-dimensional array to store rainfall amounts—no arrays of 2 dimensions or higher are allowed.
Similarly, a 1-dimensional array might be used to store month names (e.g., January, February, etc.), but 1-dimensional arrays of size more than 12 are not needed or allowed for this program. Of course, placing all rainfall data data into one or more strings is similarly prohibited.
More generally, only a small amount of data storage may be used for this program no more than 15-20 variables, and the amount of storage should not depend upon the number of years stored in the data file.
|
created 16 September 2018 last revised 17 September 2018 last revised 19-20 January 2020 |
|
| For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |