Chris Pollett >
Old Classes >
PIC10B Lec 1&2

   ( Print View )

Lecture Notes-PDF.

Enrollment info.

Course Info: Homework Assignments:
Practice Exams:
PIC:
                                












Fall 2000 PIC 10b HW1 Solutions Page

Return to homework page.

//
//HW1 Purpose: To implement two classes for ordered pairing
//============ which support the same functionality but
//             whose underlying implementation is different.
//             This homework is supposed to get you more familiar
//
// This file contains the interface and implementation of the two classes:
//
// PairType1 
// PairType2
//
// Both of which support methods for ordered pairing.
// 
// It also contains a driver program main.

#include<iostream.h>

// Class name: PairType1
//
// Purpose: Stores two integers x >= 0 and y >=0. Allows user to access these
//          integers using getX() and getY() functions.
//          copy(p) method can be used to copy the
//          contents of the current pair into p
//          output() method can be used to print these values
//          to screen.
//
// Remark: This class is supposed to store the two integers
//         internally using the private variables x and y.  
//
//
// Known Bugs: none 
class PairType1
{
        public:
           PairType1(); // Postcondition: a PairType1 object is
                        // created and the values 0, 0 are stored in it. 

           PairType1( int xVal, int yVal); 
                       // Precondition: xVal >=0 , yVal >= 0
                       // Postcondtion: a PairType1 object is created
                       // and the values xVal, yVal are stored in it. 

           int getX(); //Postcondition: x value is returned.
 
           int getY(); //Postcondition: y value is returned.

           void copy(PairType1 &p); //Precondition: p is an initialized
                                    //   PairType1 object
                                    //Postcondition: p stores same
                                    //   x and y values as the PairType1 object 
                                    //   whose copy method is being invoked.
           

           void output(); //Postcondition: stored x and y values
                          // printed to screen 

        private:
           int x; //where x value stored
           int y; //where y value stored
};

// Class name: PairType2
//
// Purpose: Stores two integers x >= 0 and y >= 0. Allows user to access these
//          integers using getX() and getY() functions.
//          copy(p) method can be used to copy the
//          contents of the current pair into p
//          output() method can be used to print these values
//          to screen.
//
// Remark: This class is supposed to store the two integers
//         internally using the single private variable 
//         pair=((x+y)*(x+y+1)/2)+x.
//         Let r >=0 be the largest integer value so that r*(r+1)/2=< pair
//         Then x = pair - r*(r+1)/2, y = r - x.     
//
//
// Known Bugs: none 

class PairType2
{
        public:
           PairType2(); // Postcondition: a PairType2 object is
                        // created and the values 0, 0 are stored in it.

           PairType2( int xVal, int yVal); 
                       // Precondition: xVal >=0 , yVal >= 0
                       // Postcondtion: a PairType2 object is created
                       // and the values xVal, yVal are stored in it. 

           int getX(); //Postcondition: x value is returned. 

           int getY(); //Postcondition: y value is returned.

           void copy(PairType2 &p); //Precondition: p is an initialized
                                    //   PairType2 object
                                    //Postcondition: p stores same
                                    //   x and y values as the PairType2 object 
                                    //   whose copy method is being invoked.
           

           void output(); //Postcondition: stored x and y values
                          // printed to screen 


        private:
           int findBase(); //Postcondition: returns the largest values r >=0
                           //   such that r*(r+1)/2 =< pair

           int pair; // stores (x+y)*(x+y+1)/2+x.
};

//
//
//            MEMBER FUNCTION IMPLENTATIONS
// 
//
//

//
// First for PairType1
//

// Method name: PairType1()
//
// Purpose: default constructor sets order pair to be (0,0)          
//
// Known Bugs: none

PairType1::PairType1()
{
	x=0;
	y=0;
}

// Method name: PairType1(int , int)
//
// Purpose: Constructor. Takes two arguments xVal, yVal and makes
//          an object for the ordered pair (xVal, yVal).          
//
// Known Bugs: none

PairType1::PairType1(int xVal, int yVal)
{
	x=xVal;
	y=yVal;
}

// Method name: getX
//
// Purpose: returns the first coordinate of the ordered pair stored in object 
//
// Known Bugs: none

int PairType1::getX()
{
	return x;
}

// Method name: getY
//
// Purpose:  returns the second coordinate of the ordered pair stored in object
//
// Known Bugs: none

int PairType1::getY()
{
	return y;
}

// Method name: copy(PairType1 &p)
//
// Purpose: copies the pair stored in current object into p.          
//
// Known Bugs: none

void PairType1::copy(PairType1 &p)
{
	p = PairType1(getX(),getY());
}

// Method name: output
//
// Purpose: prints out ordered pair to cout          
//
// Known Bugs: none

void PairType1::output()
{
 	cout << "x := " << getX() << endl;
	cout << "y := " << getY() << endl;
}


//
//
// Implementations for PairType2
//
//


// Method name: PairType2()
//
// Purpose: default constructor sets order pair to be (0,0)          
//
// Known Bugs: none

PairType2::PairType2()
{
	pair =0;
}


// Method name: PairType2(int , int)
//
// Purpose: Constructor. Takes two arguments xVal, yVal and makes
//          an object for the ordered pair (xVal, yVal). 
//          This is stored in  one int using equation
//          pair= (xVal+yVal)*(xVal + yVal +1)/2 +x  
//
// Known Bugs: none

PairType2::PairType2(int xVal, int yVal)
{
	int tmp = xVal+yVal; //tmp var used to avoid repeating this addition

	pair = tmp*(tmp+1)/2+xVal;
}


// Method name: findBase()
//
// Purpose: returns the largest integer r>0 such that
//          r*(r+1)/2 < pair
//
// Comment: code would work faster if used quadratic formula.
//          This method used as an auxiliary method for getX(), getY()
//
// Known Bugs: none

int PairType2::findBase()
{
	int r=1 ;

	while ( r*(r+1) < 2*pair)
	{
	  r++;
	}

	return r-1;
}


// Method name: getX()
//
// Purpose: returns the first coordinate of the ordered pair stored in object 
//
// Known Bugs: none

int PairType2::getX()
{
	int r;
	r=findBase();

	return pair - (r*(r+1))/2;

}

// Method name: getY()
//
// Purpose: returns the second coordinate of the ordered pair stored in object 
//
// Known Bugs: none

int PairType2::getY()
{
	return findBase()-getX();
}


// Method name: copy(PairType2 &p)
//
// Purpose: copies the pair stored in current object into p.          
//
// Known Bugs: none

void PairType2::copy(PairType2 &p)
{
	p= PairType2(getX(),getY());
}

// Method name: output()
//
// Purpose: prints out ordered pair to cout          
//
// Known Bugs: none

void PairType2::output()
{
 	cout << "x := " << getX() << endl;
	cout << "y := " << getY() << endl;
}

//
//      
//           DRIVER PROGRAM
//
//

// Function name: main()
//
// Purpose: driver program to test our two pairing classes          
//
// Known Bugs: none

int main()
{
        PairType1 p1 , p2(1,2); //tests two constructors for PairType1
        PairType2 q1 , q2(1,2); //tests two constructors for PairType2

        cout << endl;

        //
        // Begin test of PairType1
        //

        cout << "PairType1 Test" << endl;
        cout << "==============" << endl;

        cout << "PairType1 p1 has values:" << endl;

        p1.output(); //tests output() method and 
                     //default constructor

        cout << "PairType1 p2 has values:" << endl;

        p2.output(); //tests output() method and 
                     //two input constructor


        cout << "Copying p2 to p1" << endl;

        p2.copy(p1); //tests copy() method


        //Finally ... test PairType1's getX() and getY() methods
 
        cout << "The x Value of p1 is:" << p1.getX() << endl;
        cout << "The y Value of p1 is:" << p1.getY() << endl;
        cout << endl;

        //
        // Begin test of PairType2
        //

        cout << "PairType2 Test" << endl;
        cout << "==============" << endl;

        cout << "PairType2 q1 has values:" << endl;

        q1.output(); //tests output() method and 
                     //default constructor

        cout << "PairType2 q2 has values:" << endl;

        q2.output(); //tests output() method and 
                     //two input constructor


        cout << "Copying q2 to q1" << endl;

        q2.copy(q1); //tests copy() method

        //Finally ... test PairType2's getX() and getY() methods

        cout << "The x Value of q1 is:" << q1.getX() << endl;
        cout << "The y Value of q1 is:" << q1.getY() << endl;

        return 0;

}