Logo of the Augustana Faculty of the University of Alberta

COMPUTING SCIENCE 120 — Abstraction, Design, and Object-Oriented Programming


Lab Assignment 5



Due Date: Thursday, March 9

Note that the due date for this lab has been postponed one week from the original due date as announced in the course outline.

It is intended that you complete this lab assignment during the lab session on Thursday, March 2. You may choose to complete these exercises with a pair programming partner, but if you were working with a different partner on Lab 4, be sure to finish the Lab 4 exercises with the same partner you had last week before starting this lab. When you have finished, demonstrate your programs to the instructor or the lab assistant to receive credit for this lab.

If you do not finish during the March 2 lab session, your work may be checked off at the start of the March 9 lab session instead.

Objectives

Assignment

The following programming exercises are to be completed on your own or with a different programming partner from the person you worked with on the previous lab (if possible). Remember to trade roles every 5 to 10 minutes. When your team finishes a programming exercise, you will both receive credit for that exercise.

  1. Currency conversion: Do Programming Exercise P7.1 (page 270) but revise the problem so that it reads Canadian dollar values and converts them to U.S. dollars. Note that today's exchange rate is entered only once, then a sequence of Canadian dollar values is entered, and each is converted to U.S. dollars right after being entered.

    As an example of how your program should work, assume that the current exchange rate is 0.8796 (the "US/Canada noon rate" for 2006 March 1 from the Bank of Canada Rates and Statistics page). Then $10.00 Canadian would be equivalent to $8.80 U.S., and $25.00 Canadian would be $21.99 U.S.

    Design your program using two files:

    1. a Converter class that has
      1. a constructor that accepts an exchange rate as a parameter, and
      2. a convert method; and
    2. a driver program that handles input and output (I/O) with the user.

    In the driver program, use console input with a sentinel value ('Q' or 'q') and the "loop-and-a-half" pattern as illustrated on page 251. Note: Don't type in the '$' sign when entering values.

    Hint: In order to round the results you calculate to the nearest cent, it would probably be easiest to convert the dollar amounts entered by the user into cents, then calculate the U.S. equivalent (still in cents), and round the result to the nearest cent using Math.round(). Convert the result back to dollars just before printing the result. Remember that Math.round() accepts a double argument and returns a long result, so you should store the user's input value as a double. If you want to store the rounded result in an int variable, you will have to cast the result with (int). Alternatively, you could use System.out.printf() with a format string such as "%.2f", as illustrated in Advanced Topic 4.6 on page 137, which will round a double value to the specified precision at the time of "printing" the output.

  2. Mean and standard deviation: Do Programming Exercise P7.6 (page 272). Note that the exercise specifies that the input loop quites when the "end-of-file" (EOF) is reached. We will generalize that requirement somewhat by specifying that any input that cannot be scanned as a real number will be treated as a sentinel indicating end-of-file. [Hint: Use hasNextDouble() from the Scanner class. Note: In the Windows environment, the user can enter the Ctrl-Z key combination to signal the end of input (EOF).]

    Write this program as two Java files: a driver file that reads the input values and prints the results, and a modification of the DataSet class from Chapter 7 (pages 252–253) that includes a getStandardDeviation() method. [Hint: Calculate the sum of the squares of the data values entered as well as the sum and count of the values.]

    Note that this program does not print any results until after all the input values have been entered.

    To test your program, try entering all the integers between -5 and 5 (including 0). The mean should be 0 and the standard deviation should be about 3.32. (Note that your program should accept real values and store them in a double variable, but we are entering integers in order to test a simple case.)

    As another test, try entering the values 6.2, 3.14, 1.414, -5.8, and 2.7. The mean should be 1.5308 and the sample standard deviation should be 4.4585.

  3. Polygon: Do Programming Exercise P8.8 (page 316).

Grading

Each program in this set of lab exercises will be graded on a "mastery" basis - you will get full credit for the program if it works correctly, or no points if it is not completed. The programs of this lab will be worth 4 points each, for a total of 12 points for this lab assignment.

Copyright © 2006 Jonathan Mohr