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

#include <iostream> #include <string> #include <vector> using namespace std; class Song{ public: Song(); //default constructor Song(string...
#include <iostream> #include <string> #include <vector> using namespace std; class Song{ public: Song(); //default constructor Song(string t, string a, double d); //parametrized constructor string getTitle()const; // return title string getAuthor()const; // return author double getDurationMin() const; // return duration in minutes double getDurationSec() const; // return song's duration in seconds void setTitle(string t); //set title to t void setAuthor(string a); //set author to a void setDurationMin(double d); //set durationMin to d private: string title; //title of the song string author;...
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...
Array based application #include <iostream> #include <string> #include <fstream> using namespace std; // Function prototypes void...
Array based application #include <iostream> #include <string> #include <fstream> using namespace std; // Function prototypes void selectionSort(string [], int); void displayArray(string [], int); void readNames(string[], int); int main() {    const int NUM_NAMES = 20;    string names[NUM_NAMES];    // Read the names from the file.    readNames(names, NUM_NAMES);    // Display the unsorted array.    cout << "Here are the unsorted names:\n";    cout << "--------------------------\n";    displayArray(names, NUM_NAMES);    // Sort the array.    selectionSort(names, NUM_NAMES);    //...
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...
Create a class named BankAccount, containing: a constructor accepting a String corresponding to the name of...
Create a class named BankAccount, containing: a constructor accepting a String corresponding to the name of the account holder. a method, getBalance, that returns a double corresponding to the account balance. a method withdraw that accepts a double, and deducts the amount from the account balance. Write a class definition for a subclass, CheckingAccount, that contains: a boolean instance variable, overdraft. (Having overdraft for a checking account allows one to write checks larger than the current balance). a constructor that...
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...
Constructor: -empty body Class method 1: goodFriend - return string value, "I like you!" Class method...
Constructor: -empty body Class method 1: goodFriend - return string value, "I like you!" Class method 2: badFriend - return string value, "I don't like you!" Main Method: - create new Friend object - call goodFriend and badFriend using Friend object /* class header */ { /* constructor method header */ { } public static void main (String [] args) { Friend f = /* new Friend object */ // call goodFriend using Friend object // call badFriend using Friend...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT