Names:


CS 120 Lab 05

Tom Linton, http://www.cs.moravian.edu/~linton
Moravian College, Spring 1999

Introduction

You may work in pairs on this assignment if you wish.

Many problems require a mixture of data types. Consider, for example, discounting the sticker price of a car by a fixed percentage. Car prices are high enough that we typically truncate to the nearest dollar, so int variables are appropriate for storing sticker prices. However, percentages are best described with real numbers (12% is 0.12), or type double variables for our course. If a car has a sticker price of 22165 dollars and is to have a 15 percent discount applied to it, the natural calculation would be

newPrice = 22165 - 0.15*22165.
Java's order of operations will perform the multiplication (0.15*22165) first, and since one of the operands is a double, the result of this multiplication will be a double. The subtraction is therefore another mixed expression which will result in a double as well, but we want newPrice to be an int. Declaring newPrice as an int (and then issuing the command above) won't work (it is illegal to assign a double value to an integer variable). Not only that, but it is tricky to figure out just when you want to "convert things to integers". This conversion is known as casting, and casts take precedence (are done before) over the standard operations of multiplication, division, remainder, addition and subtraction. Roughly, you cast a value using the syntax
(cast_type) value
so, (int) 7.9 gives 7 (the fractional part is dropped, not rounded) and (int) 7.9 + 2.2 gives 9.2 since this is equivalent to
((int) 7.9) + 2.2
(the cast is done before the addition, so an int is added to a double and the result is a double). You can override Java's order of operations by using grouping parenthesis, thus, (int) (0.12*12) is the same as (int) 1.44 which is 1.
  1. Create a file called CarPrices.java which defines a class CarPrices. Your program should have just a main method. In main, create an instance of a SimpleGUI object (so you'll have to import simpleIO.* at the top of your file) and then use the displayResult method of the SimpleGUI class to display each of the following attempts at calculating the new price of the car above. Your display should "echo back" the expressions below, so the first example should produce output something like:
In Java, 22165 - 0.15*22165 = 18840
    After each expression, add parenthesis to indicate clearly how Java computed the expression and give the value which Java produces from the expression. Using grade, submit your final version of CarPrices.java.

    1. (int) 22165 - 0.15*22165 


    2. 22165 - (int) 0.15*22165 


    3. (int) (22165 - 0.15*22165) 


    4. 22165 - (int) (0.15*22165)

  1. The modulus or remainder operator, %, gives the integer remainder after division of the operand on its left by the operand on its right. For example, 17 / 6 = 2 (integer division) with a remainder of 5 (17 = 2*6 + 5), so 17 % 6 = 5. The remainder operator is extremely useful in computer science and here is an example of its usage. Suppose you had to make change, using only nickels and pennies for N cents (N is an int). You would use N / 5 nickels and N % 5 pennies. Clean and simple, many times the remainder is just as important as the quotient! The precedence of % is equivalent to that of multiplication and division, so 4 + 5 % 3 - 7 = (4 + (5 % 3) ) - 7 = -1, and 2 * 8 % 6 * 3 = ((2*8)%6) * 3 = 12. Create a MakeChange class which prompts the user for an integer valued "number of cents" input between 0 and 100 (use the 3 argument getInt method, i.e. getInt("String", lowerbound, upperbound) to ensure users enter values between 0 and 100 inclusive) and displays a meaningful result which designates how many quarters, dimes, nickels and pennies one would use to make change for their input amount. Use as many quarters as possible, then as many dimes as you can, and so. For an input of 62, your response should be something like (you don't need to put in a line break though):
62 cents can be made with 2 quarters,
1 dimes, 0 nickels and 2 pennies.
Using grade, submit your MakeChange.java file.
  1. In a computer science course, students took a 100 point exam, had a 100 point programming assignment and a 100 point oral presentation. These activities are weighted 60% (exam), 25% (programming) and 15% (presentation) to calculate a "class percentage". If a student scores 82, 75 and 95 respectively, their class percentage would be
(0.6*82) + (0.25*75) + (0.15*95) = 49 + 18 + 14 = 81
Design a class called ClassScore which prompts the user for their exam score, programming assignment score and their presentation score, and then produces a meaningful display of their class percentage. Submit ClassScore.java using the grade script.