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.

Stream Input in Java

Summary

This laboratory provides practice in reading data from the keyboard and from text files.

Background Reading

Before working on this lab, be sure you have read the reading on stream input in Java.

Acknowledgment

Parts of this lab are edited versions of materials written by Samuel Rebelsky and revised by Jerod Weinman.

Reading Input from a File

  1. 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.

  2. Run TestScanner1.java (using your file name), and be sure you can explain how this program works.

    Now try these experiments with TestScanner1.java.

    1. What happens to program execution if you misspell the file or path name?

    2. Delete the try and catch details, so the various steps of reading are completed without a try..catch block. Describe what happens.

    3. 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.

    4. 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.

    5. 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.

  3. Repeat Step 2 with program TestBufferedReader1.java.

Reading Text from the Keyboard

  1. 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!
    
    1. Use Java's Scanner class to read this input.
    2. Use BufferedReader with InputStreamReader to read this input.

      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.

      1. Place the readLine within a try..catch block when performing this input.
      2. Specify that main may throw an exception (so no try..catch block will be needed for readLine).

    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.

  2. 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.

  3. 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.

The Quadratic Formula

  1. Create a class file called QuadraticRoot.java.

    1. 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.

    2. Write a main method that reads three real numbers from the keyboard, calls smallerQuadraticRoot to determine the root, and prints the result.

  2. Modify smallerQuadraticRoot, so that it tests whether coefficient a is zero. If so, the method should throw an exception.

    1. 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
      
    2. At the beginning of smallerQuadraticRoot, add a test, so that it throws an exception if a is 0. For example,
           if (a == 0)
           {
             throw new Exception("Cannot compute quadratic roots of linear functions.");
           }
      
    3. Extend smallerQuadraticRoot so that it throws an exception if the root is not real (i.e., if it has an imaginary component). Note that the root is not real if the thing you're taking a square root of is negative.
    4. Modify the main method so that the call to smallerQuadraticRoot is contained in a try..catch block. The program should print an appropriate error message (within main), if the user input yields a non-computable result. For example, part of your main program might include the following:
      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);
      }
      
    5. Modify the smallerQuadraticRoot method further, so that it throws one type of exception for imaginary roots and another (perhaps a ArithmeticException) if a is zero.

      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)
          {
            ...
          }
      

Working with File Input

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.

  1. 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.

Programing Notes


created 16 September 2018
last revised 17 September 2018
last revised 19-20 January 2020
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.