Question

In: Computer Science

Complete the definitions for the following prototypes in the Time class: Time(std::string) – a constructor that...

Complete the definitions for the following prototypes in the Time class:

Time(std::string) – a constructor that will take in a string with the format h:m:s and parse the

h, m and s into separate int values that can be used to set the time of a new time object.

int getSecond() const - return the value of the second void setSecond() - set the value of the second, making sure the new second value is valid before changing it.

std::string to24format() - return a string with the time in the format hh:mm:ss

Starting point for lab 3: https://repl.it/@jholst/Time-class-lab-3-starting-point

Solutions

Expert Solution

Code

Time.h

#include <string>

#ifndef TIME_H
#define TIME_H

class Time{

public:
  
// constructors
Time (int = 0, int = 0, int = 0);
Time (std::string); // lab 3 define

// set functions
void setTime(int, int, int);

// get functions
int getHour() const;
int getMinute() const;
int getSecond() const; // lab 3 define

// display functions
std::string to24format() const; // lab 3 define
std::string toAMPMformat() const;

private:

// data members
int hour;
int minute;
int second;

// private helper functions
void setHour(int);
void setMinute(int);
void setSecond(int); // lab 3 define
};


#endif

Time.cpp

#include <string>
#include <sstream>
#include <iostream>
#include <iomanip>
#include "Time.h"

// constructor to take three int values
Time::Time(int hour, int minute, int second){
setTime(hour, minute, second);
}
Time::Time(std::string time)

{
   std::string temp;
   size_t found = time.find(":");
   temp=time.substr(0,found);
   setHour(stoi(temp));

   time=time.substr(found+1);

   found = time.find(":");
   temp=time.substr(0,found);

   setMinute(stoi(temp));

   time=time.substr(found+1);

   setSecond(stoi(time));

}
void Time::setTime(int h, int m, int s){
setHour(h);
setMinute(m);
setSecond(s);
}

void Time::setHour(int h){
if (h >= 0 && h < 24){
hour = h;
}
else{
std::cout << "\nInvalid hour value " << h << "\n";
}
}

void Time::setMinute(int m){
if (m >= 0 && m < 60){
minute = m;
}
else{
std::cout << "\nInvalid minute value " << m << "\n";
}
}
void Time::setSecond(int s)
{
   if (s >= 0 && s < 60){
second = s;
}
else{
std::cout << "\nInvalid minute value " << s << "\n";
}
}
int Time::getHour() const {
return hour;
}

int Time::getMinute() const {
return minute;
}
int Time::getSecond() const
{
   return second;
}
std::string Time::to24format() const
{
   std::ostringstream output;

output << std::setfill('0') << std::setw(2)
<< ((hour == 0 || hour == 12) ? 12 : hour)
<< ":" << std::setw(2) << minute
<< ":" << std::setw(2) << second ;

return output.str();
}
std::string Time::toAMPMformat() const{
std::ostringstream output;

output << std::setfill('0') << std::setw(2)
<< ((hour == 0 || hour == 12) ? 12 : hour % 12)
<< ":" << std::setw(2) << minute
<< ":" << std::setw(2) << second
<< (hour < 12 ? " AM" : " PM");
  
return output.str();

}

Main.cpp

#include <iostream>
#include <sstream>
#include "Time.h"
using namespace std;

void displayTime(const string & message, const Time * time){
cout << message << "\n24 Hour clock: " << time->to24format() << "\nAM/PM format: " << time->toAMPMformat() << "\n\n";
}

int main() {
  
Time time1;
Time time2(11);
Time time3(22, 30);
Time time4(7, 45, 30);

displayTime("Time 1", &time1);
displayTime("Time 2", &time2);
displayTime("Time 3", &time3);
displayTime("Time 4", &time4);

string rawinput;
cout << "Enter time h:m:s = ";
getline(cin, rawinput);
istringstream inputTime(rawinput);
int hour, minute, second;
char c;
inputTime >> hour >> c >> minute >> c >> second;
  
Time newTime(hour, minute, second);
displayTime("New time", &newTime);

// lab 3 addition of constructor taking string
Time newStringTime("5:30:15"); // use new constructor
displayTime("New string time", &newStringTime);

}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.


Related Solutions

C++ programming question class Address { public: Address(const std::string& street, const std::string& city, const std::string& state,...
C++ programming question class Address { public: Address(const std::string& street, const std::string& city, const std::string& state, const std::string& zip) : StreetNumber(street), CityName(city), StateName(state), ZipCode(zip) {} std::string GetStreetNumber() const { return StreetNumber; } void SetStreetNumber(const std::string& street) { StreetNumber = street; } std::string GetCity() const { return CityName; } void SetCity(const std::string& city) { CityName = city; } std::string GetState() const { return StateName; } void SetState(const std::string& state) { StateName = state; } std::string GetZipCode() const { return ZipCode; }...
In C++ 14.22 A dynamic class - party list Complete the Party class with a constructor...
In C++ 14.22 A dynamic class - party list Complete the Party class with a constructor with parameters, a copy constructor, a destructor, and an overloaded assignment operator (=). //main.cpp #include <iostream> using namespace std; #include "Party.h" int main() { return 0; } //party.h #ifndef PARTY_H #define PARTY_H class Party { private: string location; string *attendees; int maxAttendees; int numAttendees;    public: Party(); Party(string l, int num); //Constructor Party(/*parameters*/); //Copy constructor Party& operator=(/*parameters*/); //Add destructor void addAttendee(string name); void changeAttendeeAt(string...
Please write code in java and comment . thanks Item class A constructor, with a String...
Please write code in java and comment . thanks Item class A constructor, with a String parameter representing the name of the item. A name() method and a toString() method, both of which are identical and which return the name of the item. BadAmountException Class It must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it. It must have a default...
JAVA The class will have a constructor BMI(String name, double height, double weight). The class should...
JAVA The class will have a constructor BMI(String name, double height, double weight). The class should have a public instance method, getBMI() that returns a double reflecting the person's BMI (Body Mass Index = weight (kg) / height2 (m2) ). The class should have a public toString() method that returns a String like Fred is 1.9m tall and is 87.0Kg and has a BMI of 24.099722991689752Kg/m^2 (just print the doubles without special formatting). Implement this class (if you wish you...
Create an IceCreamConeException class whose constructor recetves a String that consists of an ice creams cone’s...
Create an IceCreamConeException class whose constructor recetves a String that consists of an ice creams cone’s flavour and the number of scoops Create an IceCreamCone class with two fields - flavor and scoops The IceCreamCone constructor calls two data entry methods — getFlavor() and getScoops() The getScoops() method throws an IceCreamConeException when the scoop quantity exceeds 3 Write a program that establish three TceCreamCone objects and handles the Exception
Create an ApartmentException class whose constructor receives a string that holds a street address, an apartment...
Create an ApartmentException class whose constructor receives a string that holds a street address, an apartment number, a number of bedrooms, and a rent value for an apartment. Upon construction, throw an ApartmentException if any of the following occur: • The apartment number does not consist of 3 digits • The number of bedrooms is less than 1 or more than 4 • The rent is less than $500.00 or over $2500 Write a driver class that demonstrates creating valid...
(JAVA) Referencing the class Oven, complete the Constructor and set method for the instance variable temp....
(JAVA) Referencing the class Oven, complete the Constructor and set method for the instance variable temp. Constructor checks if the temperature is between 0 and 500. If the temperature does not fit the requirement, then set the temperature to be zero. Otherwise, set it to be the value passed to the constructor. Set the power to be off. Set the String to be the color passed to the constructor. Use the same requirement for the constructor for the set temperature...
A constructor, with a String parameter representing the name of the item.
USE JAVA Item classA constructor, with a String parameter representing the name of the item.A name() method and a toString() method, both of which are identical and which return the name of the item.BadAmountException ClassIt must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it.Inventory classA constructor which takes a String parameter indicating the item type, an int parameter indicating the initial...
Class Exercise: Constructor using JAVA Let’s define a Class together and have a constructor while at...
Class Exercise: Constructor using JAVA Let’s define a Class together and have a constructor while at it. - What should the Class object represent? (What is the “real life object” to represent)? - What properties should it have? (let’s hold off on the methods/actions for now – unless necessary for the constructor) - What should happen when a new instance of the Class is created? - Question: What are you allowed to do in the constructor? - Let’s test this...
Complete the following definitions with regards to viral replication: Retrovirus Prophage Eclipse period Burst Time
Complete the following definitions with regards to viral replication: Retrovirus Prophage Eclipse period Burst Time
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT