Skip to content

Instantly share code, notes, and snippets.

@dillmo
Created May 13, 2014 23:40
Show Gist options
  • Save dillmo/6e89c35d5e54970363d8 to your computer and use it in GitHub Desktop.
Save dillmo/6e89c35d5e54970363d8 to your computer and use it in GitHub Desktop.
Introduction to C++ Problem Set 6
/* Introduction to C++ Project 2 by Dillon Morse
* =============================================
*
* Description
* -----------
* Spec out how you will code and test the definition and use of a right
* triangle class.
* 1. Determine what your data will be.
* 2. Create prototypes of the following functions:
* - Constructor(s)
* - Area
* - Perimeter
* - Set/get sides
* - Do you have a right triangle?
* - Are the sides in range.
* - Create a right triangle given two non-hypot sides.
* 3. Come up with a plan to test your class functionality.
*
* Test plan
* ---------
* Testing can be done by a suite of unit tests that each create a new instance
* of the class in or called by main. The test suite will be seeded with
* time(0) when it is called. Then, random data that fits certain parameters
* will be passed to the current object and verified against an expected
* output. There will also be a control expectation that uses non-random data.
* Each expectation/assertion will test for whether the class returns the
* proper result and handles exceptions properly.
*/
#include<cmath>
class RTriangle {
public:
double leg1;
double leg2;
double hypotenuse;
double Area();
double Perimeter();
void SetHypotenuse(double);
bool TestRight();
bool TestRange(double, double);
double Hypotenuse();
RTriangle(double, double);
RTriangle();
};
RTriangle::RTriangle(double x, double y) {
leg1 = x;
leg2 = y;
hypotenuse = -1;
}
RTriangle::RTriangle() {
leg1 = 3;
leg2 = 4;
hypotenuse = 5;
}
double RTriangle::Area() {
return leg1 * leg2;
}
double RTriangle::Perimeter() {
return leg1 + leg2 + Hypotenuse();
}
void RTriangle::SetHypotenuse(double input) {
hypotenuse = input;
}
bool RTriangle::TestRight() {
if ( sqrt( pow( leg1, 2.0 ) + pow( leg2, 2.0 ) ) == Hypotenuse() ) {
return true;
}
}
bool RTriangle::TestRange(double min, double max) {
if ( !( ( leg1 > min ) && ( leg1 < max ) ) ) {
return false;
}
if ( !( ( leg2 > min ) && ( leg2 < max ) ) ) {
return false;
}
if ( !( ( hypotenuse > min ) && ( hypotenuse < max ) ) ) {
return false;
}
return true;
}
double RTriangle::Hypotenuse() {
if ( hypotenuse != -1 ) {
return hypotenuse;
}
return sqrt( pow( leg1, 2.0 ) + pow( leg2, 2.0 ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment