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

COSC 1430 - Homework 4: Classes 1- Objective Construction of a class with variou

ID: 3724372 • Letter: C

Question

COSC 1430 - Homework 4: Classes 1- Objective Construction of a class with various features, paired with the use of a dynamic array 2 - Problem For this assignment, you will need to turn in 3 files: main.cpp, class.h, and class.cpp. These should be turned in via the server in the same directory named hw4. Your hw4 directory will be copied at 11:59 PM the night the assignment is due. Anything not in the directory at that time will not be accepted 2.1 - Class You will need to make a class named stopwatch with the attributes hour, minute, and second. You need to declare them as private members. Your class will need to contain two constructors, a default that will set the classes attributes to 0, and a constructor that takes three arguments to se t hour, minute and second. 2.2 - Main Program should ask the user how many times they wish to enter and then create a dynamic Your main program array based on that input. The user should then be asked to enter the data for the time objects that will be stored in the array. If the user enters a value outside of the ranges for one or more of the attributes (i.e. negative for hours, negative or>60 values for minutes or seconds), the default constructor should be called. Once the array has been completed, the user should be presented with a menu with three options: 1) Print the Individual Stored Times, 2) Print the Total Time, 3) Exit the Program. In order to perform the first two menu functions you may need to expand your class beyond the earlier mentioned constructors. Specifically, to implement 1), add a new member function, called print time) to print out the time in the format of hh:mm:ss (see the following example output for a demonstration). To implement 2), you need to implement a loop to go over all objects in your dynamic array and sum their

Explanation / Answer

First, we will define the class definition in the header file - "StopWatch.h". This will include the declarations of the variables and helper functions of this class.

Then we will include this as the header file for "StopWatch.cpp". This will include the definitions of all the helper functions and the constructors.

Finally, we have the main file where the user will interact with the program. Here also we will include the header file "StopWatch.h" to get access to the class StopWatch and its functionality.

C++ Code for "StopWatch.h"

#ifndef STOPWATCH_H
#define STOPWATCH_H

class StopWatch
{
private:
int hour;
int minute;
int second;
public:
StopWatch(); // Default constructor
StopWatch(int a, int b, int c);
int getHour(); // Getter functions for private variables
int getMinute();
int getSecond();
void printTime();
};

#endif // STOPWATCH_H

C++ Code for "StopWatch.cpp"

#include <iostream>
#include <iomanip>
#include "StopWatch.h"
using namespace std;

StopWatch :: StopWatch() : hour(0), minute(0), second(0) {} // Default constructor

// Constructor with parameters

StopWatch :: StopWatch(int a, int b, int c) : hour(a), minute(b), second(c) {}

int StopWatch :: getHour() {
return hour;
}

int StopWatch :: getMinute() {
return minute;
}

int StopWatch :: getSecond() {
return second;
}

void StopWatch :: printTime() {
cout << setw(2) << setfill('0') << hour << ":" << setw(2) << setfill('0') << minute << ":" << setw(2) << setfill('0') << second << " ";
} // setw and setfill will fill the extra spaces with '0's

C++ Code for the main program

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
#include "StopWatch.h"

// Function to print the sum of all times

void print_sum(int h, int m, int s) {
cout << "Total Time - " << setw(2) << setfill('0') << h << ":" << setw(2) << setfill('0') << m << ":" << setw(2) << setfill('0') << s << " ";
}

// Function to calculate the sum

void calculate_sum(StopWatch* arr, int n) {
int carry_sec = 0;
int carry_min = 0;
int h = 0, m = 0, s = 0; // Initialized all sums to 0
for(int i = 0; i < n; i++) {
s += arr[i].getSecond();
if(s > 59) { // Out of range value
carry_sec = s / 60;
s = s % 60;
}
else carry_sec = 0;

m += arr[i].getMinute() + carry_sec;
if(m > 59) {
carry_min = m / 60;
m = m % 60;
}
else carry_min = 0;

h += arr[i].getHour() + carry_min;
}
print_sum(h, m, s); // Call print_sum to print the total time
}

int main()
{
int n;
cout << "Please enter how many values would you like to store: " << endl;
cin >> n;
StopWatch* arr = new StopWatch[n]; // Dynamic array
for(int i = 0; i < n; i++) {
int a, b, c;
string inp1, inp2, inp3;
cout << "Please enter hour: ";
cin >> inp1;
a = stoi(inp1);
cout << "Please enter minute: ";
cin >> inp2;
b = stoi(inp2);
cout << "Please enter second: ";
cin >> inp3;
c = stoi(inp3);

if(a < 0 || b < 0 || b > 59 || c < 0 || c > 59)
arr[i] = StopWatch(); // Call default constructor if out of range values
else
arr[i] = StopWatch(a, b, c); // Parameterized constructor call
}
int choice = 1;
while(choice != 0) {
cout << "1) Print Times 2) Print Total Time 0) Exit ";
cout << "Please enter your selection: ";
cin >> choice;
if(choice == 0){
cout << "Terminating.. ";
return 0;
}
if(choice == 1) {
for(int i = 0; i < n; i++) {
cout << "Time " << i + 1 << " - ";
arr[i].printTime(); // Print values of dynamic array
}
}
else if(choice == 2) {
calculate_sum(arr, n); // Function to calculate and print sum
}
}

return 0;
}