CS 010 - Introduction to Computer Science I
Lab 3. Objects

Points(10 overall)
Collaboration policy:
Collaboration on this lab exercise is strongly ENCOURAGED. The exercise is intended for practice, not assessment -- most people who try should get all participation points. Feel free to ask for help from, and provide help to, others. You shouldn't blindly copy solutions from one another (or from anywhere else) nor simply write code for someone else (even if you explain it as you write), but you can certainly help each other debug, give plenty of suggestions and hints, **explain** why things work or don't work, etc. If you get done early, feel free to walk around and help others.

Lab Objectives

To gain experience in


Review Exercises   Programs   Reference Tables   Special Instructions  

Review 1. 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. It is suggested you initialize counter when declaring it like this, 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 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 (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 Time next_date = Time (18, 0, 0);


Review 2. 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()

tend.add_seconds(1200);

tend.seconds_from(tstart);


Review 3. 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);
a.get_number();
a.get_street_name();
a.get_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
096-33-2123
December 30, 1949


Grachics Objects

Instructions for using the graphics objects in lab:

To use the graphics objects provided by the author of our textbook, we will need to do a few things differently from what you have learned.


Graphic Objects and their member functions:

Below is a reference to all the objects provided in the ccc_win library and their member functions.

Point

The Point is simply a ( x, y ) pair representing a coordinate in the window.

Functions of the Point Class
Name Purpose
Point( x, y ) Constructs a point at location ( x, y )
p.get_x() Returns the x-coordinate of point p
p.get_y() Returns the y-coordinate of point p
p.move( dx, dy ) Moves point p by the displacement ( dx, dy )

If you wanted to create a point in the center of the default drawing area and then move it 3 units to the left, your code might look something like the following:

    Point my_point( 0, 0 );
    my_point.move( -3, 0 );
    

Circle

A Circle is defined by a Point and a radius.

Functions of the Circle Class
Name Purpose
Circle( p, r ) Construct a circle with center p and radius r
c.get_center() Returns the center point of circle c
c.get_radius() Returns the radius of circle c
c.move( dx, dy ) Moves circle c by the displacement ( dx, dy )

Line

A line simply joins 2 end Points. The Line object differentiates between the starting Point, p, and the ending Point, q.

Functions of the Line Class
Name Purpose
Line( p, q ) Constructs a line joining points p and q
l.get_start() Returns the starting point of line l
l.get_end() Returns the ending point of line l
l.move( dx, dy ) Moves line l by the displacement ( dx, dy )

Message

Message objects are used to display text and numbers in the graphics window. You can create a Message object using either a string or a numeric type such as int or double.

Functions of the Message Class
Name Purpose
Message( p, s ) Constructs a message with starting point p and text string s
Message( p, x ) Constructs a message with starting point p and a label equal to the number x
m.get_start() Returns the starting point of message m
m.get_text() Gets the text string of message m
m.move( dx, dy ) Move the message m by the displacement ( dx, dy )


Input/Output to Graphics Window

To display objects in a graphics window, use the following functions of the GraphicWindow class. The GraphicWindow object, w, in this table will be cwin in your programs.

Functions of the GraphicWindow Class
Name Purpose
w.coord( x1, y1, x2, y2 ) Sets the coordinate system for subsequent drawing: ( x1, y1 ) is the top left corner, ( x2, y2 ) is the bottom right corner
w << x Displays the object x ( a Point, Circle, Line, or Message ) in window w
w.clear() Clears window w ( erases its contents )
w.get_string( p ) Displays prompt p in window w and returns the entered string
w.get_int( p ) Displays prompt p in window w and returns the entered integer
w.get_double( p ) Displays prompt p in window w and returns the entered floating-point value
w.get_mouse( p ) Displays prompt p in window w and returns the mouse click point


Graphics Structures

Program 1. Line

(click here to view Point member functions)
(click here to view Line member functions)

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


Program 2. Lines and Circles

(click here to view Point member functions)
(click here to view Line member functions)
(click here to view Circle member functions)

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


Program 3. Moving graphical objects

(click here to view Point member functions)
(click here to view Circle member functions)

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

Program 4. Comparing coordinate systems

(click here to view GraphicWindow functions)

Write a program that changes the default coordinate system to a coordinate system where the upper lefthand corner is (0,0) and the lower righthand corner is (3,3). Then 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?



Program 5. Zooming out

(click here to view GraphicWindow functions)

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;
}



Program 6. Getting Input from the Graphics Window

(click here to view GraphicWindow functions)

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.