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

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 Subtract()  (8)
Subtract two times```
I believe I have covered all the requirments. Two constructors, two observers, and two transformers?? 2. 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? 3. 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;
}

{
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 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. 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. I've got it working now, but when I compile it, nothing comes up..... just a blank screen... 6. 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. what do I add to the int main function? 8. 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. 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. >> 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. your compare function does not bother to check hours 12. >> 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. 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; Popular pages Recent additions 