Daniel Maguire
Daniel Maguire

Reputation: 33

Trouble developing simple Clock and Date class

I'm getting the problem with getting the desired results.

I've tried a lot of time frying my brain, but it wasn't working... I think the problem is malfunctions of void functions in Date class and some pointer mistakes and advanceTime functions in Clock class, but I feel ambiguous myself how to further progress in this step.

I tried to describe the problem at my best, so could you please help me?

The expected results are like this:

Setting the clock to 2:03:04 using normal setters
02:03:04
Advancing the clock one hour with a value parameter
02:03:04
Advancing the clock one hour with a pointer parameter
03:03:04
Advancing the clock one hour with a reference parameter
04:03:04
Setting the clock to 8:09:10 with cascading setters returning an instance
08:03:04
Setting the clock to 5:06:07 with cascading setters returning a pointer
05:06:07
Setting the clock to 11:12:13 with cascading setters returning a reference
11:12:13
Setting the clock to 23:59:59
23:59:59
Advancing the clock one second
00:00:00

However, what I get is:

Setting the clock to 2:03:04 using normal setters
02:03:04
Advancing the clock one hour with a value parameter
02:03:04
Advancing the clock one hour with a pointer parameter
03:03:04
Advancing the clock one hour with a reference parameter
04:03:04
Setting the clock to 8:09:10 with cascading setters returning an instance
08:03:04
Setting the clock to 5:06:07 with cascading setters returning a pointer
05:03:04
Setting the clock to 11:12:13 with cascading setters returning a reference
11:03:04
Setting the clock to 23:59:59
23:59:59
Advancing the clock one second
23:59:00

I've tried to do some evaluation of remaining things in hour, minute, and second.

Well, I was getting in lost in the ways of getting the 00:00:00 from 59 marginals, and also, some advancing clock is working, but something is not working.

I was actually getting in confused.

Clock.h

// Clock.h
// This class describes a Clock.

class Clock {
    private:
        int hour;
        int minute;
        int second;
    public:
        // Constructor
        Clock();

        // Normal Setters
        void setHour(int newHour);
        void setMinute(int newMinute);
        void setSecond(int newSecond);

        // Cascading Setters returning an instance
        Clock setHourIns(int newHour);
        Clock setMinuteIns(int newMinute);
        Clock setSecondIns(int newSecond);

        // Cascading Setters returning a pointer
        Clock * setHourPtr(int newHour);
        Clock * setMinutePtr(int newMinute);
        Clock * setSecondPtr(int newSecond);

        // Cascading Setters returning a reference
        Clock & setHourRef(int newHour);
        Clock & setMinuteRef(int newMinute);
        Clock & setSecondRef(int newSecond);

        // Getters
        int getHour() const;
        int getMinute() const;
        int getSecond() const;

        // Advance one hour
        void advanceOneHour();
        void advanceOneMinute();
        void advanceOneSecond();

        // Printing Methods
        void printTwoDigits(int number) const;
        void printTime() const;
};

Clock.cpp

// Clock.cpp
// This class describes a Clock.

#include <iostream>
#include <iomanip>
#include "Clock.h"

using namespace std;

// Constructor for Clock
Clock::Clock() {
    hour = 0;
    minute = 0;
    second = 0;
}

// Normal Setters
void Clock::setHour(int newHour) {
    hour = newHour;
}

void Clock::setMinute(int newMinute) {
    minute = newMinute;
}

void Clock::setSecond(int newSecond) {
    second = newSecond;
}

// Cascading Setters which return an instance
Clock Clock::setHourIns(int newHour) {
    hour = newHour;
    return (*this);
}

Clock Clock::setMinuteIns(int newMinute) {
    minute = newMinute;
    return (*this);
}

Clock Clock::setSecondIns(int newSecond) {
    second = newSecond;
    return (*this);
}

// Cascading Setters which return an pointer
Clock * Clock::setHourPtr(int newHour) {
    hour = newHour;
    return this;
}

Clock * Clock::setMinutePtr(int newMinute) {
    minute = newMinute;
    return this;
}

Clock * Clock::setSecondPtr(int newSecond) {
    second = newSecond;
    return this;
}

// Cascading Setters which return a reference
Clock & Clock::setHourRef(int newHour) {
    hour = newHour;
    return (*this);
}

Clock & Clock::setMinuteRef(int newMinute) {
    minute = newMinute;
    return (*this);
}

Clock & Clock::setSecondRef(int newSecond) {
    second = newSecond;
    return (*this);
}

// Getters
int Clock::getHour() const {
    return hour;
}

int Clock::getMinute() const {
    return minute;
}

int Clock::getSecond() const {
    return second;
}

// Advance one hour
void Clock::advanceOneHour() {
    if((hour + 1) >= 24) {
        hour = 0;
    }
    hour = (hour + 1) % 24;
}

void Clock::advanceOneMinute() {
    if((minute + 1) >= 60) {
        minute = 0;
        hour++;
    }
    minute = (minute + 1) % 60;
}

void Clock::advanceOneSecond() {
    if((second + 1) >= 60) {
        second = 0;
        minute++;
    }
    second = (second + 1) % 60;
}

// Printing methods
void Clock::printTwoDigits(int number) const {
    cout << setw(2) << setfill('0') << number;
}

void Clock::printTime() const {
    printTwoDigits(hour); cout << ":";
    printTwoDigits(minute); cout << ":";
    printTwoDigits(second); cout << endl;
}

ClockMain.cpp

// ClockMain.cpp

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

using namespace std;

// Advance the clock by one hour with a value parameter
void advanceOneHourVal(Clock myClock) {
    myClock.advanceOneHour();
}

// Advance the clock by one hour with a pointer parameter
void advanceOneHourPtr(Clock * myClock) {
    myClock->advanceOneHour();
}

// Advance the clock by one hour with a reference parameter
void advanceOneHourRef(Clock & myClock) {
    myClock.advanceOneHour();
}

int main() {
    Clock c1;

    cout << "Setting the clock to 2:03:04 using normal setters" << endl;
    c1.setHour(2); c1.setMinute(3); c1.setSecond(4); c1.printTime();

    cout << "Advancing the clock one hour with a value parameter" << endl;
    advanceOneHourVal(c1);
    c1.printTime();

    cout << "Advancing the clock one hour with a pointer parameter" << endl;
    advanceOneHourPtr(&c1);
    c1.printTime();

    cout << "Advancing the clock one hour with a reference parameter" << endl;
    advanceOneHourRef(c1);
    c1.printTime();

    cout << "Setting the clock to 8:09:10 with cascading setters returning an instance" << endl;
    c1.setHourIns(8).setMinuteIns(9).setSecondIns(10);
    c1.printTime();

    cout << "Setting the clock to 5:06:07 with cascading setters returning a pointer" << endl;
    c1.setHourIns(5).setMinuteIns(6).setSecondIns(7);
    c1.printTime();

    cout << "Setting the clock to 11:12:13 with cascading setters returning a reference" << endl;
    c1.setHourIns(11).setMinuteIns(12).setSecondIns(13);
    c1.printTime();

    cout << "Setting the clock to 23:59:59" << endl;
    c1.setHourRef(23).setMinuteRef(59).setSecondRef(59);
    c1.printTime();
    c1.advanceOneSecond();
    cout << "Advancing the clock one second" << endl;
    c1.printTime();
}

Similarly, I'm also getting the similar errors, in Date class. The error indicates that there's wrong returning of void functions, in advancing.

Date.cpp

// Date.cpp
// This class describes a Date.

#include <iostream>
#include <iomanip>
#include "Date.h"

using namespace std;

// Constructor for Clock
Date::Date() {
    year = 0;
    month = 0;
    day = 0;
}

// Normal Setters
void Date::setYear(int newYear) {
    year = newYear;
}

void Date::setMonth(int newMonth) {
    month = newMonth;
}

void Date::setDay(int newDay) {
    day = newDay;
}

// Cascading Setters which return an instance
Date Date::setYearIns(int newYear) {
    year = newYear;
    return (*this);
}

Date Date::setMonthIns(int newMonth) {
    month = newMonth;
    return (*this);
}

Date Date::setDayIns(int newDay) {
    day = newDay;
    return (*this);
}

// Cascading Setters which return an pointer
Date * Date::setYearPtr(int newYear) {
    year = newYear;
    return this;
}

Date * Date::setMonthPtr(int newMonth) {
    month = newMonth;
    return this;
}

Date * Date::setDayPtr(int newDay) {
    day = newDay;
    return this;
}

// Cascading Setters which return a reference
Date & Date::setYearRef(int newYear) {
    year = newYear;
    return (*this);
}

Date & Date::setMonthRef(int newMonth) {
    month = newMonth;
    return (*this);
}

Date & Date::setDayRef(int newDay) {
    day = newDay;
    return (*this);
}

// Getters
int Date::getYear() const {
    return year;
}

int Date::getMonth() const {
    return month;
}

int Date::getDay() const {
    return day;
}

// Advance one date
void Date::advanceOneYear() {
    year = year + 1;
}

void Date::advanceOneMonth() {
    if (month == 1 || month == 3 || month == 5 || month == 7 || month == 9 || month == 11) {
        month = (month + 1) % 31;
    } else if (month == 2) {
        month = (month + 1) % 28;
    } else {
        month = (month + 1) % 30;
    }
}

void Date::advanceOneDay() {
    day = (day + 1) % 365;
}

// Printing methods
void Date::printTwoDigits(int number) const {
    cout << setw(2) << setfill('0') << number;
}

void Date::printDate() const {
    printTwoDigits(year); cout << ":";
    printTwoDigits(month); cout << ":";
    printTwoDigits(day); cout << endl;
}

Date.h

// Date.h
// This class describes a Clock.

class Date {
    private:
        int day;
        int month;
        int year;
    public:
        // Constructor
        Date();

        // Normal Setters
        void setYear(int newYear);
        void setMonth(int newMonth);
        void setDay(int newDate);

        // Cascading Setters returning an instance
        Date setYearIns(int newYear);
        Date setMonthIns(int newMonth);
        Date setDayIns(int newDate);

        // Cascading Setters returning a pointer
        Date * setYearPtr(int newYear);
        Date * setMonthPtr(int newMonth);
        Date * setDayPtr(int newDate);

        // Cascading Setters returning a reference
        Date & setYearRef(int newYear);
        Date & setMonthRef(int newMonth);
        Date & setDayRef(int newDate);

        // Getters
        int getYear() const;
        int getMonth() const;
        int getDay() const;

        // Advance one hour
        void advanceOneYear();
        void advanceOneMonth();
        void advanceOneDay();

        // Printing Methods
        void printTwoDigits(int number) const;
        void printDate() const;
};

DateMain.cpp

// DateMain.cpp

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

using namespace std;

int main() {
    Date d1;

    cout << "Setting the date to 2018/02/17 using cascading setters" << endl;
    d1.setYear(2018).setMonth(2).setDay(17);
    d1.printDate();

    cout << "Advancing the date one month" << endl;
    advanceOneMonth(d1);
    d1.printDate();

    cout << "Happy St. Patrick's Day!!" << endl << endl;

    cout << "Setting the date to 12/31/2018 (US Style) using cascading setters" << endl;
    d1.setMonth(12).setDay(31).setYear(2018);
    d1.printDate();

    cout << "Advancing the date one day" << endl;
    advanceOneDay(d1);
    d1.printDate();

    cout << "Happy New Year!!" << endl;
}

Upvotes: 1

Views: 115

Answers (2)

JL. Sanchez
JL. Sanchez

Reputation: 371

Lets examine this function.

void Clock::advanceOneMinute() {
    minute = (minute + 1) % 60;
    if((minute + 1) >= 60) {
        hour++;
    }
}

In pseudocode:

1. Add 1 to minute, divide by 60 and store the remain into minute.
2. If minute is greater or equal than 59, then add 1 to hour

Before calling: minute=58, hour=5.

After calling: minute=59, hour=6.

Fix: swap change and comparison

void Clock::advanceOneMinute() {
    if((minute + 1) >= 60) {
        hour++;
    }
    minute = (minute + 1) % 60;
}

Please note that Clock::advanceOneMinute() has the same problem.

Upvotes: 0

user10957435
user10957435

Reputation:

The problem is that you only look at the seconds variable. You need to check to see if there is overflow. Something like this:

if((seconds +1) >= 60) {
    //advance one minute...
}

You will need to do similar things in the rest of your clock (minutes, hours, etc) too.

Upvotes: 1

Related Questions