Site Navigation

Site Search

CS 110: Lab 7

Learning Objectives

After completion of this lab, you should be able to

Work collaboratively as a pair programming team

All labs in CS 110 will be done as pair programming teams. Your partner for today's lab is listed in the table below:

Hebeler 203
Grader: Vinh Tran; Assistant: Daniel Carpenter
Team 1
Aguilar Jr, Jaime
Hastings, Jake
Team 2
Amezcua Gutierrez, Edson
Hogan, Martin
Team 3
Baird, Owen
Ling, Nathan
Team 4
Berman, Jake
Nash, Chris
Team 5
Coudriet, Blake
Olivares, Scott
Team 6
Erickson, Joel
Shearer, Stetson
Team 7
Goeke, Max
Wagster, Nathan
Team 8
Harris, Alexander
Smith, Stephanie
Team 9
Heflick, Liz
Awan, Samara
Team 10
McCauley, Rylee
Bloom, Thomas
Team 11
Millard, Ryan
Olden, Greg
Team 12
Barrett Wright, Matthew
Cuddington, Chris
Team 13
Millard, Mike
Straub-Walden, Andy
Team 14
Field, Sarah
Williamson, Sarah
Team 15
Akana, Chris
Quayle, Weston
Fill in: Hansen, Mitchell

Note: Partners will change every week.

You may wish to review basic pair programming guidelines before you begin.

You should change roles every 10 to 15 minutes.

Introduction

two boxes

In today's lab, you will write a Java class that represents a Rectangle that can be displayed on a computer screen

Use a UML class diagram to create a Java class

rectangle UML

The UML class diagram for the Rectangle class is shown at right.

  1. Create a new Java class named Rectangle and save as Rectangle.java in folder lab7
  2. Type in the Java code to implement the public Rectangle class shown in the UML diagram on the right
  3. Compile your class and remove all syntax errors

Change Pair Partner Roles

Change pair partner roles. The driver should become the navigator, the navigator should become the driver.

Test your class with a main method

Use a short main method in RectangleDemo.java to test your Rectangle class methods.

  1. Create a new Java class named RectangleDemo and save as RectangleDemo.java in folder lab7
  2. Add the main method shown below to your RectangleDemo class
  3. Compile the class and test your program
public class RectangleDemo
{
   public static void main(String[] args)
   {
   // Create a new Rectangle object
      Rectangle box1 = new Rectangle(50, 30, 100, 50);
   
   // Display the information about box1
      System.out.println("box 1 is at (" + box1.getXCoordinate() + "," +
                    box1.getYCoordinate() + ")");
      System.out.println("height = " + box1.getHeight());
      System.out.println("width = " + box1.getWidth());
      System.out.println("color = " + box1.getColor());
   
   // Change the location, size, and color of box1  
      box1.setXCoordinate(100);
      box1.setYCoordinate(200);
      box1.setColor("#FF0000");  // red
      box1.setHeight(20);
      box1.setWidth(15);
     
   // Display the new information about box1
      System.out.println();
      System.out.println("box 1 is at (" + box1.getXCoordinate() + "," +
                    box1.getYCoordinate() + ")");
      System.out.println("height = " + box1.getHeight());
      System.out.println("width = " + box1.getWidth());
      System.out.println("color = " + box1.getColor());
   }
}
  1. Run your program and verify that the console output is correct based on the code and values used above

Use your Rectangle class in a simple Java applet

Java applets are simple Java programs designed to run in a web browser.

  1. Download and save in your U:\labs\lab7 folder the following one Java file
    • RectangleApplet.java file
    • Note: you are not expected to be able to write applets, but see the textbook's Chapter 13 if you want to learn more.
  2. Compile the RectangleApplet class and run it by using the Build Menu > Run as Applet (or using the Run General toolbar button)
    • Note: you should not need to make any changes to your tested Rectangle class code

applet with blue box

Read and modify a program written by someone else

Java applets use the Graphics object g to draw rectangles at (x, y) with a specified width and height

g.fillRect(x, y, width, height);

The fill color of the rectangle may be specified using hex values (as a string)

g.setColor(Color.decode("#00FF00"));  // blue
  1. Modify RectangleApplet.Java to set and get new values for the box1 Rectangle object, to approximate the drawing shown below
    • Using the Rectangle class setter and getter methods to set and get the box's values, following the example shown in RectangleDemo class
    • You should not need to change the code inside your tested Rectangle class
    • Browse to hex values for popular web colors for colors

applet with three boxes

To Receive Credit

You are allowed to drop one lab grade for the entire quarter.