Computing Concepts with Java Essentials
Laboratory Notebook
Chapter 3 - Objects

Cay S. Horstmann
Geof Pawlicki

Your name:
Your email address:
Your student ID number:

Once this form has been customized for your institution, you can use this button to send your lab work. Be sure to read the instructions before starting your work.

Lab Objectives

To gain experience in

R1. Constructing Objects

Declaring an object to store information is similar to declaring a variable of numeric data type. For instance

int counter = 60; /* makes an int */
Time  meetingTime = new Time(1998, 4, 1, 12, 0, 0); /* makes a Time object */

Objects are always constructed with a call to new. You specify the type of the object, such as Time, followed by construction parameters such as (1998, 4, 1, 12, 0, 0). You usually store the constructed object in an object variable (here Time meetingTime).

To what values is a Time object set by Time new_time = Time()

Initialize a Time variable to your birthday.

What is the outcome of constructing a Time object with

Time nextMeeting = new Time(Thursday, 12, 12, 0, 0, 0);

What is the outcome of Time nextMeeting = "12/12/1999";

What is the outcome of Time nextMmeeting(1997, 10, 1, 10, 0, 0);

R2. Using Objects

An object's methods are applied to a particular instance of an object type using the dot-notation. For example, timeVar.getSeconds() returns the number of seconds stored in a particular Time object called timeVar. Methods that return stored information when applied to an object are called accessors, those that change the stored information are called mutators.

Given two instances of the Time class, the first day and time of classes in 1997 and 1998

static final float SECONDS_PER_DAY = 24 * 60 * 60.0;
. . .
t1 = new Time(1998,9,1,10,15,0)
t2 = new Time(1997,8,31,10,45,0)

What is the result of each of the following:

System.out.println(t1.getMonth() + " / " + t1.getDay());

t2.addSeconds(SECONDS_PER_DAY - 1800);


R3. Real Life Objects

A direct and natural comparison can be made between an object and the entity that it represents. For example, here is how you might construct and access objects of a PhoneNumber class:

PhoneNumber p = new PhoneNumber(areacode, prefix, number);

Street addresses are another common example. Give a constructor and corresponding accessors for addresses having the following form:

John Smithson
766 N. Park Ave.
New York NY 12345

Graphics Structures

P1. Lines

Write a program that uses three Line objects to draw a triangle, like this:


P2. Lines and Circles

Use Circle and Line objects to draw an automobile like this :

Line drawing Automobile

P3. Moving graphical objects

Generate five circles with center (0,0) and radius 1, 2, 3, 4, and 5. Use the Circle class's .move(dx, dy) member function to draw the circles all tangent at a common point, like this:

Line drawing tangent circles

Choosing a Coordinate System

P4. Comparing coordinate systems

Given a coordinate system where the upper lefthand corner is (0,0) and the lower righthand corner is (3,3), write a graphics program to draw a stopsign like this:

irregular stopsign

Change the program to display the same stopsign in a coordinate system with upper left = (-1.5, 1.5) and lower-right = (1.5, -1.5).

Which coordinate system was easier for you to work with? Why?

P5. Zooming out

Modify the program of the textbook to display the points in a zoomed-out coordinate system, that is, the curve should look as though it is being viewed from a greater distance, but still be centered, like this:

Zoomed-out view of phoenix.cpp

import ccj.*;

public class Phoenix extends GraphicsApplet
{  public void run()
   {  setCoord(1, 33, 12, 11);
      new Point(1, 11).draw();
      new Point(2, 13).draw();
      new Point(3, 16).draw();
      new Point(4, 20).draw();
      new Point(5, 25).draw();
      new Point(6, 31).draw();
      new Point(7, 33).draw();
      new Point(8, 32).draw();
      new Point(9, 29).draw();
      new Point(10, 23).draw();
      new Point(11, 16).draw();
      new Point(12, 12).draw();

P6. Getting Input from the Graphics Window

Write a program that 1) gets two mouseclick points from the user 2) draws a rectangle with the coordinates of the clicks as upper-left and lower-right corners, 3) prompts for the user's name and 4) draws the name inside the rectangle.

P7. Comparing Visual and Numerical Information

On a Cartesian coordinate plane with upper-left = (-10,10) and lower-right = (10, -10), write a program that 1) draws the coordinate axes, 2) gets and plots 2 user mouse clicks and their coordinates, 3) plots the line between them, and 4) calculates and plots the y-intercept of the line and its coordinates, like this:

Plot line on Cartesian plane

Verify the appearance of the Y-intercept by calculating its value by hand. Show your work here.

Don't forget to send your answers when you're finished.