Question

In: Computer Science

I need this code in C++ form using visual studios please: Create a class that simulates...

I need this code in C++ form using visual studios please:

Create a class that simulates an alarm clock. In this class you should:

•       Store time in hours, minutes, and seconds. Note if time is AM or PM. (Hint: You should have separate private members for the alarm and the clock. Do not forget to have a character variable representing AM or PM.)

•       Initialize the clock to a specified time.

•       Allow the clock to increment to the next second. (Hint: You need to take into account things like if the clock's time is 11:59:59 AM and you increment by a second, the time will be

12:00:00 PM. You may need to consider some iterated if statements.)

•       Set the alarm and have the alarm print out "WAKE UP" when the set time is reached. (Hint: You may wish to create a private function that provides the wished-for printout when the alarm time is reached and the alarm clock is on.)     Display the present time.

•       Use the class in a program that uses the functions requiring displaying of time and setting of the alarm.

Include 2 constructors. One constructor should be the default constructor that will initialize the object to 12:00:00 AM. The second constructor should take parameters for hours, minutes, seconds, and AM/PM. Both constructors will provide the private members with the time. In addition, have both constructors set the alarm clock as off. (You will need a Boolean variable that determines whether the alarm is on or off). The function or method you use to set the alarm will set the alarm on.

Solutions

Expert Solution

#include <iostream>
#include <stdexcept>
using namespace std;


class AlarmClock {
public:
// Only included one constructor, since it does both the jobs that were required in the question
AlarmClock(int h = 12, int m = 0, int s = 0, bool am = true) {
    setAm(am);
    setHours(h);
    setMins(m);
    setSecs(s);
  
}

// Make an operator to allow incrementing the time by one second.
void operator++(int s) {
    if(secs != 59)
      ++secs;
    else {
      secs = 0;
      if(mins != 59)
   ++mins;
      else {
   mins = 0;
   setHours(hours + 1);
      }
    }

    checkAlarm();
}

// Check if it is time to set off the alarm
void checkAlarm() {
    if(!aon)
      return;
    if(hours == ahours && mins == amins && secs == asecs)
      cout << "WAKE UP!!!" << endl;
}

void setHours(int h) {
    if(h < 0)
      throw invalid_argument("Can't set the hours to less than 0");
    if(0 == h) {
      hours = 12;
      alternateAm();
    } else if(h < 12) {
      hours = h;
    } else if(12 == h) {
      hours = h;
      alternateAm();
    } else if(h < 24) {
      hours = h-12;
      alternateAm();
    } else
      throw invalid_argument("Can't set the hour to greater than 24");
  
}

int getHours() {
    return hours;
}

void setMins(int m) {
    if(m < 0)
      throw invalid_argument("Can't set the minute to less than 0");
    else if(m < 60) {
      mins = m;
    } else
      throw invalid_argument("Can't set the minute to greater than 59");
}

int getMins() {
    return mins;
}

void setSecs(int s) {
    if(s < 0)
      throw invalid_argument("Can't set the second to less than 0");
    else if(s < 60) {
      secs = s;
    } else
      throw invalid_argument("Can't set the second to greater than 59");
}

int getSecs() {
    return secs;
}

void setAm(bool am) {
    isam = am;
}

bool getAm() {
    return isam;
}

void alternateAm() {
    if(isam)
      isam = false;
    else
      isam = true;
}

void setAlarm(int h, int m, int s, bool am) {
    asetAm(am);
    asetHours(h);
    asetMins(m);
    asetSecs(s);
    aon = true;
}

// Now a set of functions for setting up the alarm's time

void asetHours(int h) {
    if(h < 0)
      throw invalid_argument("Can't set the hours to less than 0");
    if(0 == h) {
      ahours = 12;
      aalternateAm();
    } else if(h < 12) {
      ahours = h;
    } else if(h < 24) {
      ahours = h-12;
      aalternateAm();
    } else
      throw invalid_argument("Can't set the hour to greater than 24");
}

void aalternateAm() {
    if(aisam)
      aisam = false;
    else
      aisam = true;
}

int agetHours() {
    return ahours;
}

void asetMins(int m) {
    if(m < 0)
      throw invalid_argument("Can't set the minute to less than 0");
    else if(m < 60) {
      amins = m;
    } else
      throw invalid_argument("Can't set the minute to greater than 59");
}

int agetMins() {
    return amins;
}

void asetSecs(int s) {
    if(s < 0)
      throw invalid_argument("Can't set the second to less than 0");
    else if(s < 60) {
      asecs = s;
    } else
      throw invalid_argument("Can't set the second to greater than 59");
}

int agetSecs() {
    return asecs;
}

void asetAm(bool am) {
    aisam = am;
}

bool agetAm() {
    return aisam;
}

void printTime() {
    cout << "Hours:" << hours
   << " Mins:" << mins
   << " Secs:" << secs
   << " AM:" << isam << endl;
}
private:
int hours; // Current time - hours
int mins; // Current time - minutes
int secs; // Current time - seconds
bool isam; // Current time - AM/PM
bool aon; // Alarm - On/Off
int ahours; // Alarm - hours
int amins; // Alarm - minutes
int asecs; // Alarm - seconds
bool aisam; // Alarm - AM/PM
}; // End class AlarmClock

int main() {
AlarmClock a(11, 59, 57, true), b(4, 15, 50, true), c(15) ;
int counter;

a.setAlarm(12,1,30,false);
cout << "Time is: ";
while(counter != 0) {
    a.printTime();
    cout << "Enter 0 to quit. How much do you want to increment the time by, in seconds? ";
    cin >> counter;
  
    for(int i = 0; i < counter; i++) {
      a++;
    }
}

}



Related Solutions

Your task is to create a book ordering form using VISUAL STUDIOS, with the code and...
Your task is to create a book ordering form using VISUAL STUDIOS, with the code and screenshots 1. Boxes for first name, last name, address. 2. Radio buttons to select: hard cover, soft cover, ebook. 3. Drop down list to select the book (make up three or four book names). 4. Radio buttons to select credit card (at least Visa, Master Card, American Express). 5. Box to enter credit card numbers. 6. The credit card box MUST verify that numbers...
I need the code for following in C++ working for Visual studio please. Thanks Use a...
I need the code for following in C++ working for Visual studio please. Thanks Use a Struct to create a structure for a Player. The Player will have the following data that it needs maintain: Struct Player int health int level string playerName double gameComplete bool isGodMode Create the 2 functions that will do the following: 1) initialize(string aPlayerName) which takes in a playername string and creates a Player struct health= 100 level= 1 playerName = aPlayerName gameComplete = 0...
Code should be written in C++ using Visual Studios Community This requires several classes to interact...
Code should be written in C++ using Visual Studios Community This requires several classes to interact with each other. Two class aggregations are formed. The program will simulate a police officer giving out tickets for parked cars whose meters have expired. You must include both a header file and an implementation file for each class. Car class (include Car.h and Car.cpp) Contains the information about a car. Contains data members for the following String make String model String color String...
C# Programming language!!! Using visual studios if possible!! PrimeHealth Suite You will create an application that...
C# Programming language!!! Using visual studios if possible!! PrimeHealth Suite You will create an application that serves as a healthcare billing management system. This application is a multiform project (Chapter 9) with three buttons. The "All Accounts" button allows the user to see all the account information for each patient which is stored in an array of class type objects (Chapter 9, section 9.4).The "Charge Service" button calls the Debit method which charges the selected service to the patient's account...
In c# I need to create a simple payroll management system using visual basic and GUI....
In c# I need to create a simple payroll management system using visual basic and GUI. I also need to connect to SQL database. It needs a log in screen, inside the login screen it needs another screen to enter or edit employee information. It needs somewhere to enter hours worked for that specific employee, and another screen that can access reports.
Using C# Windows App Form Create a simple calculator using +,-,*,/ Please show code GUI Code...
Using C# Windows App Form Create a simple calculator using +,-,*,/ Please show code GUI Code for calculator menus radio button input text boxes
can someone code this problem please? Introduction Students will create a C++ program that simulates a...
can someone code this problem please? Introduction Students will create a C++ program that simulates a Pokemon battle mainly with the usage of a while loop, classes, structs, functions, pass by reference and arrays. Students will be expected to create the player’s pokemon and enemy pokemon using object-oriented programming (OOP). Scenario You’ve been assigned the role of creating a Pokemon fight simulator between a player’s Pikachu and the enemy CPU’s Mewtwo. You need to create a simple Pokemon battle simulation...
Program is to be written in C++ using Visual studios Community You are to design a...
Program is to be written in C++ using Visual studios Community You are to design a system to keep track of either a CD or DVD/Blu-ray collection. The program will only work exclusively with either CDs or DVDs/Blu-rays since some of the data is different. Which item your program will work with will be up to you. Each CD/DVD/Blu-ray in the collection will be represented as a class, so there will be one class that is the CD/DVD/Blu-ray. The CD...
In C++ Create a class that simulates a school calendar for a course and has a...
In C++ Create a class that simulates a school calendar for a course and has a warner that provides the school administration with the option of warning students when the last day is that a student is permitted to drop the course. (Allow administrators to warn or not, as they wish. Do not make this a required function.) You will assume in this calendar that there are 12 months in a year, 4 weeks in a month, and 7 days...
C++ PROGRAM Using the attached C++ code (Visual Studio project), 1) implement a CoffeeMakerFactory class that...
C++ PROGRAM Using the attached C++ code (Visual Studio project), 1) implement a CoffeeMakerFactory class that prompts the user to select a type of coffee she likes and 2) returns the object of what she selected to the console. #include "stdafx.h" #include <iostream> using namespace std; // Product from which the concrete products will inherit from class Coffee { protected:    char _type[15]; public:    Coffee()    {    }    char *getType()    {        return _type;   ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT