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

C++ Question: The first image is the assignment given, and the second & third im

ID: 3874208 • Letter: C

Question

C++ Question: The first image is the assignment given, and the second & third images are sample codes for the assignment's solution. Explain in detail, (almost) line-by-line, what is happening in the program, as if you are teaching someone who never programmed before. You are welcome to include comments next to the lines using "//" operater in the program. Included below the images is the program in text form. Remember to explain in FULL detail.

#include <iostream>

using namespace std;

class Seconds {

  

private:

unsigned int inputSeconds;

unsigned int days;

unsigned int hours;

unsigned int minutes;

unsigned int seconds;

  

public:

Seconds(unsigned int sec) {

inputSeconds = sec;

days = 0;

hours = 0;

minutes = 0;

seconds = 0;

}

  

void setSeconds(unsigned int sec) {

inputSeconds = sec;

}

  

unsigned int getInputSeconds() {

return seconds;

}

  

unsigned int getDays() {

return days;

}

  

unsigned int getHours() {

return hours;

}

  

unsigned int getMinutes() {

return minutes;

}

  

unsigned int getSeconds() {

return seconds;

}

  

void convertInputToDayHrMinSec() {

seconds = inputSeconds;

days = seconds / (24 * 60 * 60);

seconds = seconds % (24 * 60 * 60);

hours = seconds / (60 * 60);

seconds = seconds % (60 * 60);

minutes = seconds / 60;

seconds = seconds % 60;

}

  

bool weekCalculation() {

if (inputSeconds > (7 * 24 * 60 * 60))

return false;

  

else

return true;

}

};

int main() {

  

int seconds;

cout << "Enter the amount of seconds: ";

cin >> seconds;

  

while(seconds <= 0) {

cout << "Invalid input. Enter an amount of seconds greater then 0: ";

cin >> seconds;

}

  

Seconds s(seconds);

  

if(!s.weekCalculation()) {

cout << "User entered seconds greater then 7 days." << endl;

}

  

else {

s.convertInputToDayHrMinSec();

  

if(s.getDays() > 0)

cout << "Days: " << s.getDays()<<endl;

  

if(s.getHours() > 0)

cout << "Hours: " << s.getHours()<<endl;

  

if(s.getMinutes() )

cout << "Minutes: " << s.getMinutes()<<endl;

  

if(s.getSeconds() )

cout << "Remaining seconds: " << s.getSeconds() << endl;

}

}

SHORT PROGRAMMING TASKS: :Object Orientated Programming Use Object Oriented Design (OOP), Use Constructors, Parameters, and arguments as needed Have the user input number of seconds. Convert number of seconds to days, hours, minutes, and remaining seconds up to 7 days. If the number of seconds is greater than 7 days do not convert and print a message "user entered seconds greater than 7 days." Create a main function to control the operation of the program. If the user inputs a second amount for conversion less than or equal to 0 the program should prompt the user again for input. Implement setter and getter method Only output days if there are enough seconds for 1 day needed . Days Hours e Minutes » Seconds Only output hours if there are enough seconds for 1 hour Hours e Minutes » Seconds Only output minutes if there are enough seconds for 1 minute Minutes * Seconds Attach Snipping Photo of Source Code and Output below

Explanation / Answer

Now lets us understand what the requirement says and divide the program:

1)   We need to use OOPs

-      This means we will use class

2)   We need input from user – seconds

3)   We need output as day hour minutes and seconds

-      The above two means we will have four data members days, hours, minutes and seconds.

4)   Use constructor, parameters and arguments

-      This means we need a parameterized constructor and this will be used to initialize data members.

5)   Use setter and getter methods

-      This means we will have setter and getter methods for data members to access their value. Now to note here is we will have setter method only for seconds as we are taking input only for seconds and other data members are just getting calculated and we need to get these values.

6)   Need a method to calculate days, hours, minutes, seconds

-      The method will calculate all the values based on the conditions provided to calculate

7)   Need a main function

-      This will ask user for input and also relevant messages and call the method to calculate required values.

So based on the above requirements let’s see what we have in the code.

#include <iostream>

using namespace std;

//Requirement – 1, a class names Seconds has been created. The name of the class could be anything but the standards says a relevant name should be given.

class Seconds {

  

//Requirement 2&3 – declared four data members and provided them access modifier as private which means it could be only accessed within the class or by the object of the class.

private:

unsigned int inputSeconds;

unsigned int days;

unsigned int hours;

unsigned int minutes;

unsigned int seconds;

  

//Requirement 4 – Need a parameterized constructor which will initialize the data members. Now the constructor has the same name as of class hence Seconds and we have a parameter sec which will hold the value passed when an object is created. It has public access modifier so that it could be called anywhere. And as we can see we have got values of seconds as parameter to the function so we are setting the value to data member inputSeconds and all other members to 0(as for all we need to calculate values)

public:

Seconds(unsigned int sec) {

inputSeconds = sec;

days = 0;

hours = 0;

minutes = 0;

seconds = 0;

}

  

//Requirement 5 – Below we have getter for all data members which are returning their values. And we also wrote a setter for inputSeconds as we have input for this from the user. Now as per standards we write the setter and getter with syntax

-      Setter – void setNameofDataMember(), here return type is void as the method doesn’t return anything.

-      Getter – typeofDataMemeber getNameofDataMember, here the method will be of type of value it returns like if its returning an int value then the type of method will be int

void setSeconds(unsigned int sec) {

inputSeconds = sec;

}

  

unsigned int getInputSeconds() {

return seconds;

}

  

unsigned int getDays() {

return days;

}

  

unsigned int getHours() {

return hours;

}

  

unsigned int getMinutes() {

return minutes;

}

  

unsigned int getSeconds() {

return seconds;

}

  

//Requirement 6 – Here we have the function to calculate hours, days, minutes. Now we have some conditions given to when to calculate and display hour, day and minute. We have two options either checks that complete conditions in this method itself or break the logic to main method and one more method. And we have second option here hence calculated the values in below function

void convertInputToDayHrMinSec() {

seconds = inputSeconds;

days = seconds / (24 * 60 * 60);

seconds = seconds % (24 * 60 * 60);

hours = seconds / (60 * 60);

seconds = seconds % (60 * 60);

minutes = seconds / 60;

seconds = seconds % 60;

}

  

//In continuation of requirement 6 we had a condition to check whether user has inputs second greater than 7 days.

bool weekCalculation() {

if (inputSeconds > (7 * 24 * 60 * 60))

return false;

  

else

return true;

}

};

//Requirement 7 – Need a main function to ask user input and display the calculated values.

Now we do so by first asking the input from user and store it in a value as called here seconds.

And let’s see at each line how the code checks other validation logic

int main() {

  

int seconds;

cout << "Enter the amount of seconds: ";

cin >> seconds;

  

//We have a condition to check whether the provided input is less than 0 and if yes prompt a message saying invalid input and ask to re-enter the value. Hence we have a while loop which will loop until we have value of seconds less than 0 and the loop body will keep on prompting the message and asking for input.

while(seconds <= 0) {

cout << "Invalid input. Enter an amount of seconds greater then 0: ";

cin >> seconds;

}

  

//Once we have a valid input call the constructor by creating a object s of class Seconds and pass the input as parameter to it(required as per requirement 4)

Seconds s(seconds);

  

//We need to check the validation if user has entered second exceeding 7 days hence we wrote the if condition with call to method written above to check the logic and this method is defined in class scope we are calling it with using object of the class hence s.weekCalculation

if(!s.weekCalculation()) {

cout << "User entered seconds greater then 7 days." << endl;

}

  

//Now if the input passed the validation we will go for further calculations

else {

//Calling the function to calculate the value again using object

s.convertInputToDayHrMinSec();

  

//Now below if conditions are to check scenarios on when to display values of days, hours, minutes and seconds. And here as the calculated values are data members of the class we are accessing the values using getter methods defined above and again using object s of the class.

if(s.getDays() > 0)

cout << "Days: " << s.getDays()<<endl;

  

if(s.getHours() > 0)

cout << "Hours: " << s.getHours()<<endl;

  

if(s.getMinutes() )

cout << "Minutes: " << s.getMinutes()<<endl;

  

if(s.getSeconds() )

cout << "Remaining seconds: " << s.getSeconds() << endl;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote