Help with assignment!

This is a discussion on Help with assignment! within the C++ Programming forums, part of the General Programming Boards category; CST 206 Programming Problem 4 pg. 579 Design, implement, and test a class that represents an amount of time in ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    20

    Question Help with assignment!

    CST 206 Programming Problem 4 pg. 579

    Design, implement, and test a class that represents an amount of time in minutes and seconds. The class should provide a constructor that sets the time to a specified number of minutes and seconds(1). The default constructor should create an object for a time of zero minutes and zero seconds(2). The class should provide observers that return the minutes and seconds separately(3), and an observer that returns the total time in seconds(4). Boolean comparison observers should be used that tell whether two times are equal(5), one is greater than the other(6), or one is lesser than the other(7). Transformers should be provided that add one time to another(8) or subtract one time from another(9). The class should not allow negative time (subtraction of more time than is currently stored should result in a time of 0:00)(10).

    here's my pseudocode

    Code:
     #include <iostream>
    using namespace std;
    
    class Time 
    {
        int mins, secs
    public:
        Time();
        Time( int, int, int, int);
    };
    
     (1)Time::Time(){
     mins=0;
     secs=0;
    };
    
    (2)Time::Time(int mins, int secs) {
        Time1(11, 30)
        Time2(12, 14)
        Time3(22, 54)
        Time4(23, 19)
    };
    
    
    int Return() const                 // (3??)
    //observer that returns the minutes and seconds seperately
      
       Retun min;
       Return Secs;
      
    int Total() const     //(4)
     //observer that retuens the total time as seconds
    
        Get total time
        Return total time as seconds
        
    bool Time::Equal( /* in */ Time otherTime ) const   (5)
    
    // Postcondition:
    //     Function value == true, if this time equals otherTime
    //                    == false, otherwise
    
    {
        return (hrs == otherTime.hrs && mins == otherTime.mins &&
                secs == otherTime.secs);
    }
    
    //******************************************************************
    
    bool Time::LessThan( /* in */ Time otherTime ) const  (6)
    
    // Precondition:
    //     This time and otherTime represent times in the
    //     same day
    // Postcondition:
    //     Function value == true, if this time is earlier
    //                             in the day than otherTime
    //                    == false, otherwise
    
    {
        return (hrs < otherTime.hrs ||
                hrs == otherTime.hrs && mins < otherTime.mins ||
                hrs == otherTime.hrs && mins == otherTime.mins
                                     && secs < otherTime.secs);
    }
    
    void Add Time()  (7)
      Add the times together
      
    void Subtract()  (8)
    Subtract two times
    I believe I have covered all the requirments. Two constructors, two observers, and two transformers??
    Last edited by RVDFan85; 11-10-2006 at 12:47 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    The class should provide a constructor that sets the time to a specified number of minutes and seconds(1). The default constructor should create an object for a time of zero minutes and zero seconds(2)
    Go through the rest of your requirement, and annotate with numbers for each thing which you're supposed to do.
    Then annotate your pseudocode with those numbers as well.

    When your pseudocode contains all the numbers, then you're done with that phase of the project.

    What's also worth doing now is designing a set of tests which test your requirements as well.

    What do you want from us?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    20
    Here's my code for this assignment, but I can not get it quite right...
    Code:
     #include "Time.h"
    #include <iostream>
    using namespace std;
    
    Time::Time(int m, int s)
    {
    set( m , s);
    }
    
    Time::Time()
    {
    mins = 0;
    secs = 0;
    }
    
    
    
    int Time::getMinutes() const
    {
    return mins;
    }
    
    int Time::getSeconds() const
    {
    return secs;
    }
    
    int Time::getTotalSeconds() const
    {
    return (mins * 60) + secs;
    }
    
    void Time::set(int m, int s)
    {
    if(m < 0) mins = 0;
    if(s < 0) secs = 0;
    
    
    if(m > 59)
    {
    mins = m % 60;
    }
    else 
    mins = m;
    
    if(s > 59)
    {
    mins += s / 60;
    secs = s % 60;
    }
    else
    secs = s;
    }
    
    void Time::increment()
    {
    secs++;
    
    if (secs > 59)
    {
    secs = 0;
    mins++;
    
    if (mins > 59)
    {
    mins = 0;
    ;
    
    
    }
    }
    }
    
    void Time::write() const
    {
    if (mins < 10)
    cout << '0';
    
    cout << mins << ':';
    
    if (secs < 10)
    cout << '0';
    
    cout << secs;
    }
    
    // comparison function
    // returns 0 if otherTime is equal to this time
    // returns -1 if otherTime is less than this time
    // returns 1 if otherTime is greater than this time
    // returns 69 if none of the other conditions return
    int Time::compare(const Time& otherTime) const
    {
    if (mins == otherTime.getMinutes() &&
    secs == otherTime.getSeconds())
    return 0;
    
    if (mins < otherTime.mins ||
     mins == otherTime.mins && secs < otherTime.secs)
    return -1;
    
    if (mins > otherTime.mins ||
     mins == otherTime.mins && secs > otherTime.secs)
    return 1;
    
    return 69;
    }
    
    void Time::addTimes(const Time& otherTime)
    {
    set (mins + otherTime.getMinutes(), secs + otherTime.getSeconds());
    }
    
    void Time::subtractTimes(const Time& otherTime)
    {
    int m = mins - otherTime.getMinutes();
    int s = secs - otherTime.getSeconds();
    
    if(m == 0)
       {
          s = 0;
       }
       else
       {
          if(s < 0)
          {
             m -= (s / 60) + 1;
             s += 60;
          }
    
    if(m < 0)
    {
    m -= m % 60;
    }
    }
    
    if(m <= 0) m = 0;
    if(s <= 0) s = 0;
    
    mins = m;
    secs = s;
    }
    
    int main()
    {
        return 0;
    }
    Code:
    class Time
    {
    private:
    int hrs;
    int mins;
    int secs;
    
    public:
    Time(int, int, int);
    Time();
    
    int getHours() const;
    int getMinutes() const;
    int getSeconds() const;
    int getTotalSeconds() const;
    
    void set(int, int);
    void increment();
    void write() const;
    int compare(const Time&) const;
    void addTimes(const Time&);
    void subtractTimes(const Time&);
    };
    and the errors I am getting are:

    Line File Message
    6 Time21.cpp prototype for `Time::Time(int, int)' does not match any in class `Time'
    error Time.h candidates are: Time::Time(const Time&)
    are are Time::Time(const Time&)
    10 Time.h Time::Time()
    9 Time.h Time::Time(int, int)
    Time21.cpp In constructor `Time::Time(int, int)':

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Look at the text of the errors. The first one tells you what is wrong. Time::Time(int, int)' does not match any (prototype) in class 'Time', so fix the prototype in the header file to match the implementation or fix the implementation to match the header file.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    20
    I've got it working now, but when I compile it, nothing comes up..... just a blank screen...

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Your main() is empty in the code above. If you don't add anything to it, chances are nothing will happen in the program.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    20
    what do I add to the int main function?

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Code to test your class. Create an instance or two or three or more of the class. Use all of its member functions. Output the results and test that they are what you expect. Verify that the rules from the assignment were followed.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    20
    Here is what I have so far in the int main function.. why doesn't time1.compare() work as well??
    Code:
    int main()
    {   Time Time1(3, 56);
        Time Time2;
        
        
        Time1.write();
        cout <<""<<endl;
        Time2.write();
        cout<<""<< endl;
        
        cout<< "The minutes for Time1 are "<< Time1.getMinutes() << endl;
        cout<< "The seconds for Time1 are " <<Time1.getSeconds()<< endl;
        
        cout<< "The minutes for Time2 are " <<Time2.getMinutes()<< endl;
        cout<< "The seconds for Time2 are " <<Time2.getSeconds()<< endl;
        
        cout<< "Time1 in seconds would be "<< Time1.getTotalSeconds()<< endl;
        cout<< "Time2 converted to seconds would be" << Time2.getTotalSeconds()<< endl;
        
        Time1.increment();
        cout << "Incremented by 1 second, the new Time1 is ";
        Time1.write();
        cout << ""<< endl;
        Time2.increment();
        cout << "The new Time2 is ";
        Time2.write();
        cout<< "" << endl;
        
        
       system("Pause");
        return 0;

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> why doesn't time1.compare() work as well??
    Does that mean you tried it and it didn't work? If so, please show the code you tried, and explain why and how it didn't work.

    Don't forget that you have to pass a second Time object to the function to compare to.

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    your compare function does not bother to check hours
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> your compare function does not bother to check hours
    There are no hours in the Time class so there's no reason to check hours.

  13. #13
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    I looked at this version of the class
    Code:
    class Time
    {
    private:
    int hrs;
    int mins;
    int secs;
    
    public:
    Time(int, int, int);
    Time();
    
    int getHours() const;
    int getMinutes() const;
    int getSeconds() const;
    int getTotalSeconds() const;
    
    void set(int, int);
    void increment();
    void write() const;
    int compare(const Time&) const;
    void addTimes(const Time&);
    void subtractTimes(const Time&);
    };
    But according to the constructor call in the last sa,ple I see that this code is not the actual version... So Sorry.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Help with a pretty big C++ assignment
    By wakestudent988 in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2006, 08:46 PM
  5. Replies: 1
    Last Post: 10-27-2006, 01:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21