Chapter 2: Data and Expressions 20
Lab Grades
Suppose your lab instructor has a somewhat complicated method of determining your grade on a lab. Each lab consists of two
out-of-class activities—a pre-lab assignment and a post-lab assignment—plus the in-class activities. The in-class work is
60% of the lab grade and the out-of-class work is 40% of the lab grade. Each component of the grade is based on a different
number of points (and this varies from lab to lab)—for example, the pre-lab may be graded on a basis of 20 points (so a
The program LabGrade.java is supposed to compute the lab grade for a student. To do this it gets as input the number of
points the student earned on the prelab assignment and the maximum number of points the student could have earned; the
number of points earned on the lab itself and the maximum number of points; the number of points earned on the postlab
1. First carefully hand trace the program assuming the input stream contains the values 17, 20, 23, 25, 12, 15. Trace the
program exactly as it is written (it is not correct but it will compile and run so the computer would not know it isn’t
correct). Fill in the answers to the following questions:
a. Show exactly how the computer would execute the assignment statement that computes the out of class average for
this set of input. Show how the expression will be evaluated (the order in which the operations are performed) and
what the result will be.
b. Show how the computer would execute the assignment statement that computes the in-class average. What will the
result be?
2. Now run the program, typing in the input you used in your trace. Compare your answers to the output. Clearly the output
is incorrect! Correct the program. This involves writing the expressions to do calculations correctly. The correct answers
3. Modify the program to make the weights for the two components of the grade variable rather than the constants 0.4 and
0.6. To do this, you need to do four things:
a. Change the declarations so the weights (IN_WEIGHT and OUT WEIGHT) are variables rather than constants. Note
that you should also change their names from all capital letters (the convention for constants) to lowercase letters
b. In the input section, add statements that will prompt the user for the weight (in decimal form—for example .4 for
c. In the section that calculates the labGrade add an assignment statement that calculates the weight to be assigned to
// ***************************************************************
// LabGrade.java
// This program computes a student’s lab grade from