help with classes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • el jefe
    New Member
    • Jun 2006
    • 8

    help with classes

    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 StockTankCalcul ator 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 StockTankCalcul ator
    {
    private:
    //what is all the data that the calculator needs?

    public:
    StockTankCalcul ator();

    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 StockTankCalcul ator 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 StockTankCalcul ator 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!
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    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.

    Comment

    • el jefe
      New Member
      • Jun 2006
      • 8

      #3
      i meant help with coding......

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        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.

        Comment

        • el jefe
          New Member
          • Jun 2006
          • 8

          #5
          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.SetT ankDataCircle(i nt diam=72, int d=24);

          do
          {

          string info;


          cout << "Stock Tank Calculator"<<en dl;
          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<<en dl;
          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.SetT ankShape(anssha pe);
          //Calculator.SetT ankDataCircle() ;
          //Calculator.GetT ankCapacity();

          case 2:
          Calculator.SetT ankShape(anssha pe);
          //Calculator.SetT ankDataRectangl e();
          //Calculator.GetT ankCapacity();

          }



          case 2:

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

          info=Calculator .GetTankData();

          cout << info;

          break;

          case 3:

          Calculator.GetT ankPercent();


          break;

          case 4:

          break;


          default:

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


          }

          }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."<<e ndl;
          cout<<endl;
          }[/PHP]
          stocktank.cpp
          [PHP]
          #include "stocktank. h"
          #include <string>
          #include <iostream>
          #include <cmath>

          using namespace std;

          double pi=3.14;

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



          }

          void StockTank::SetT ankShape(int s)
          {

          if (s=1)
          {
          shape="circular ";
          //SetTankDataCirc le();
          }
          else if (s=2)
          {
          shape="rectangu lar";
          //SetTankDataRect angle();
          }
          else if (s=3)
          {
          shape="oval";
          }


          }


          void StockTank::SetT ankDataCircle()
          {
          int diameter;
          int depth;
          shape="circlula r";
          diameter=diam;
          depth=d;
          r=diam/2;


          StockTank::GetT ankCapacity();


          }

          void StockTank::SetT ankDataCircle(i nt diam, int d)
          {
          int diameter;
          int depth;
          shape="circlula r";
          diameter=diam;
          depth=d;
          r=diam/2;


          //StockTank::GetT ankCapacity();


          }
          void StockTank::SetT ankDataRectangl e()
          {
          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::GetT ankCapacity();

          }





          float StockTank::GetT ankCapacity()
          {
          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::GetT ankPercent()
          {
          cout << "Enter a percentage in whole numbers (ex: 25 = 25%) ";
          cin >> percap;

          totcapper=(totc apgal*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<<e ndl<<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,totca pper;


          public:
          StockTankCalcul ator();
          void SetTankShape(in t s);
          void SetTankDataCirc le();
          void SetTankDataCirc le(int diam, int d);
          void SetTankDataRect angle();
          //void SetTankDataOval ();
          float GetTankCapacity ();
          float GetTankPercent( );
          string GetTankData();

          };

          #endif
          [/PHP]
          Last edited by el jefe; Jun 18 '06, 10:44 PM.

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Originally posted by el jefe
            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::Stoc kTank()
            {
            ...
            }[/php]

            In Stocktank.cpp my comments below in bold

            Code:
            #include "stocktank.h"
            #include <string>
            #include <iostream>
            #include <cmath>
            
            using namespace std;
            
            double pi=3.14;
            [b]/* Why use a double if you are only going to use 3 places 
               of precision.  Additionally this is a constant and only used 
               in this file so declare it const static. */[/b]
            StockTank::StockTankCalculator()
            {
                //hape="circular";
                //diam=72;
                //d=24;
                GetTankCapacity();
                GetTankData();
            [b]/* This function looks like it needs work. */[/b]
            
            
            
            }
            
            void StockTank::SetTankShape(int s)
            [b]/* It would be better to have this function accept the same 
               type as shape, then all you have to do is verify it has a legal value
               and copy it to shape rather than input a completely unrelated type
               and interpret it. */[/b]
            {
                    
                if (s=1)
            [b]/* This is an assignment and returns the value 1, you will only 
               ever be able to set the shape to circular.  Test for equality is == */[/b]
                {
                    shape="circular";
                    //SetTankDataCircle();
                }
                else if (s=2)
                {
                    shape="rectangular";
                    //SetTankDataRectangle();
                }
                else if (s=3)
                {
                    shape="oval";
                }
            
            
            }
            
            
            void StockTank::SetTankDataCircle()
            [b]/* This function is outside the specification of the 
               assignment and is not required */[/b]
            {
                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;
            [b]/* Diameter and depth are complement unused and unrequired */[/b]
                shape="circlular";
            [b]/* Interesting should you default the shape to circular to 
               check to see if the shape is circular and if not return an error */[/b]
                diameter=diam;
                depth=d;
                r=diam/2;
            
            
                //StockTank::GetTankCapacity();
            
            
            }
            void StockTank::SetTankDataRectangle()
            [b]/* It is good practice to separate you input and output and 
               your data storage.  The StockTank class describes a water 
               tank which has no capacity for text input and output.  I think
               it would be better to call you cout/cin line externally to the
               function and pass the length width and height to this
               function as parameters. */[/b]
            {
                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)
            [b]/* You switch on a local variable which is initialised to 0.
               You do not even have a 0 case so this is bound to do 
               absolutely nothing.  You need to be referencing the 
               variable shape. */[/b]
                {
            
                    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()
            [b]/* Again pass the required percentage as a parameter 
               rather than using cout/cin within this class method. */[/b]
            {
                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 StockTank::GetTankData()
            [b]/* This function breaks the assignment as you are asked to 
               write a function that returns a string, which this does but 
               this also outs to cout which is not required. */[/b]
            {
                string data;
            
                data=shape;
                data+=diam;
                data+=d;
                data+=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;
            
            }
            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.

            Comment

            • el jefe
              New Member
              • Jun 2006
              • 8

              #7
              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::Stoc kTankCalculator ()
              {
              shape="circular ";
              diam=72;
              d=24;
              r=diam/2;
              GetTankCapacity ();
              GetTankData();



              }

              void StockTank::SetT ankShape(int s)
              {

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


              }


              void StockTank::SetT ankDataCircle()
              {
              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::SetT ankDataRectangl e()
              {
              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::SetT ankDataOval()
              {
              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::GetT ankCapacity()
              {
              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::GetT ankPercent()
              {
              cout << "Enter a percentage in whole numbers (ex: 25 = 25%) ";
              cin >> percap;

              totcapper=(totc apgal*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::GetT ankData()
              {
              stringstream ss;

              ss.setf(ios::fi xed);
              ss.precision(2) ;


              if (shape=="circul ar")
              {

              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=="rectan gular")
              {

              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!

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                Originally posted by el jefe
                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.

                Originally posted by el jefe
                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++.

                Originally posted by el jefe
                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.

                Comment

                Working...