Computing Concepts with C++ Essentials, 2nd ed.
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 like declaring a variable of numeric data type. For instance

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

Both declare a new variable. Apart from the differences in type, there's a big difference in how each is initialized. Unless explicitly initialized, counter will contain whatever value is leftover from the memory's last use. Remember too that quality Tip 2.1 suggests setting int counter= 0; in order to avoid complications due to the leftover data. However, objects are initialized when they are defined. The acutal initialization depends on the object type. Time objects are initialized to the current time.

If you don't 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 (12,30,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_meeting(Thursday, 12, 0, 0);

What is the outcome of Time next_meeting = 12:30pm?

What is the difference between Time next_meeting(12, 30, 0) and next_meeting = Time (12, 30, 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:

t1 = Time(9,0,0)
t2 = Time(17,0,0)

What is the result of each of the following:

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

t2.add_seconds(1800);

t2.seconds_from(t1);


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(areacode, prefix, number);
p.get_areacode();
p.get_prefix();
p.get_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:

Triangle

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 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 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.