Chris Pollett >
Old Classes >
PIC10B Lec 1&2

   ( Print View )

Lecture Notes-PDF.

Enrollment info.

Course Info: Homework Assignments:
Practice Exams:
PIC:
                                












PIC 10b Fall 2000 HW#1

Homework FAQ.

Solution Set.

Due date: Oct. 6. Lecture 1's due time is: noon 
=========         Lecture 2's due time is: 5:30pm.
        
Files to be submitted: pic10bhw1file1.cpp
======================

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
             with writing classes and methods for classes.
             It is also supposed to help you learn about information
             hiding and give you experience with writing 
             a very simple abstract data type.

Specification:
==============

You must implement the methods for the following two classes:

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

The file you submit should contain any #include statements you use, 
the two classes above plus the methods you wrote for them. 
It should also contain the following int main() function
which will act as a 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;

}



Here is what your code should output if it runs correctly:

PairType1 Test
==============
PairType1 p1 has values:
x := 0
y := 0
PairType1 p2 has values:
x := 1
y := 2
Copying p2 to p1
The x Value of p1 is:1
The y Value of p1 is:2

PairType2 Test
==============
PairType2 q1 has values:
x := 0
y := 0
PairType2 q2 has values:
x := 1
y := 2
Copying q2 to q1
The x Value of q1 is:1
The y Value of q1 is:2



Point Breakdown for HW1
=======================
If the submit program didn't collect your file
because you misnamed it or you did not follow
the required formatting instructions in the syllabus
you will get a 0 without an opportunity for a regrade.

How well your code is commented........1pt.
PairType1's constructors both work.....1pt.
PairType1's copy(), output() work......1pt.
PairType1's copy(), output() work......1pt.


PairType2's constructors both work.....1pt.
PairType2's getX() works...............1pt.
PairType2's getY() works...............1pt.
PairType2's copy(), output() work......1pt.
===========================================
Total..................................8pts