It is intended that you complete this lab assignment during the lab session on Thursday, January 26. When you have finished one or more exercises, demonstrate your programs to the instructor or the lab assistant to receive credit for this lab.
If you do not finish during the Jan. 26 lab session, you may be checked off at the start of the Feb. 2 lab session instead.
The following programming exercises are to be completed with a different programming partner from the person you worked with last week. Remember to trade roles every 5 to 10 minutes. When your team finishes a programming exercise, you will both receive credit for that exercise.
You may wish to create a new folder named "lab2" (or something similar) inside your "csc120" folder in order to keep your solutions for this lab separate from those from last week's lab.
The first three exercises below use the following two Java programs as a starting point. Copy the following two files into your "lab2" folder, either by copying them from your web browser and pasting them into two new files using TextPad or by right-clicking on the links below and choosing the "Save" option.
RectangleComponent.java (from page 156 of the textbook)
RectangleViewer.java (from page 157 of the textbook)
After you have saved these two files, compile and run the RectangleViewer class. The result should look like Figure 2 on page 154 of the textbook.
NOTE: Because the RectangleViewer class uses the RectangleComponent class, the latter class will be automatically compiled as well. Note that you cannot execute the RectangleComponent class, because it does not have a main method.
Rectangle Grid: Modify the RectangleComponent class so that, instead of drawing two intersecting rectangles, it draws four rectangles in a 2 x 2 grid, as illustrated in the figure accompanying Exercise P2.3 on page 60.
NOTE: You do not have to understand all the code in the RectangleViewer and RectangleComponent classes. It will be explained when we discuss Chapter 5 of the textbook.
Copy, Grow and Translate: Modify the RectangleComponent class so that it draws two rectangles by using the copy constructor of the Rectangle class along with the methods grow and translate.
The public interface of the Rectangle copy constructor is:
public Rectangle(Rectangle r)
The copy constructor constructs a new Rectangle object, initialized to match the values of the specified Rectangle.
The translate method is discussed on pages 46–47 of the textbook. For more information on the grow method, consult the J2SE 5.0 API Specification.
NOTE: Instead of replacing the code that you created in the RectangleComponent class for the previous exercise, comment out the code that you don't need and write the new code that you need for this exercise below the commented-out code. This will allow you to reconstruct your solution to the previous exercise without renaming the RectangleComponent class.
Intersecting Rectangles: Modify the RectangleComponent class so that it constructs two rectangle objects, draws them, then computes the intersection of the two rectangles and draws it. This exercise is similar to Programming Exercise P2.4 on page 60, but we are using a Graphics context to draw the rectangles instead of printing a textual representation of them. Your intersecting rectangles should look like the figure accompanying that exercise. You can use your solution to the previous exercise to create the first two intersecting rectangles.
The intersection of rectangles r1 and r2 is computed as follows:
Rectangle r3 = r1.intersection(r2);
In order that we can see rectangle r3, draw it using a fill color. We have to look ahead to Chapter 5 to learn how to do this (see page 172), but for now you just have to add the following two lines after you have created rectange r3 and before you have drawn it:
g2.setColor(Color.PINK); g2.fill(r3);
You will also have to add the following import statement at the top of the RectangleComponent file:
import java.awt.Color;
If you don't like pink, you can choose another color — see Table 1 on page 164 for a list of predefined color names.
Random Numbers: Do Programming Exercise P2.7 (page 60).
Employee Class: Do Programming Exercises P3.4 and P3.5 (page 99). Have your test class (EmployeeTester) create at least one employee, print the employee's name and salary, give the employee a 10% raise, then print the employee's new salary.
Student Class: Do Programming Exercise P3.7 (page 99). Also supply a small program that tests your Student class by creating a student, calling the addQuiz method three or four times, then prints the student's name, total score and average score. Modify your test program by adding one more call to addQuiz — did your implementation of the Student class correctly count the number of quizzes in order to compute the correct average score?
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 first four programs will be worth 1 point each and the last two, 2 points each, for a total of 8 points for this lab assignment.
Copyright © 2006 Jonathan Mohr