C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-10-2006, 12:01 AM   #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 01:47 AM.
RVDFan85 is offline   Reply With Quote
Old 11-10-2006, 12:53 AM   #2
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,710
Quote:
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.

Salem is offline   Reply With Quote
Old 11-30-2006, 05:04 PM   #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)':
RVDFan85 is offline   Reply With Quote
Old 11-30-2006, 05:25 PM   #4
Registered User
 
Join Date: Jan 2005
Posts: 7,252
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.
Daved is offline   Reply With Quote
Old 11-30-2006, 10:04 PM   #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...
RVDFan85 is offline   Reply With Quote
Old 11-30-2006, 10:28 PM   #6
Registered User
 
Join Date: Jan 2005
Posts: 7,252
Your main() is empty in the code above. If you don't add anything to it, chances are nothing will happen in the program.
Daved is offline   Reply With Quote
Old 11-30-2006, 10:34 PM   #7
Registered User
 
Join Date: Oct 2006
Posts: 20
what do I add to the int main function?
RVDFan85 is offline   Reply With Quote
Old 11-30-2006, 10:48 PM   #8
Registered User
 
Join Date: Jan 2005
Posts: 7,252
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.
Daved is offline   Reply With Quote
Old 12-02-2006, 10:51 PM   #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;
RVDFan85 is offline   Reply With Quote
Old 12-03-2006, 12:35 AM   #10
Registered User
 
Join Date: Jan 2005
Posts: 7,252
>> 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.
Daved is offline   Reply With Quote
Old 12-03-2006, 12:35 AM   #11
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,336
your compare function does not bother to check hours
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 12-03-2006, 12:42 AM   #12
Registered User
 
Join Date: Jan 2005
Posts: 7,252
>> 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.
Daved is offline   Reply With Quote
Old 12-03-2006, 12:46 AM   #13
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,336
I looked at this version of the class
Quote:
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.
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Menu Krush C Programming 17 09-01-2009 02:34 AM
Assignment Operator, Memory and Scope SevenThunders C++ Programming 47 03-31-2008 06:22 AM
Screwy Linker Error - VC2005 Tonto C++ Programming 5 06-19-2007 02:39 PM
Help with a pretty big C++ assignment wakestudent988 C++ Programming 1 10-30-2006 09:46 PM
A mini-compilier I made stuck in an infinite loop (lots of code) dan06 C++ Programming 1 10-27-2006 01:21 PM


All times are GMT -6. The time now is 02:51 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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