CSCI 201 Homework Assignment 4

time due submission file
8:00 AM 15 November, 2004 csci/201/HW4/fever.java

Problems with the filename

Although you can use csci/201/HW4 to store your NetBeans projects, many students are having problems copying their Java program to the correct file, in this case, csci/201/HW4/fever.java, when they do so.

To avoid this problem, we suggest that you do not store your NetBeans project within your csci/201/HW4 directory. Instead put it in something like csci/201/fever and copy the Java source file from where NetBeans stores it (csci/201/fever/src/fever.java) to the filename specified for this assignment.

Also, don't use a Java package when developing your code.

A serious problem in NetBeans

Unfortunately, there is a serious problem in reading input from System.in in NetBeans 4.0 Beta 1 and Beta 2. It just doesn't work. Due to some obscure interaction between NetBeans and Ant (a separate program that controls the building and running of Java programs), System.in always appears to have no data when run under NetBeans.

You could build your program under NetBeans and run it from the terminal window, but we're going to suggest a different solution. We're going to give you about twenty lines of Java code that will test if System.in is working and, if it is not, open a "test" file from which your program can read input.

Place this Java code at the beggining of your application's main method. After the code is executed, your program should read input from a Scanner object referenced by the stdin variable.

When you include this code, you must modify the line that defines testFile.

        // This section of code is written for use in CSCI 201 to avoid
        // problems in reading System.in from the terminal in NetBeans 4.0
        //
        // The test file will be used if System.in isn't working.
        // In Windows the \'s in test file name must be doubled!
        // Here are some example definitions for testFile.
        //
        // String testFile = "/home/carvergw/csci/201/HW4.txt" ;
        // String testFile = "C:\\Documents and Settings" +
        //                    "\\Carver\\My Documents\\HW4.txt" ;
        String testFile = "PUT THE TEST FILE NAME HERE" ;
        //
        java.util.Scanner stdin = new java.util.Scanner(System.in) ;
        if (!stdin.hasNext()) {
            // Looks like you are using NetBeans 4.0
            try {
                stdin = new java.util.Scanner(new java.io.File(testFile)) ;
            } catch (java.io.FileNotFoundException fnfe) {
                System.err.println("Unable to open " + testFile) ;
                System.exit(1) ;
            }
            System.out.println("Reading input from " + testFile) ;
            System.out.println() ;
        }
        // End of section of code to handle System.in problem

The task

Write a Java application called fever that reads lines of input from the Scanner class stdin, set as shown in the code given in the previous section.

Each lines of input specifies a temperature. Temperature may be given in either the Fahrenheit or Centigrade scale. The specific scale is indicated by putting the letters F or C after the temperature. These letter must be separated from the temperature by at least one space to make Java happy. Here is an example of a legal set of input readings:

 98.6 F
 37 C
 13 F
 35 C
 104 F

Your program should, if necessary, convert the temperature reading into the Fahrenheit scale. (In case you've forgotten, the formula for the conversion is 1.8*Centegrade+32.)

For each input temperature reading, your program should produce a single line of output. Your program should first check the temperature and verify that it is between 90 F and 110 F. If it isn't, print the message "Faulty reading. Try again.".

If the temperature is between 90 F and 110 F, your program should print a line that begins with the temperature reading, neatly formatted as a decimal number with a single digit after the decimal point. Next, print two blanks. Now, finish off the output line by printing "normal" if the temperature is less than 100 and by printing "fever" if the temperature is over 100.

Use the Scanner class to read from input. Use hasNext to test if there is more input, then nextDouble to read the temperature and next to read the letter as a Java String.

For the above input sequence, the output should be:

 98.6  normal
 98.6  normal
Faulty reading.  Try again.
 95.0  normal
104.0  fever

You must line up the readings neatly as shown in the above example. Use either the NumberFormat class or the printf method of System.out to do this.