Cay Horstmann & Timothy Budd

Laboratory Notebook
Chapter 3 - Objects

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 like declaring a variable of numeric data type. For instance:

int counter; /* makes an int */
Time  lunch_time; /* makes a Time object */

While both of these statements declare a new variable, there is a difference in how each is initialized. When declaring counter, the value will be whatever is leftover from that memory location's last use. Quality tip 2.x suggests setting int counter = 0; to avoid this problem.

Objects are initialized when they are defined, based upon the object type. Time objects are initialized to the current time. If you do not want to initialize an object with the default, you supply construction parameters. For example, you can construct a Time object by specifying hour, minute and second, for example Time (11,20,0)

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

Initialize a Time variable to the time you usually get up.

What is the outcome of constructing a Time object with

Time next_date(Saturday, 18, 0, 0);

What is the outcome of Time next_date = 6:00pm?

What is the difference between Time next_date(18, 0, 0) and next_date = Time (18, 0, 0);

R2. Using Objects

An object's member functions are applied to a particular instance of an object type using the dot-notation. For example, time_var.get_seconds() returns the number of seconds stored in a particular Time object called time_var. Member functions 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 beginning and end of a typical workday:

tstart = Time(9,0,0)
tend = Time(17,0,0)

What is the result of each of the following:

cout << tstart.get_hours() << " / " << tstart.get_minutes()



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 an Address class:

Address a(number, street_name, street_type);

Personal information about an individual would be another common example. Give a constructor and corresponding accessors for personal information having the following form:

Serdni Vasnar
December 30, 1949

Graphics Structures

P1. Lines

Write a program that uses five Line objects to draw a pentagon, like this:

P2. Lines and Circles

Use Circle and Line objects to draw a train engine like this :

P3. Moving graphical objects

Generate five circles with center (0,0) and radius 1, 2, 3, 4, and 5. Use the move method of the Circle class 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 = (-2.5, 1.5) and lower-right = (1.5, -3.5).

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

P5. Zooming out

Modify the phoenix.cpp 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

#include "ccc_win.cpp"

int main()
{  cwin.coord(1, 33, 12, 11) // only change this line!!!
   cwin << Point(1, 11);
   cwin << Point(2, 12);   
   cwin << Point(3, 16);   
   cwin << Point(4, 20);   
   cwin << Point(5, 25);   
   cwin << Point(6, 31);   
   cwin << Point(7, 33);   
   cwin << Point(8, 32);   
   cwin << Point(9, 29);   
   cwin << Point(10, 23);   
   cwin << Point(11, 16);   
   cwin << Point(12, 12);
   return 0;

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 their 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.
It should look like this:

Plot line on Cartesian plane

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

Do not forget to send your answers when you are finished.