By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 938 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

help with classes

P: 8
hey guys, this is my assignment but i have no godly idea where to start. can anyone help?

the assignment:

A stock tank is a metal or plastic tank that ranchers and farmers use to provide water for their livestock. The tanks are usually made of metal or plastic, and come in three basic shapes; rectangular, “oval”, and circular. The rectangular and circular tanks are shaped as you expect, but the “oval” tank is actually a rectangle with ˝-circles on each end.

In this program, you are going to write a StockTankCalculator class that can be used to calculate the volume of a stock tank. Your calculator class will work with all 3 types of tanks. Given the shape of the tank, pertinent dimensions, the calculator can tell you how many gallons of water it will hold (completely full) as well as if given a percentage of the tank capacity will report the resultant number of gallons.


The class should look like this and have these methods. Do not cout << anything from within the class. The gets and sets should be named using “get” and “set” in the name. You should use either floats or doubles for capacity values.

class StockTankCalculator
{
private:
//what is all the data that the calculator needs?

public:
StockTankCalculator();

set function for shape of the tank
three set function for pertinent data for all three tank shapes
get function for total capacity of tank
get function, give a percentage value of total capacity, i.e. 50%, 75%, etc.

string getTankData() //returns a string that describes the type of tank and its dimensions

};



The StockTankCalculator has a default constructor that sets the tank to circular type, with diameter of 72” and depth of 24”.

Pertinent data that you’ll need or assumptions to use: 1) dimensions of the tanks are in whole inches, 2) there are 231 cubic inches per gallon of water, 3) total capacity of the tank has the water level at the very top.

Your main method should write a header to the screen, and then create one StockTankCalculator object. Then present the user with the choice so that he may calculate the various tank sizes and capacities. Write all capacities in floating point data to two decimal places of accuracy.

thanks guys!
Jun 16 '06 #1
Share this Question
Share on Google+
7 Replies


Banfa
Expert Mod 5K+
P: 8,916
Start by filling in the private data that you think the class will need, then proceed to implementing the function to set, manipulate and get this data.
Jun 16 '06 #2

P: 8
i meant help with coding......
Jun 17 '06 #3

Banfa
Expert Mod 5K+
P: 8,916
That is help with the coding, if you are looking for someone to do your assignment for you then you will have to find someone else. One the other hand if you want to attempt a solution yourself and then post it here then I will help you iron out any mistakes.

When posting code try to post compilable units that you have already compiled and where possible eliminated any compile errors and warnings, if you can't eliminate a particular error/warning because you do not understand what is causing it then post the code and the error message and the code line it is happening on.

So you should start by defining the class and you should start the class by defining the data that the class contains. Then you should define your function prototypes and then you should implement the functions.

You know the starting data because the assignment basically gives it to you, you need to store the shape and dimensions of the tank and you know what functions you need because that is also given by the assignment. So write you class definition and once you have completed that start on coding the methods.
Jun 17 '06 #4

P: 8
Heres what I have after working on it this weekend. I have a bunch of errors and i started commenting out stuff to try to get it to compile. I finally got it to compile but now i have three linking errors. The prob is GetTankData() but i am stuck. thanks


driver.cpp
[PHP]#include "stocktank.h"
#include <string>
#include <iostream>

using namespace std;

void WriteHeader();

int main()
{
int ans;

WriteHeader();

StockTank Calculator;
//Calculator.SetTankDataCircle(int diam=72, int d=24);

do
{

string info;


cout << "Stock Tank Calculator"<<endl;
cout << "Please select from one of the below options"<<endl<<endl;

cout << "1. Set Tank Shape "<<endl;
cout << "2. See Tank Information "<<endl;
cout << "3. Get Tank Capacity Percentage "<<endl;
cout << "4. Exit Program "<<endl<<endl;
cout << "Enter your selection 1-4 here: ";
cin >> ans;
cout << '\n';

switch(ans)

{
case 1:


cout << "Stock Tank Shape Screen " << endl;
cout << "Enter a tank type: " << endl;
cout << "1. Circle" << endl;
cout << "2. Rectangle" << endl;
cout << "3. Oval" << endl;
cout << "4. Return "<< endl;

int ansshape;

cin >> ansshape;


switch (ansshape)
{
case 1:

Calculator.SetTankShape(ansshape);
//Calculator.SetTankDataCircle();
//Calculator.GetTankCapacity();

case 2:
Calculator.SetTankShape(ansshape);
//Calculator.SetTankDataRectangle();
//Calculator.GetTankCapacity();

}



case 2:

cout.setf(ios::fixed);
cout.precision(2);

info=Calculator.GetTankData();

cout << info;

break;

case 3:

Calculator.GetTankPercent();


break;

case 4:

break;


default:

cout << "You must enter a valid response!" << endl<<endl<<endl;


}

}while(ans!=4);

return 0;
}

void WriteHeader()
{
cout<<endl;
cout<<" Joshua Hind"<<endl;
cout<<" CP 278B - C++ 2"<<endl;
cout<<endl;
cout<<" Welcome to the Program 3, The Stock Tank Calculator."<<endl;
cout<<endl;
}[/PHP]
stocktank.cpp
[PHP]
#include "stocktank.h"
#include <string>
#include <iostream>
#include <cmath>

using namespace std;

double pi=3.14;

StockTank::StockTankCalculator()
{
//hape="circular";
//diam=72;
//d=24;
GetTankCapacity();
GetTankData();



}

void StockTank::SetTankShape(int s)
{

if (s=1)
{
shape="circular";
//SetTankDataCircle();
}
else if (s=2)
{
shape="rectangular";
//SetTankDataRectangle();
}
else if (s=3)
{
shape="oval";
}


}


void StockTank::SetTankDataCircle()
{
int diameter;
int depth;
shape="circlular";
diameter=diam;
depth=d;
r=diam/2;


StockTank::GetTankCapacity();


}

void StockTank::SetTankDataCircle(int diam, int d)
{
int diameter;
int depth;
shape="circlular";
diameter=diam;
depth=d;
r=diam/2;


//StockTank::GetTankCapacity();


}
void StockTank::SetTankDataRectangle()
{
int length=0, width=0, height=0;

cout << "You selected a " << shape << " stock tank." << endl;

cout << "Please enter length of rectangle: " << endl;
cin >> length;
cout << "Please enter width of rectangle: " << endl;
cin >> width;
cout << "Please enter height of rectangle: " << endl;
cin >> height;

l=length;
w=width;
h=height;

//StockTank::GetTankCapacity();

}





float StockTank::GetTankCapacity()
{
int tanktype=0;

switch (tanktype)
{

case 1:
vol=pi*(pow(r,2))*d;
totcapgal=vol/231;
break;

case 2:
vol=l*w*h;
totcapgal=vol/231;
break;

default:
break;
}
return totcapgal;
}

float StockTank::GetTankPercent()
{
cout << "Enter a percentage in whole numbers (ex: 25 = 25%) ";
cin >> percap;

totcapper=(totcapgal*percap)*.01;

cout << "The capacity of the " << shape << " tank at " << percap << "% is " << totcapper << endl << endl;

return totcapper;

}

string GetTankData()
{
string data;

//data=shape + diam + d+ totcapgal;

//cout.setf(ios::fixed);
//cout.precision(2);
//cout << "The Stock Tank type is " << shape << endl;
//cout << "The tank dimensions are: " << diam <<"\" by " << d << "\"."<<endl;
//cout << "The Tank capacity (in gallons) of a "<< shape << " tank at 100 % "<<endl;
//cout << "with these dimensions is: "<<totcapgal<<endl<<endl<<endl;

return data;

}
[/PHP]
stocktank.h
[PHP]
#ifndef _STOCKTANK_H
#define _STOCKTANK_H

#include <string>

using namespace std;


class StockTank
{
private:
int l,w,h,d,r,diam,percap;
string shape;
float vol;
float totcapgal,totcapper;


public:
StockTankCalculator();
void SetTankShape(int s);
void SetTankDataCircle();
void SetTankDataCircle(int diam, int d);
void SetTankDataRectangle();
//void SetTankDataOval();
float GetTankCapacity();
float GetTankPercent();
string GetTankData();

};

#endif
[/PHP]
Jun 18 '06 #5

Banfa
Expert Mod 5K+
P: 8,916
Heres what I have after working on it this weekend. I have a bunch of errors and i started commenting out stuff to try to get it to compile. I finally got it to compile but now i have three linking errors. The prob is GetTankData() but i am stuck. thanks
Commenting out code to get rid of errors is almost always a mistake. In doing this instead of fixing the problem you have hidden it at the compile stage and just caused an error at the link stage.

The actual error is a typo, you have left StockTank:: out of the definition of GetTankData in stocktank.cpp.

When you have fixed this you will get a stack load of errors on the line

data=shape + diam + d + totcapgal;

You should note that string inplements the operator += but not the operator + and re-write this line.

The stocktank class.

You are using almost 3 times as many data members as I would. I would use height, width, depth and shape. Additionally I would use an enum for shape not a string because it is much easier to make decisions based on the binary (you can easily use switch and if) where as a string takes quite a lot of effort to make decisions on because it requires a string compare.

From these 4 variables (and for a circular tank I would not use depth I'd just use width to hold the diameter) everything else that is needed can be calculated as required.

You have failed to implement the default constructor required by your assignment, remember the default constructor will have the definition

[php]StockTank::StockTank()
{
...
}[/php]

In Stocktank.cpp my comments below in bold

Expand|Select|Wrap|Line Numbers
  1. #include "stocktank.h"
  2. #include <string>
  3. #include <iostream>
  4. #include <cmath>
  5.  
  6. using namespace std;
  7.  
  8. double pi=3.14;
  9. /* Why use a double if you are only going to use 3 places 
  10.    of precision.  Additionally this is a constant and only used 
  11.    in this file so declare it const static. */
  12. StockTank::StockTankCalculator()
  13. {
  14.     //hape="circular";
  15.     //diam=72;
  16.     //d=24;
  17.     GetTankCapacity();
  18.     GetTankData();
  19. /* This function looks like it needs work. */
  20.  
  21.  
  22.  
  23. }
  24.  
  25. void StockTank::SetTankShape(int s)
  26. /* It would be better to have this function accept the same 
  27.    type as shape, then all you have to do is verify it has a legal value
  28.    and copy it to shape rather than input a completely unrelated type
  29.    and interpret it. */
  30. {
  31.  
  32.     if (s=1)
  33. /* This is an assignment and returns the value 1, you will only 
  34.    ever be able to set the shape to circular.  Test for equality is == */
  35.     {
  36.         shape="circular";
  37.         //SetTankDataCircle();
  38.     }
  39.     else if (s=2)
  40.     {
  41.         shape="rectangular";
  42.         //SetTankDataRectangle();
  43.     }
  44.     else if (s=3)
  45.     {
  46.         shape="oval";
  47.     }
  48.  
  49.  
  50. }
  51.  
  52.  
  53. void StockTank::SetTankDataCircle()
  54. /* This function is outside the specification of the 
  55.    assignment and is not required */
  56. {
  57.     int diameter;
  58.     int depth;
  59.     shape="circlular";
  60.     diameter=diam;
  61.     depth=d;
  62.     r=diam/2;
  63.  
  64.  
  65.     StockTank::GetTankCapacity();
  66.  
  67.  
  68. }
  69.  
  70. void StockTank::SetTankDataCircle(int diam, int d)
  71. {
  72.     int diameter;
  73.     int depth;
  74. /* Diameter and depth are complement unused and unrequired */
  75.     shape="circlular";
  76. /* Interesting should you default the shape to circular to 
  77.    check to see if the shape is circular and if not return an error */
  78.     diameter=diam;
  79.     depth=d;
  80.     r=diam/2;
  81.  
  82.  
  83.     //StockTank::GetTankCapacity();
  84.  
  85.  
  86. }
  87. void StockTank::SetTankDataRectangle()
  88. /* It is good practice to separate you input and output and 
  89.    your data storage.  The StockTank class describes a water 
  90.    tank which has no capacity for text input and output.  I think
  91.    it would be better to call you cout/cin line externally to the
  92.    function and pass the length width and height to this
  93.    function as parameters. */
  94. {
  95.     int length=0, width=0, height=0;
  96.  
  97.     cout << "You selected a " << shape << " stock tank." << endl;
  98.  
  99.     cout << "Please enter length of rectangle: " << endl;
  100.     cin >> length;
  101.     cout << "Please enter width of rectangle: " << endl;
  102.     cin >> width;
  103.     cout << "Please enter height of rectangle: " << endl;
  104.     cin >> height;
  105.  
  106.     l=length;
  107.     w=width;
  108.     h=height;
  109.  
  110.     //StockTank::GetTankCapacity();
  111.  
  112. }
  113.  
  114.  
  115.  
  116.  
  117.  
  118. float StockTank::GetTankCapacity()
  119. {
  120.     int tanktype=0;
  121.  
  122.     switch (tanktype)
  123. /* You switch on a local variable which is initialised to 0.
  124.    You do not even have a 0 case so this is bound to do 
  125.    absolutely nothing.  You need to be referencing the 
  126.    variable shape. */
  127.     {
  128.  
  129.         case 1:
  130.             vol=pi*(pow(r,2))*d;
  131.             totcapgal=vol/231;
  132.             break;
  133.  
  134.         case 2:
  135.             vol=l*w*h;
  136.             totcapgal=vol/231;
  137.             break;
  138.  
  139.         default:
  140.         break;
  141.     }
  142.     return totcapgal;
  143. }
  144.  
  145. float StockTank::GetTankPercent()
  146. /* Again pass the required percentage as a parameter 
  147.    rather than using cout/cin within this class method. */
  148. {
  149.     cout << "Enter a percentage in whole numbers (ex: 25 = 25%) ";
  150.     cin >> percap;
  151.  
  152.     totcapper=(totcapgal*percap)*.01;
  153.  
  154.     cout << "The capacity of the " << shape << " tank at " << percap << "% is " << totcapper << endl << endl;
  155.  
  156.     return totcapper;
  157.  
  158. }
  159.  
  160. string StockTank::GetTankData()
  161. /* This function breaks the assignment as you are asked to 
  162.    write a function that returns a string, which this does but 
  163.    this also outs to cout which is not required. */
  164. {
  165.     string data;
  166.  
  167.     data=shape;
  168.     data+=diam;
  169.     data+=d;
  170.     data+=totcapgal;
  171.  
  172.     cout.setf(ios::fixed);
  173.     cout.precision(2);
  174.     cout << "The Stock Tank type is " << shape << endl;
  175.     cout << "The tank dimensions are: " << diam <<"\" by " << d << "\"."<<endl;
  176.     cout << "The Tank capacity (in gallons) of a "<< shape << " tank at 100 % "<<endl;
  177.     cout << "with these dimensions is: "<<totcapgal<<endl<<endl<<endl;
  178.  
  179.     return data;
  180.  
  181.  
driver.cpp - I am not going to say so much about this file, however you have seriously messed up your switch statements. Remember a case has to end with a break or the flow of control runs into the next case. Also have you consider what might happen if the cin statement does not receive the data it is expecting.

For instance for the line

cin >> ans;

have you consider what will have if the user enters a 'z'.

This might sound stupid but more than half the task of writing user interfaces is handling the dumb, stupid and sometime downright dangerous inputs the user trys to give in such a way that everything doesn't fall down around your ears.
Jun 19 '06 #6

P: 8
first, thanks for all your help.
here is my cpp file
[PHP]#include "stocktank.h"
#include <string>
#include <iostream>
#include <cmath>
#include <sstream>


using namespace std;

double pi=3.14;

StockTank::StockTankCalculator()
{
shape="circular";
diam=72;
d=24;
r=diam/2;
GetTankCapacity();
GetTankData();



}

void StockTank::SetTankShape(int s)
{

if (s==1)
{
shape="circular";
}
else if (s==2)
{
shape="rectangular";
}
else if (s==3)
{
shape="oval";
}


}


void StockTank::SetTankDataCircle()
{
cout<<"You selected a "<<shape<<" stock tank."<<endl;

int diameter;
int depth;
diam=0;
diameter=diam;
depth=d;
d=0;

cout<<"Enter the diameter of the circle. "<<endl;
cin>>diameter;
cout<<"Enter the depth of the tank."<<endl;
cin>>depth;

diam=diameter;
d=depth;


}

void StockTank::SetTankDataRectangle()
{
int length=0, width=0, height=0;



cout<<"You selected a "<<shape<<" stock tank."<<endl;

cout<<"Please enter length of rectangle: "<<endl;
cin>>length;
cout<<"Please enter width of rectangle: "<<endl;
cin >> width;
cout<<"Please enter height of rectangle: "<<endl;
cin>>height;

l=length;
w=width;
h=height;


}


void StockTank::SetTankDataOval()
{
int length=0, width=0, height=0, radius=0;

cout << "You selected a " << shape << " stock tank." <<endl;

cout << "Please enter length of oval: " << endl;
cin >> length;
cout << "Please enter width of oval: " << endl;
cin >> width;
cout << "Please enter height of oval: " << endl;
cin >> height;
cout << "Please enter radius of oval: " << endl;
cin >> radius;

l=length;
w=width;
h=height;
r=radius;
}



float StockTank::GetTankCapacity()
{
int tanktype=0;

switch (tanktype)
{

case 1:
vol=pi*(pow(r,2))*d;
totcapgal=vol/231;
break;

case 2:
vol=l*w*h;
totcapgal=vol/231;
break;

default:
break;
}
return totcapgal;
}

float StockTank::GetTankPercent()
{
cout << "Enter a percentage in whole numbers (ex: 25 = 25%) ";
cin >> percap;

totcapper=(totcapgal*percap)*.01;

cout.setf(ios::fixed);
cout.precision(2);

cout << "The capacity of the " << shape << " tank at " << percap << "% is " << totcapper << endl << endl;

return totcapper;

}

string StockTank::GetTankData()
{
stringstream ss;

ss.setf(ios::fixed);
ss.precision(2);


if (shape=="circular")
{

ss<<"The Stock Tank type is " << shape << endl
<< "The tank dimensions are: " << diam <<"\" by " << d << "\"."<< endl
<< "The Tank capacity (in gallons) of a "<< shape << " tank at 100 % "<< endl
<< "with these dimensions is: "<< totcapgal << endl << endl << endl;

}

else if (shape=="rectangular")
{

ss << "The Stock Tank type is " << shape << endl
<< "The tank dimentions are: " << l <<"\" by " << w << "\" by " << h << "\"."<< endl
<< "The tank capacity (in gallons ) of a " << shape << " tank at 100 % " << endl
<< "with these dimenstions is: " << totcapgal << endl << endl << endl;

}

return ss.str();


}[/PHP]

Everything seems to be working except the GetTankCapacity() function.

Also, i was wondering if i should use if then statements instead of switch and case?
also cant i call some math function. like MATH.PI or something?
thanks!
Jun 20 '06 #7

Banfa
Expert Mod 5K+
P: 8,916
Everything seems to be working except the GetTankCapacity() function.
I pointed out the error in this function in my previous post and since you have not sginificantly cchanged the function the error still exists. Please re-read my previous post. Everything is not working, for instance GetTankData is not handling the "Oval" case.

Also, i was wondering if i should use if then statements instead of switch and case?
Switch is absolutely fine if you have multiple options on a binary variable (an integer). It wont work for string though. And it's if ... else if ... else, there is no then in C/C++.

also cant i call some math function. like MATH.PI or something?
Unfortunately there is no standard library function returning the value of PI so you will just have to have it coded as a constant. I would consider using more decimal places than you have, I'd think 3.1416 should be plenty.
Jun 21 '06 #8

Post your reply

Sign in to post your reply or Sign up for a free account.