Lab 6

In homework 5, you wrote an application that asked the user for some information about an investment and then calculated and displayed the future value and the total interest earned. In that application, you simply assumed that the user would enter valid input. This type of application is an example of the "garbage in, garbage out" style of programming. If the user enters invalid data (garbage in), the output generated by the program will also be invalid (garbage out). Here is a sample run of that application that illustrates the garbage in, garbage out style:

Garbage in, garbage out

In homework 6, you modified the Investment class to guarantee that an Investment object is always in a valid state. That is, the Investment class will ignore requests to set any of its attributes (present value, nominal rate, years, or compounding fequency) to invalid values. While this protects the integrity of an Investment object, it wouldn't help much in our application. If our sample run above used the modified Investment class, the resulting Investment object would be valid but the output would still be useless (garbage):

Garbage in, garbage out

Data Validation

The application from homework 5 needs to be modified to check for invalid input from the user. In particular, your application should check each value entered by the user, immediately after it has been entered. That is, immediately following the input statement, you need to write a branching statement that checks the value entered by the user and, if the value is not valid, your application should display an error message and terminate the program.

We have used the standard output stream (System.out) to display text in a Java program. However, Java also provides a separate output stream just for displaying error messages. System.err and System.out are both PrintStream objects. Consequently, they have the same methods and work in exactly the same way. It is up to the programmer to decide whether error messages should be sent to System.out or System.err. In this assignment, you will send error messages to System.err and all normal output will go to System.out as before. In the BlueJ console, text sent to System.err will appear in red in a separate window below the normal console window (see the first two sample runs below).

In all of our previous applications, the application terminates when the last statement in the application has been executed. This application may terminate early if the user enters an invalid value. To terminate an application, execute the exit method in the System class:

System.exit(int status);

The integer parameter to the exit method is a status code. By common convention, the status code is 0 if a program terminates normally and is non-zero if it terminates abnormally. In this application, you can just set the status code to zero.

 Now, let's put all the pieces together. For each of the four investment attributes, you will implement the following logic:

get the value from the user
if (value is not valid)
    System.err.println(error message);

Three sample runs are shown below. In the first two, the user entered an inappropriate value for one of the investment attributes. In the last sample run, no input errors were made.

Invalid present value

Invalid Compounding Frequency

No Invalid Data

Submitting Your Work

Send both your Lab06.java application and your Investment.java class to me as attachments to an email message whose subject is "Lab06".