Question

In: Computer Science

A. Define and implement the class Alarm as follows: The class Alarm consists of two private...

A. Define and implement the class Alarm as follows: The class Alarm consists of two private member variables: description of type string and atime of type Time. The class Alarm also includes the following public member functions:

1. print to print out the alarm’s description and hour, minute, and second of the alarm time.

2. setDescription to accept a string argument and use it to set the description member variable.

3. setAtime to accept three integers (for hour, minute, and second) to set the atime member variable (alarm time).

4. getDescription to return the value of the description member variable.

5. getAtime to return the atime member as a Time object.

6. A default constructor that initializes the description to the string “None” and the alarm time to 00:00:00.

7. An overloaded constructor that accepts a string argument for the alarm description and three int arguments (as the hour, minute, and second) and use them to initialize the description and atime member variables of an Alarm object.

8. An overloaded constructor that accepts a string argument for the alarm description and a Time object and use them to initialize the description and atime member variables of an Alarm object.

9. An equals function to compare two Alarm objects’ values. Return true if both objects have the same values of the member variables. Otherwise, return false. B. In the function main, write statements to declare Alarm class objects and test your class implementation.

Your project is expected to contain five source files: Time.h, Time.cpp, Alarm.h, Alarm.cpp, Lab8.cpp.

Solutions

Expert Solution

Following c++ application demonstrates the use Time , Alarm classes.

Time class has hrs, min, sec.
Alarm class has alarm description, hrs, min, sec.

Instantiates alarm objects with different inputs.
Checks two alarm objects , whether they are equal or not.


PFB source code with sample output.

------------------
Time.h
------------------
#include<iostream>

#ifndef Time_H   
#define Time_H

using namespace std;

class Time {

private:
int hrs, min, sec;

public:
Time();
Time(int hrs, int min, int sec);
  
int getHrs();
void setHrs(int hrs);
int getMin();
void setMin(int min);
int getSec();
void setSec(int sec);
};

#endif
------------------

------------------
Time.cpp
------------------
#include<iostream>
#include"Time.h"

//default constructor
Time::Time(){
hrs = min = sec =0;
}

//Instantiate time with hrs, min, sec provided
Time::Time(int hrs, int min, int sec){
  
this->hrs = hrs;
this->min = min;
this->sec = sec;
}

//geet hrs
int Time::getHrs(){
return hrs;
}

//set hrs
void Time::setHrs(int hrs){
this->hrs = hrs;
}

//get min
int Time::getMin(){
return min;
}

//set min
void Time::setMin(int min){
this->min = min;
}

//get sec
int Time::getSec(){
return sec;
}

//set sec
void Time::setSec(int sec){
this->sec = sec;
}
------------------

------------------
Alarm.h
------------------
#include<iostream>
#include<string>
#include "Time.h"

#ifndef Alarm_H   
#define Alarm_H

using namespace std;

class Alarm : public Time{

private:

string description;
  
public:

Alarm();
Alarm(string description, int hrs, int min, int sec);
Alarm(string description, Time alarmTime);
void printAlarm();

void setDescription(string description);
void setAtime(int hrs, int min, int sec);

string getDescription();

Time getAtime();
bool equals(Time alarmTime);

};
#endif
------------------

------------------
Alarm.cpp
------------------
#include<iostream>
#include<string>
#include "Time.h"
#include "Alarm.h"

//default constructor
Alarm::Alarm(){
description = "None";
Time();
}

//instantiate alarm class with description, hrs, min, sec
Alarm::Alarm(string description, int hrs, int min, int sec){
this->description = description;
Time(hrs, min, sec);
}

//instantiate alarm class with description and Time instance
Alarm::Alarm(string description, Time alarmTime){
  
this->description = description;
this->setHrs(alarmTime.getHrs());
this->setMin(alarmTime.getMin());
this->setSec(alarmTime.getSec());
}

//prints alarm details
void Alarm::printAlarm(){

cout<<endl<<"Alarm's description : "<<description;
cout<<endl<<"Alarm's time : "<<this->getHrs()<<":"<<this->getMin()<<":"<<this->getSec()<<endl;
}

//set alarm description
void Alarm::setDescription(string description){
this->description = description;
}

//set time with hrs, min, sec
void Alarm::setAtime(int hrs, int min, int sec){
this->setHrs(hrs);
this->setMin(min);
this->setSec(sec);
}

//get alarm description
string Alarm::getDescription(){
return description;
}

//get time instance
Time Alarm::getAtime(){
return Time(this->getHrs(), this->getMin(), this->getSec());

}

//check for equal time
bool Alarm::equals(Time alarmTime){

if((this->getHrs()==alarmTime.getHrs()) && (this->getMin()==alarmTime.getMin()) && (this->getSec()==alarmTime.getSec())){
return true;
}else{
return false;
}
}

------------------

------------------
Lab8.cpp
------------------
#include <iostream>
#include "Alarm.h"
#include "Time.h"

using namespace std;

int main(){

//alarm1 object
Alarm alarm1 = Alarm("morning alarm", 5, 0, 0);

cout<<"Alarm1 :"<<endl;
//print alarm1
alarm1.printAlarm();

//time object
Time alarm2Time = Time(17,0,0);

Alarm alarm2 = Alarm("evening snacks time", alarm2Time);
cout<<"Alarm2 :"<<endl;
  
//print object2
alarm2.printAlarm();

//check of both above defined alarm objects are equal
if(alarm1.equals(alarm2)){
cout<<endl<<"Both alarms are same";
}else{
cout<<endl<<"Both alarms are different";
}

return 0;
}
------------------

------------------
sample output
------------------

------------------

Let me know , if you face any issue.


Related Solutions

A. Define and implement the class Alarm as follows: The class Alarm consists of two private...
A. Define and implement the class Alarm as follows: The class Alarm consists of two private member variables: description of type string and atime of type Time. The class Alarm also includes the following public member functions: 1. print to print out the alarm’s description and hour, minute, and second of the alarm time. 2. setDescription to accept a string argument and use it to set the description member variable. 3. setAtime to accept three integers (for hour, minute, and...
1. Define a class counterType to implement a counter. Your class must have a private data...
1. Define a class counterType to implement a counter. Your class must have a private data member counter of type int and functions to set counter to the value specified by the user, initialize counter to 0, retrieve the value of counter, and increment and decrement counter by one. The value of counter must be nonnegative. 2. Some of the characteristics of a book are the title, author(s), publisher, ISBN, price, and year of publication. Design a class bookType that...
Define a JAVA class ISP. The class consists of the name of the subscription plan and...
Define a JAVA class ISP. The class consists of the name of the subscription plan and a method that display the plan. Derive a class DPlan from ISP. The DPlan will charge RM10 per Mbps subscribe and RM0.20 per GB used. Derive a class MPlan from ISP. The MPlan will charge RM5 per Mbps subscribe and RM0.80 per GB used. Display the plan and select the best plan.
1. Implement the Vehicle class.  Add the following private instance variables to the Accoun class:...
1. Implement the Vehicle class.  Add the following private instance variables to the Accoun class: • An instance variable called wheelsCount of type int • An instance variable called vType of type String • An instance variable called isTruck of type boolean .  Add getters and setters for the three instance variables  Add the following methods to the Account class: • A void method called initialize that takes a parameter of type int, a String,and one double...
Programming Exercise Implement the following class design: class Tune { private:    string title; public:   ...
Programming Exercise Implement the following class design: class Tune { private:    string title; public:    Tune();    Tune( const string &n );      const string & get_title() const; }; class Music_collection { private: int number; // the number of tunes actually in the collection int max; // the number of tunes the collection will ever be able to hold Tune *collection; // a dynamic array of Tunes: "Music_collection has-many Tunes" public: // default value of max is a conservative...
public class OperationsBetween { // You must define the following: // 1.) Two private instance variables,...
public class OperationsBetween { // You must define the following: // 1.) Two private instance variables, min and max   // 2.) A constructor which takes initial values for // min and max // 3.) An instance method named sum, which sums the // values between min and max and returns the // result. For example, if min = 3 and max = 5, // then this should return 12 (3 + 4 + 5). If // min is greater than...
public class OperationsBetween { // You must define the following: // 1.) Two private instance variables,...
public class OperationsBetween { // You must define the following: // 1.) Two private instance variables, min and max // // 2.) A constructor which takes initial values for // min and max // // 3.) An instance method named sum, which sums the // values between min and max and returns the // result. For example, if min = 3 and max = 5, // then this should return 12 (3 + 4 + 5). If // min is...
Create a class named GameCharacter to define an object as follows: The class should contain class...
Create a class named GameCharacter to define an object as follows: The class should contain class variables for charName (a string to store the character's name), charType (a string to store the character's type), charHealth (an int to store the character's health rating), and charScore (an int to store the character's current score). Provide a constructor with parameters for the name, type, health and score and include code to assign the received values to the four class variables. Provide a...
c++ programming 1.1 Class definition Define a class bankAccount to implement the basic properties of a...
c++ programming 1.1 Class definition Define a class bankAccount to implement the basic properties of a bank account. An object of this class should store the following data:  Account holder’s name (string)  Account number (int)  Account type (string, check/savings/business)  Balance (double)  Interest rate (double) – store interest rate as a decimal number.  Add appropriate member functions to manipulate an object. Use a static member in the class to automatically assign account numbers. 1.2 Implement...
Define the class HotelRoom. The class has the following private data members: the room number (an...
Define the class HotelRoom. The class has the following private data members: the room number (an integer) and daily rate (a double). Include a default constructor as well as a constructor with two parameters to initialize the room number and the room’s daily rate. The class should have get/set functions for all its private data members [20pts]. The constructors and the get/set functions must throw an invalid_argument exception if either one of the parameter values are negative. The exception handler...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT