Design, implement, and test a class that represents an amount of time in minutes
ID: 3658866 • Letter: D
Question
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. The default constructor should create an object for a time of zero minutes and zero seconds. The class should provide observers that return the minutes and the seconds separately, and an observer that returns the total time in seconds (minutes x 60 + seconds). Boolean comparison observers should be provided that test whether two times are equal, one time is greater than the other, or one time is less than the other. (You may use RelationType and function ComparedTo if you choose). A function should be provided that adds one time to another, and another function that subtracts one time from another. The class should not allow negative times (subtraction of more time than is currently stored should result in a time of 0:00). This class should be immutable.
#include
"RelationType.h"
class
Time
{
private
:
int minutes;
int seconds;
public
:
//Default Constructors
Time();
//Post: minutes, and seconds are set to 0
//Pre: 0 <= minutes <= 59, 0 <= seconds <= 59
Time(
int initMinutes, int initSeconds);
//Post: Time is set according to the incoming parameters
int getMinutes();
//Post: returns minutes
int getSeconds();
//Post: returns seconds
int totalTime();
//Post: returns total time in seconds
//Pre: time to compare
bool equalTime (Time cTime);
//Post: returns true if both times are equal
//Pre: time to compare
bool lessTime (Time cTime);
//Post: returns true if cTime is less
//Pre: time to add
Time addTime (Time cTime);
//Post: returns time after addition
//Pre: time to subtract
Time subTime (Time cTime);
//Post: returns time after subtraction
//Pre: time to compare
RelationType comparedTo (Time cTime);
//Post: Returns Before if instance comes before
//cTime;
//Returns Same if instance and cTime are the same;
//Returns After if isntance comes after cTime;
};
?
#include
"Time.h"
#include
<iostream>
using
namespace std;
//default constructor to initialise values to 0
Time::Time()
{
minutes = 0;
seconds = 0;
}
//Constructor to initialise values with parameters
Time::Time(
int initMinutes, int initSeconds)
{
minutes = initMinutes;
seconds = initSeconds;
}
//Method to return minutes
int
Time::getMinutes()
{
return minutes;
}
//Method to return seconds
int
Time::getSeconds()
{
return seconds;
}
//Method to convert time into seconds
int
Time::totalTime()
{
return minutes*60+seconds;
}
//Method to check times are equal or not
bool
Time::equalTime(Time cTime)
{
RelationType equal;
equal = comparedTo(cTime);
if (equal == SAME)
return true;
return false;
}
//Method to check time is less than parameter time or not
bool
Time::greaterTime(Time cTime)
{
RelationType greater;
greater = comparedTo(cTime);
if (greater == AFTER)
return true;
return false;
}
//Method to check time is greater than parameter time or not
bool
Time::lessTime(Time cTime)
{
RelationType less;
less = comparedTo(cTime);
if (less == BEFORE)
return true;
return false;
}
//Method to add two times
Time Time::addTime(Time cTime)
{
int min, sec;
min = minutes+cTime.minutes;
sec = seconds+cTime.seconds;
if (sec>59)
{
min = min+1;
sec = sec - 60;
}
Time rTime(min,sec);
return rTime;
}
//Method to subtract two times
Time Time::subTime(Time cTime)
{
int min, sec;
min = minutes-cTime.minutes;
sec = seconds-cTime.seconds;
if(sec<0)
{
min = min-1;
sec = sec + 60;
}
if(min<0)
{
min = 0;
sec = 0;
}
Time rTime(min,sec);
return rTime;
}
//Method to compare two times
RelationType Time::comparedTo(Time cTime)
{
if (minutes < cTime.minutes)
return BEFORE;
else if (minutes > cTime.minutes)
return AFTER;
else if (seconds < cTime.seconds)
return BEFORE;
else if (seconds > cTime.seconds)
return AFTER;
else
return SAME;
}
//Header Files
#include
<iostream>
#include
"Time.h"
using
namespace std;
?
//Main Function
int
main()
{
//Creating two time objects
Time time1 (45, 25);
Time time2 (35, 46);
//Display values in time 1
cout<<
"Time 1 is "<<time1.getMinutes()
<<
":"<<time1.getSeconds()<<endl;
cout<<
"The time in seconds is "
<<time1.totalTime()<<
"."<<endl;
//Display values in time 2
cout<<
"Time 2 is "<<time2.getMinutes()
<<
"."<<time2.getSeconds()<<endl;
cout<<
"The time in seconds is "
<<time2.totalTime()<<
"."<<endl<<endl;
//Compare two times and display the times equal or less or greater
if(time1.equalTime (time2) )
cout<<
"Time 1 and Time 2 are equal."<<endl;
if(time1.greaterTime (time2) )
cout<<
"Time 1 is greater than Time 2."<<endl;
if(time1.lessTime (time2) )
cout<<
"Time 1 is less than Time 2."<<endl;
//Create a time object and store the sum of times
Time sumTimes = time1.addTime(time2);
//Display sum of times
cout<<endl<<
"Time after addition is "
<<sumTimes.getMinutes()<<
" : "
<<sumTimes.getSeconds()<<endl;
//Create a time object and store the difference of times
Time subTimes = time1.subTime(time2);
//Display difference of times
cout<<endl<<
"Time after subtraction is "
<<subTimes.getMinutes()<<
" : "
<<subTimes.getSeconds()<<endl;
system(
"Pause");
return 0;
}
Explanation / Answer
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. by fixing it ; i got your code working:)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.