Lab 2 Assignment

Point Distribution

This lab is worth 10 points and accounts for 12.5 percent of your laboratory grade. The points will be subjectively assigned by your TA based upon whether they believe you made a good faith effort toward complete the tasks that are assigned. Showing up on time, respecting your fellow classmates learning environment, and adherance to laboratory policies also factor into the determination of your score.

The following is an outline of the tasks for you to attempt during the lab period.

A framework containing the files and folders you will need can be found here. Download the .tgz file and decompress it in your account using the command:

tar -xzvf <file_name>
  

Programming Exercises

Multiple File Compilation

In the framework that you've downloaded you will find a folder named socket. This folder contains the three classes: Socket, ClientSocket, and ServerSocket and the main for two programs: simple_client, and simple_server. However, the makefile to compile everything is incomplete. Edit the makefile so that it directs the compilation correctly. Each compilation must use the options "-Wall -W -Werror". (ie. You must specify the command to g++ rather than relying on the make utility to generate an implicit rule. )
When it compiles correctly, open two terminals. Run the simple_server in one terminal, then run the simple_client in the other. In the terminal running the simple_client, type in some text, hit enter, and see what happens. What do you see in the terminal running the simple_server?

Rational Class

It may come as a bit of a surprise when the C++ floating-point types ( float, double ), fail to accurately capture a particular value. Certainly double, which is usually stored as a 64-bit value, is far better than the old float, which is only 32 bits, but problems do arise. For example:

float n = 2.0;
float d = 3.0;
cout.precision( 17 );
cout << n / d << endl;

Produces 0.66666668653488159, which is accurate to 8 decimal places. Not bad, but a bit dirty for a discipline that prides itself on precision.

A solution that is often used when precision is of greatest importance and all of the numbers involved are going to be rational ( that is, expressible as a fraction ) is to use a custom type that implements fractions / rational numbers. This is what you will do in this lab assignment.

The Specification

Write a C++ program that performs the rational number operations addition, subtraction, multiplication and division operations on two fractions. You will need to design a rational number class whose value will be a fraction (e.g. 1/128, or 22/7), with appropriate constructors, accessor and mutator functions. A fraction will be specified as a numerator and a denominator - e.g. the pair (8, 109) represents the fraction 8/109.

Your program must implement the rational number operations listed above, and test both the class and the operations thoroughly. You need to use constructors for:

Note that a fraction may have both a numerator and a denominator, for example: (8/3), or may simply be a number, for example: 6, in which case you will actually use (6/1) to represent it. Thus, the constructors with two and one arguments should be included in the class definition.

Question: what would be a useful default constructor - i.e. what would you consider to be the "null" Rational Number?

Your program will have a function for each one of the operations add, subtract, multiply and divide. For example: Suppose we have defined objects a and b and c as fractions, then to compute a + b, we will use:

c = a.add(b);

When you perform an operation, you do not have to simplify the result, i.e., 4/5 * 5/10 = 20/50.

The following are a list of rules that will be helpfull when designing the methods which operate on the rational class:

(a/b) + (c/d) = (a*d + b*c) / (b*d)
(a/b) - (c/d) = (a*d - b*c) / (b*d)
(a/b) * (c/d) = (a*c) / (b*d)
(a/b) / (c/d) = (a*d) / (c*b)

MyPoint ( Your own Point class )

Part 1

The Big C++ graphics package includes a point class. For this exercise you will implement a point class ( called MyPoint ) which has the same functionality as the point class in the Big C++ graphics package. You will find files in the folder mypoint that will get you started. You will also need to add the ability to compile an object file for the MyPoint class to the makefile.

Part 2

Once you've completed Part 1, write a program which draws a rectangle on the screen. However, you are not allowed to use the Point class directly. Instead you must use the MyPoint class that you have written. Add a rule to your makefile to compile the program.
Hint: The getCccPoint() member of the MyPoint class will be quite useful.

MyLine, MyCircle, ...

If you finish the first three tasks, pick another class from the Big C++ graphics package and write your own class which has the same functionality; and show that it works by writing a main which uses your class. You can find documentation for the interface of the Big C++ classes in the notes section for lab1. Don't forget to give your class a getCccXXXXX() method so that you can display it in the graphic window.