Code:

``````class statistic{
double * store;
int noOfData;
public:
statistic(double* s,int sz);
statistic(const statistic &);
~statistic();
double average() const;
int size() const;
const double* data() const;
};

statistic::statistic (double * s , int sz):noOfData(sz){
store=new double[noOfData];
for (int i=0;i<noOfData;i++) store[i]=s[i];
}``````

Definition and test code of the class..

Can somebody solve this problem? It's my last lesson for the degree. I'm from Greece. Can you help me??? Please??

I know how to do the definition.. but I don't know the test code..

Edited by Nick Evan: n/a

2
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by Fbody

Test code should use every part of the object. You should have data that produces a predictable result, then check the generated results against what you expected them to be. If they match, you know it handles that set of data correctly and you have an increased level of confidence that it will handle other data sets correctly.

If

... It's my last lesson for the degree. ...

is actually true, I would have expected you to know this.

Edited by Fbody: n/a

Yes it's true. I don't want to be a c++ programmer, I just want to teach children the basics.
If you could just solve it, it would be great. But if you don't want to, then it's fine.

If I solve it, what do YOU learn? Nothing. You can't possibly expect to provide quality instruction in the basics if you don't know the basics yourself.

You'll have to start by creating a main() and instantiating objects of your statistic class. Then, use those objects to call any and all member functions. You will then have to compare the produced results against a solution you already know is correct.

For example (excerpted from a project I did a couple months ago):
Die.h

``````// constant definitions
const int DEFAULT_FACECOUNT = 6;

class die {
private:
//static int dieCount;	//declare a static instance counter DEPRECATED DATA MEMBER
int myValue;	//the number rolled
int myFaceCount;//represents the shape of the die
int myId;		//holds an individual die's ID number
bool isRollable;//indicates if client code can use roll() method

protected:

public:
//---------- constructor(s) ----------
die(int newFaceCount = DEFAULT_FACECOUNT, int newId = 0)
: myFaceCount(newFaceCount)
, isRollable(true)
, myId(newId) { this->roll(); }
//pre: newFaceCount is a positive integer w/ a default value
//pre: newId is a positive integer w/ default value 0
//post: create an n-sided die with face values [1, n]
//		and an ID# of newId

//---------- mutator(s) ----------
void roll() {
if (isRollable)
myValue = (rand() % (myFaceCount))+1;
}
//post: select the active die face if die is allwoed to roll
void keep() { isRollable = false; }
//post: isRollable is set to false,
//		die is not allowed to complete the roll method
void pickup() { isRollable = true; }
//post:	isRollable is set to true,
//		die is allowed to complete the roll method
void setFaceCount(int newFaceCount) {
myFaceCount = newFaceCount;
isRollable = true;
this->roll();
}	//post: die "shape" is changed to the requested number of faces
void setId(int newId) { myId = newId; }
//post: sets the ID# of the die

//---------- accessor(s) ----------
int getDieId() const { return myId; }
//post: returns the die's ID#
int getFaceValue() const { return myValue; }
//post:	returns the value of the current active face
int getFaceCount() const { return myFaceCount; }
//post:	returns the die's current number of faces
bool canRoll() const { return isRollable; }
//post:	returns the current status of isRollable

//---------- destructor(s) ----------
~die() {}
};``````

main.cpp

``````#include <iostream>
#include <iomanip>
#include <ctime>
#include <new>
#include "die.h"

using namespace std;

int main() {
//seed the random number generator
srand((unsigned)time(0));

//----- test the die class -----
//declare the pointer
die *dieTester = NULL;

cout << "Creating a default die..." << endl;
try {
dieTester = new die;
cout << "Unable to allocate a die." << endl;
return EXIT_FAILURE;
}

cout << "This die has " << dieTester ->getFaceCount() << " faces." << endl;

//declare a distribution vector to check results distribution
vector<int> sampleDist6(dieTester->getFaceCount()+1, 0);

cout << "Rolling die 100 times..." << endl;
for (int i = 0; i < 100; i++) {
dieTester->roll();
sampleDist6[dieTester->getFaceValue()]++;
cout << dieTester->getFaceValue();
if ((i % 10) == 9)
cout << endl;
else
cout << " ";
}
cout << endl << "Distribution for 100 rolls: " << endl;
cout << setw(15) << "Face value:";
for (unsigned int i = 1; i < sampleDist6.capacity(); i++){
cout << setw(4) << i << "s";
}
cout << endl << setw(15) << "Times Rolled:";
for (unsigned int i = 1; i < sampleDist6.capacity(); i++){
cout << setw(5) << sampleDist6[i];
}
cout << endl;

//check keep and pickup methods
cout << "Keeping die..." << endl;
dieTester->keep();
cout << "Die " << (dieTester->canRoll() ? "is rollable." : "is NOT rollable.") << endl;
cout << "Picking up die..." << endl;
dieTester->pickup();
cout << "Die now " << (dieTester->canRoll() ? "is rollable." : "is NOT rollable.") << endl;

//check setFaceCount
cout << "Re-sizing die to 8 faces..." << endl;
dieTester->setFaceCount(8);
cout << "Resetting distribution..." << endl;
vector<int> sampleDist8(dieTester->getFaceCount()+1, 0);

cout << "Rolling die 100 times..." << endl;
for (int i = 0; i < 100; i++) {
dieTester->roll();
sampleDist8[dieTester->getFaceValue()]++;
cout << dieTester->getFaceValue();
if ((i % 10) == 9)
cout << endl;
else
cout << " ";
}
cout << endl << "Distribution for 100 rolls: " << endl;
cout << setw(15) << "Face value:";
for (unsigned int i = 1; i < sampleDist8.capacity(); i++){
cout << setw(4) << i << "s";
}
cout << endl << setw(15) << "Times Rolled:";
for (unsigned int i = 1; i < sampleDist8.capacity(); i++){
cout << setw(5) << sampleDist8[i];
}
cout << endl;

delete dieTester;
dieTester = NULL;
return 0;
}``````

Edited by Fbody: n/a

That's the test code?

``````//----- test the die class -----
//declare the pointer
die *dieTester = NULL;

cout << "Creating a default die..." << endl;
try {
dieTester = new die;
cout << "Unable to allocate a die." << endl;
return EXIT_FAILURE;
}

cout << "This die has " << dieTester ->getFaceCount() << " faces." << endl;``````

Children of the primary school.. saying basics, I mean word, excel.. those things

That's the test code?

``````//----- test the die class -----
//declare the pointer
die *dieTester = NULL;

cout << "Creating a default die..." << endl;
try {
dieTester = new die;
cout << "Unable to allocate a die." << endl;
return EXIT_FAILURE;
}

cout << "This die has " << dieTester ->getFaceCount() << " faces." << endl;``````

No, all of it is. That's just where it starts. Anything that interacts with "dieTester" is testing a specific part of the die object/class.

``````//declare the pointer
die *dieTester = NULL;

cout << "Creating a default die..." << endl;
try {
dieTester = new die;
cout << "Unable to allocate a die." << endl;
return EXIT_FAILURE;
}

cout << "This die has " << dieTester ->getFaceCount() << " faces." << endl;

//declare a distribution vector to check results distribution
vector<int> sampleDist6(dieTester->getFaceCount()+1, 0);

cout << "Rolling die 100 times..." << endl;
for (int i = 0; i < 100; i++) {
dieTester->roll();
sampleDist6[dieTester->getFaceValue()]++;
cout << dieTester->getFaceValue();
if ((i % 10) == 9)
cout << endl;
else
cout << " ";
}
cout << endl << "Distribution for 100 rolls: " << endl;
cout << setw(15) << "Face value:";
for (unsigned int i = 1; i < sampleDist6.capacity(); i++){
cout << setw(4) << i << "s";
}
cout << endl << setw(15) << "Times Rolled:";
for (unsigned int i = 1; i < sampleDist6.capacity(); i++){
cout << setw(5) << sampleDist6[i];
}
cout << endl;

//check keep and pickup methods
cout << "Keeping die..." << endl;
dieTester->keep();
cout << "Die " << (dieTester->canRoll() ? "is rollable." : "is NOT rollable.") << endl;
cout << "Picking up die..." << endl;
dieTester->pickup();
cout << "Die now " << (dieTester->canRoll() ? "is rollable." : "is NOT rollable.") << endl;

//check setFaceCount
cout << "Re-sizing die to 8 faces..." << endl;
dieTester->setFaceCount(8);
cout << "Resetting distribution..." << endl;
vector<int> sampleDist8(dieTester->getFaceCount()+1, 0);

cout << "Rolling die 100 times..." << endl;
for (int i = 0; i < 100; i++) {
dieTester->roll();
sampleDist8[dieTester->getFaceValue()]++;
cout << dieTester->getFaceValue();
if ((i % 10) == 9)
cout << endl;
else
cout << " ";
}
cout << endl << "Distribution for 100 rolls: " << endl;
cout << setw(15) << "Face value:";
for (unsigned int i = 1; i < sampleDist8.capacity(); i++){
cout << setw(4) << i << "s";
}
cout << endl << setw(15) << "Times Rolled:";
for (unsigned int i = 1; i < sampleDist8.capacity(); i++){
cout << setw(5) << sampleDist8[i];
}
cout << endl;

delete dieTester;
dieTester = NULL;
return 0;``````

The try-catch block is just an error-handling mechanism, it makes sure that I have a valid die object to work with before I start testing parts of it. If I don't it ends the program. In a way, it tests the constructor of the die class.

Edited by Fbody: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.