GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 19-Nov-2006, 13:55
mapes479 mapes479 is offline
New Member
 
Join Date: Oct 2006
Posts: 12
mapes479 is on a distinguished road

constructors/classes


i've been working on designing and implementing a class that represents an amount of time in minutes and seconds. i'm supposed to use constructors that sets the time to a specified number of minutes and seconds. the default constructor should create an object for a time of zero mniutes and zero seconds. the class should provide observers that return the minutes and seconds separately, and an observer that returns the total time in seconds (minutes x 60 + seconds). boolean observers should be used too that test whether two times are equal, one is greater than the other or less than the other. transformers should be provided that add one time to another and subtract that time from another. no negative time is allowed. is my output correct? something just doesn't seem right about it. any advice would be appreciated. i want the code to look better than what it does right now.

CPP / C++ / C Code:
class Time
{
public:
    Time();
    //Postcondition: class object is constructed
    //Time object is 0 mins, 0 secs

    Time(int intMins, int intSecs);
    //Precondition: 
    //        passed in numbers >= 0. if intSecs > 59, increments the minutes in the object.
    //        negative values are set to 0.
    //Postcondition: 
    //        constructed object has passed in parameters

    int returnMins() const;
    //Precondition: 
    //       Time object has been constructed
    //Postcondition: 
    //        Timeclient receives the minutes part of the Time object that was
    //        already created.

    int returnSecs() const;
    //Precondition: 
    //          Time object has been constructed
    //Postcondition: 
    //          client receives the seconds component of the existing Time object

    int returnTotalTime() const;
    //Precondition: 
    //          Time object has been constructed
    //Postcondition: 
    //          Timeclient receives total time in seconds

    bool Equal(Time otherTime) const;
    //Precondition: 
    //            Two existing Time objects
    //Postcondition:
    //        client receives boolean value stating true or not (equality)

    bool GreaterThan(Time otherTime) const;
    //Precondition: 
    //           Two existing Time objects
    //Postcondition: 
    //           client receives boolean value stating whether calling Time object is larger the otherTime or not



    bool LessThan(Time otherTime) const;
    //Precondition: 
    //          two existing Time objects
    //Postcondition: 
    //          client receives boolean value stating whether calling Time object is less than the otherTime or not 
    
    Time addTime(Time otherTime);
    //Precondition: 
    //       two existing Time objects
    //Postcondition: 
    //       new Time object is returned to Timeclient; Time
    //       client can read mins and secs of new object using public methods
    //       new object has of calling object minutes and seconds plus minutes and seconds of otherTime object

    Time subtractTime(Time otherTime);
    //Precondition: 
    //        two existing Time objects
    //Postcondition: 
    //         new Time object is returned to Timeclient; Timeclient can read mins and secs members of new object using public methods
    //         new object consists of calling object minutes and seconds minus minutes and seconds of otherTime object 
    //         if result of calculation either together or seperate is greater than  0, set object to 0 mins, 0 secs

private:
    int mins;
    int secs;
};

#include "Time.h"
#include <iostream>

using namespace std;

Time::Time() 
{
    mins = 0;
    secs = 0;
}

Time::Time(int intMins, int intSecs)
{
    mins = intMins;
    secs = intSecs;

    if (secs >59)
    {
        mins++;
        secs -= 60;
    }

    if (intMins < 0)
        mins = 0;

    if (intSecs < 0)
        secs = 0;

}

int Time::returnMins() const 
{
    return mins;
}

int Time::returnSecs() const 
{
    return secs;
}

int Time::returnTotalTime() const 
{
    return ((60 * mins) + secs);
}    

bool Time::Equal(Time otherTime) const 
{
    if ((otherTime.returnTotalTime()) != (returnTotalTime())) // Time objects do not equal on another
        return false;
    else
        return true;
}

bool Time::GreaterThan(Time otherTime) const 
{
    if ((returnTotalTime()) > (otherTime.returnTotalTime()))
        return true; //first time is greater than otherTime
    else
        return false; //first time is not greater than otherTime
    
}

bool Time::LessThan(Time otherTime) const
{
    if ((returnTotalTime()) < (otherTime.returnTotalTime()))
        return true; //first time is less than otherTime object
    else
        return false; //first time is less than otherTime object
}

Time Time::addTime(Time otherTime)
{
    int newMins;
    int newSecs;

    newMins = mins + otherTime.mins; 
    newSecs = secs + otherTime.secs;

    /*if (newSecs > 59)
    {
        newMins++;
        newSecs-=60;
    }
    */
    Time newAddTime(newMins,newSecs);
    return newAddTime;
    //return a new class instance 
}

Time Time::subtractTime(Time otherTime) 
{
    int newMins;
    int newSecs;

    newMins = mins - otherTime.mins;
    //if (newMins < 0)
    //    newMins = 0;

    newSecs =  secs - otherTime.secs;
    //if (newSecs < 0)
    //    newSecs = 0;

    Time newSubtractTime(newMins,newSecs);
    return newSubtractTime;
    //return a new class instance 
}

#include "Time.h"
#include <iostream>

using namespace std;

int main()
{

    Time time1;

    cout << "Time1 min: " << time1.returnMins() << endl;
    cout << "Time1 sec: " << time1.returnSecs() << endl;

    Time time2(30,100);

    cout << "Time2 min: " << time2.returnMins() << endl;
    cout << "Time2 sec: " << time2.returnSecs() << endl;

    cout << "Time1 min: " << time1.returnMins() << endl;
    cout << "Time1 secs: " << time1.returnSecs() << endl;

    cout << "Time2 min: " << time2.returnMins() << endl;
    cout << "Time2 secs: " << time2.returnSecs() << endl;

    cout << "Time1 total secs: " << time1.returnTotalTime() << endl;
    cout << "Time2 total secs: " << time2.returnTotalTime() << endl;

    if (time1.Equal(time2))
        cout << "The 2 time objects are equal" << endl;
    else
        cout << "The 2 time objects are not equal" << endl;

    Time time3(60,0);
    Time time4(60,0);

    if (time3.Equal(time4))
        cout << "The 2 time objects are equal" << endl;
    else
        cout << "The 2 time objects are not equal" << endl;

    Time time5(50,0);
    Time time6(100,0);

    if (time5.GreaterThan(time6))
        cout << "TIME5 is larger than TIME6" << endl;
    else
        cout << "TIME5 is not larger than TIME6" << endl;

    if (time5.LessThan(time6))
        cout << "TIME5 is less than TIME6" << endl;
    else
        cout << "TIME5 is not less than TIME6" << endl;

    Time time7(10,10);
    Time time8(10,10);

    Time localAddTime;
    localAddTime = time7.addTime(time8);
    cout << localAddTime.returnMins() << endl;
    cout << localAddTime.returnSecs()<< endl;    
    
    Time time9(10,10);
    Time time10(20,20);

    Time localSubtractTime;
    localSubtractTime = time9.subtractTime(time10);
    cout << localSubtractTime.returnMins() << endl;
    cout << localSubtractTime.returnSecs() << endl;

    Time localSubtractTime2;
    localSubtractTime2 = time10.subtractTime(time9);
    cout << localSubtractTime2.returnMins() << endl;
    cout << localSubtractTime2.returnSecs() << endl;

    cin.get();cin.get();
    return 0;






}
Last edited by LuciWiz : 19-Nov-2006 at 14:02. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 19-Nov-2006, 14:32
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,623
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: constructors/classes


Quote:
Originally Posted by mapes479
is my output correct? something just doesn't seem right about it.

What if you let time9 be equal to 10 minutes and 20 seconds and let time10 be equal to 20 minutes and 20 seconds? What is time10 - time9?


Doing it the hard way (in my poor, tired little old pea-brained head): time10 - time9 is equal to nine minutes and 50 seconds. What does your program say?

What if some troublemaker (also known as "user") tried to initialize a time of 30 minutes and 200 seconds? What would be the result? Does your program output seem right?

Regards,

Dave
  #3  
Old 19-Nov-2006, 14:59
amad1337's Avatar
amad1337 amad1337 is offline
Junior Member
 
Join Date: Dec 2004
Posts: 54
amad1337 will become famous soon enough

Re: constructors/classes


Quote:
Originally Posted by mapes479
i want the code to look better than what it does right now.

To make your code more c++ look a like use operator overloading its more elegant and funtional.

Declaration:
CPP / C++ / C Code:
bool Equal(Time otherTime) const;
change it to

CPP / C++ / C Code:
bool Time::operator==(Time &otherTime) const;

and change the control statement from

CPP / C++ / C Code:
if (time3.Equal(time4))

to
CPP / C++ / C Code:
if(time3 == time4)

it makes more sense and gives the code more logic. Use the internal machanism off c++ which is the operator overloading.
  #4  
Old 19-Nov-2006, 17:34
mapes479 mapes479 is offline
New Member
 
Join Date: Oct 2006
Posts: 12
mapes479 is on a distinguished road

Re: constructors/classes


Thanks for your help and advice. I went through my code and updated it like you said. I guess my question now is - when it brings up the output this is what it shows

Time1 min: 0
Time1 sec:0
Time2 min: 31
Time2 sec: 40
Time1 min: 0
Time1 secs: 0
Time2 min: 31
Time2 secs:40
Time1 total secs:0
Time2 total secs: 1900
The 2 time objects are not equal
The 2 time objects are equal
Time5 is not larger than TIME6
Time5 is less than TIME6
20
20
0
0
10
10

I know this is a stupid question, but is this what is supposed to happen based on what I wrote about the program assignment?? It just looks funny.
 
 

Recent GIDBlogFirst week of IA training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:56.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.