ExamGrader)double.
WeightedAverager)Weighted averages are typically expressed by specifying the percentage that each element contributes to the average. The percentages must sum to 100% for the average to be correct. To calculate the average, each element is multiplied by it's percentage (don't forget, 50% is actually .50), and the products then summed producing the average.
(Another way of calculating the weighted average is to treat each exam as counting for its percentage points out of 100 and then dividing the total by
100 (this works if you are restricting the calculations to integers and thus can't use something like .50). For example if an exam is worth
65%, you multiply the score by 65; f the other one is then 35%, you multiply it by 35, and after you've added those amount together, you divide by 100.)
Write a program that prompts the user for a project, midterm and final and displays their weighted average (as a floating point,
i.e., double) according to the following table:
| Project | 20% |
| Midterm | 30% |
| Final | 50% |
(Notice this is very similar yo the previous lab, but with weighted averages, and the prompts and output are different … but it still makes sense
to copy Lab 2.1 for your starting point. Starting from a similar program is often simpler than starting from scratch and a useful skill to master.)
Sample Test Run
Here is a sample execution of the program. User input is in bold. Your program should replicate the prompts and output:
WeightedAverager) Approval
Modify Lab 2.2 so that the user is prompted for the weights of the exams (see sample run).
Sample Test Run
Here is a sample execution of the program. User input is in bold. Your program should replicate the prompts and output:
Casting) Approval
Floating point values — double (and float) have two parts to them: the integer portion and the fractional portion. For example, 15.25
has an integer portion of 15 and a fractional portion of 25.
You are to write an app that prompts the user for how many values to process; reads in that many doubles, and prints it out together with its integer
and fractional portions. When you are done, print out the number of values processed.
Sample Test Run How many doubles? 5 Enter a double: 3.5 double: 3.5 integer portion: 3 decimal portion: 0.5 Enter a double: 7.0 double: 7.0 integer portion: 7 decimal portion: 0.0 Enter a double: 10.25 double: 10.25 integer portion: 10 decimal portion: 0.25 Enter a double: 0.234 double: 0.234 integer portion: 0 decimal portion: 0.234 Enter a double: 6.0 double: 6.0 integer portion: 6 decimal portion: 0.0 5 numbers processed
double is to chop off the fractional portion … we can do that by converting the double to an integer
(double) (conversion operator) in class; it converts an int value into a double. The (int
casting operator does the reverse: it converts a double into and int.
e double cast in case to force floating point division. However, we don't need it for assigning an integer to a double; i.e.,
double d = 15;or
int i = 15; double d = i;are perfectly legal since no information is lost.
int i = 15.6;is illegal because of the possible (and in this example definite) loss of information.
int cast:
double d = 15.6; int i = (int)d;and similarly when assigning a
double to an int.