Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

11. In Chapter 1, the class clockType was designed and implemented to implement

ID: 3829272 • Letter: 1

Question

11. In Chapter 1, the class clockType was designed and implemented to implement the time of day in a program. This chapter discussed how to overload various operators. Redesign the class clockType by overloading the following operators: the stream insertion << and stream extraction >> operators for input and output, the pre- and postincrement increment operators to increment the time by one second, and the relational operators to compare the two times. Also write a test program to test various operations of the class clockType.

***BELOW IS THE SOURCE CODE FOR THE CLASS AND FUNCTION DEFINITIONS PROVIDED BY THE AUTHOR THUS FAR, THANK YOU FOR YOUR HELP!***

//*************************************************************
// Author: D.S. Malik
//
// class clockType.h
// This class specifies the members to implement time in a
// program.
//*************************************************************

class clockType
{
public:
void setTime(int hours, int minutes, int seconds);
//Function to set the time
//The time is set according to the parameters
//Postcondition: hr = hours; min = minutes; sec = seconds
// The function checks whether the values of hours,
// minutes, and seconds are valid. If a value is invalid,
// the default value 0 is assigned.

void getTime(int& hours, int& minutes, int& seconds) const;
//Function to return the time
//Postcondition: hours = hr; minutes = min; seconds = sec

void printTime() const;
//Function to print the time
//Postcondition: Time is printed in the form hh:mm:ss.

void incrementSeconds();
//Function to increment the time by one second
//Postcondition: The time is incremented by one second.
// If the before-increment time is 23:59:59, the time
// is reset to 00:00:00.

void incrementMinutes();
//Function to increment the time by one minute
//Postcondition: The time is incremented by one minute.
// If the before-increment time is 23:59:53, the time
// is reset to 00:00:53.

void incrementHours();
//Function to increment the time by one hour
//Postcondition: The time is incremented by one hour.
// If the before-increment time is 23:45:53,   the time
// is reset to 00:45:53.

bool equalTime(const clockType& otherClock) const;
//Function to compare the two times
//Postcondition: Returns true if this time is equal to
// otherClock; otherwise, returns false

clockType(int hours, int minutes, int seconds);
//Constructor with parameters
//The time is set according to the parameters.
//Postconditions: hr = hours; min = minutes; sec = seconds
// The constructor checks whether the values of hours,
// minutes, and seconds are valid. If a value is invalid,
// the default value 0 is assigned.

clockType();
//Default constructor with parameters
//The time is set to 00:00:00.
//Postcondition: hr = 0; min = 0; sec = 0


private:
int hr; //variable to store the hours
int min; //variable to store the minutes
int sec; //variable to store the seconds
};


//**********************************************************
// Author: D.S. Malik
//
// Implementation file clockTypeImp.cpp
// This file contains the definitions of the functions to
// implement the operations of the classes clockType.
//**********************************************************
  
#include <iostream>
#include "clockType.h"

using namespace std;

void clockType::setTime(int hours, int minutes, int seconds)
{
   if (0 <= hours && hours < 24)
   hr = hours;
   else
   hr = 0;

   if (0 <= minutes && minutes < 60)
   min = minutes;
   else
   min = 0;

   if (0 <= seconds && seconds < 60)
   sec = seconds;
   else
   sec = 0;
}

void clockType::getTime(int& hours, int& minutes, int& seconds) const
{
   hours = hr;
   minutes = min;
   seconds = sec;
}

void clockType::incrementHours()
{
   hr++;
   if(hr > 23)
    hr = 0;
}

void clockType::incrementMinutes()
{
   min++;
   if (min > 59)
   {
   min = 0;
   incrementHours();
   }
}

void clockType::incrementSeconds()
{
   sec++;

   if (sec > 59)
   {
   sec = 0;
   incrementMinutes();
   }
}

void clockType::printTime() const
{
   if (hr < 10)
   cout << "0";
   cout << hr << ":";

   if (min < 10)
   cout << "0";
   cout << min << ":";

   if (sec < 10)
   cout << "0";
   cout << sec;
}

bool clockType::equalTime(const clockType& otherClock) const
{
   return (hr == otherClock.hr
       && min == otherClock.min
       && sec == otherClock.sec);
}

clockType::clockType(int hours, int minutes, int seconds)
{
if (0 <= hours && hours < 24)
hr = hours;
else
hr = 0;

if (0 <= minutes && minutes < 60)
min = minutes;
else
min = 0;

if (0 <= seconds && seconds < 60)
sec = seconds;
else
sec = 0;
}

clockType::clockType() //default constructor
{
hr = 0;
min = 0;
sec = 0;
}

Explanation / Answer

public class DayTime {`enter code here`
private int hour;
private int minute;
private int second;
private String time;
private static final String TIME_LONG_FORMAT = "([01]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]";
private static final String TIME_SHORT_FORMAT = "([01]?[0-9]|2[0-3]):[0-5][0-9]";
private DayTime(Builder timeBuilder) {
this.hour = timeBuilder.hour;
this.minute = timeBuilder.minute;
this.second = timeBuilder.second;
this.time = timeBuilder. time;
}
public int getHour() {
return hour;
}
public int getMinute() {
return minute;
}
public int getSecond() {
return second;
}
@Override
public String toString() {
return time;
}
public static class Builder {
private int hour = 0;
private int minute = 0;
private int second = 0;
private String time;
public Builder(String time) {
this.time = time;
}
public DayTime createTime() {
String[] timeUnits = time.split(":");
if(Pattern.compile(TIME_SHORT_FORMAT).matcher(time).matches()) {
this.hour = Integer.parseInt(timeUnits[0]);
this.minute = Integer.parseInt(timeUnits[1]);
} else if(Pattern.compile(TIME_LONG_FORMAT).matcher(time).matches()) {
this.hour = Integer.parseInt(timeUnits[0]);
this.minute = Integer.parseInt(timeUnits[1]);
this.second = Integer.parseInt(timeUnits[2]);
} else {
throw new IllegalArgumentException("Invalid time format" +
" (Expected format: 'HH:mm' or 'HH:mm:ss').");
}
return new DayTime(this);
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote