Question

In: Computer Science

How would I write this driver program in C++?? We just started learning C++ and I...

How would I write this driver program in C++??

We just started learning C++ and I still don't understand how to use classes or randomly generate numbers, and I'm struggling to complete this assignment. The premise of the whole assignment is to randomly generate dates within a range and format the dates in 3 different ways.

Calendar.cpp
Create a driver program with a main function. Use symbolic constants or "const" declarations here, also.
In particular, define local constant SIZE to be a constant with value 5.
In the main function, create an array of Date objects of size SIZE. Use a loop to set specific dates for all Date objects in the array. In each iteration of the loop do the following:
1) Generate a year between 1950 (inclusive) and 2049 (inclusive) using random number generations (generate year as a random number between 1950 and 2049 inclusive). Use constants Date:: MIN_YEAR and Date:: MAX_YEAR instead of numbers.
2) Prompt the user to enter month and day in the generated year. Then, call the mutators to set those data members.
Print the elements of the array in the US format mm/dd/yyyy.
Print the elements of the array again in the format m/d/yy.
Print the elements of the array again in the standard International format YYYY-MM-DD.

Add calls to the other member functions and constructors as needed to test them thoroughly.
Also, add calls with invalid arguments placed in try / catch blocks to test that invalid_argument exception is thrown for all functions that could throw exceptions. When checking for a value that is outside of a specified range, e.g., less than MIN value or greater than MAX values, make sure to have separate tests for the case when the value is less than MIN and when the values is greater than MAX.

CODE FOR THE PROGRAMS THAT CONTAIN THE METHODS

********************************* code for Date.h *****************************

class Date

{

private:

    unsigned int day;

    unsigned int month;

    unsigned int year;

public:

    static const int MIN_DAY = 1;

    static const int MAX_DAY = 12;

    static const int MIN_MONTH = 1;

    static const int MAX_MONTH = 31;

    static const int MIN_YEAR = 1950;

    static const int MAX_YEAR = 2049;

public:

    // default constructor

    Date();

    // parameterised constructor

    Date(int, int, int);

    // creating mutators

    void SetDay(unsigned int);

    void SetMonth(unsigned int);

    void SetYear(unsigned int);

    // creatinng accessors

    unsigned int getDay();

    unsigned int getMonth();

    unsigned int getYear();

    // methods for printing date format

    void printUS();

    void printShort();

    void printInternational();

};

*************************** end of Date.h ********************************

***************************** code for Date.cpp ****************************

#include<iostream>

#include<iomanip>

#include "Date.h"

using namespace std;

Date::Date()

{

    day = MIN_DAY;

    month = MIN_MONTH;

    year = MIN_YEAR;

}

Date::Date(int m, int d, int y){

    day = d;

    month = m;

    year = y;

}

void Date::SetDay(unsigned int d){

    day = d;

}

void Date::SetMonth(unsigned int m){

    month = m;

}

void Date::SetYear(unsigned int y){

    year = y;

}

unsigned int Date::getDay(){

    return day;

}

unsigned int Date::getMonth(){

    return month;

}

unsigned int Date::getYear(){

    return year;

}

void Date::printUS(){

    cout.fill(0);

    cout<<setw(2)<<getMonth()<<"/";

    cout<<setw(2)<<getDay()<<"/";

    cout<<setw(4)<<getYear();

}

void Date::printShort(){

    cout.fill(0);

    cout<<getMonth()<<"/";

    cout<<getDay()<<"/";

    cout<<setw(2)<<getYear();

}

void Date::printInternational(){

    cout.fill(0);

    cout<<setw(4)<<getYear()<<"-";

    cout<<setw(2)<<getMonth()<<"-";

    cout<<setw(2)<<getDay();

}

Solutions

Expert Solution

Program: In this program, we have implemented Date class and its methods, inside the Calender.cpp we test out date class by creating an array of Date class objects, we generate the year randomly in the range [1950,2049] and take user inputs for month and day. We validate the input given by user using try catch blocks and then print the output.

Date.h:

Code:

#ifndef DATE_H
#define DATE_H

class Date

{

private:

    unsigned int day;

    unsigned int month;

    unsigned int year;

public:

    static const int MIN_DAY = 1;

    static const int MAX_DAY = 31;

    static const int MIN_MONTH = 1;

    static const int MAX_MONTH = 12;

    static const int MIN_YEAR = 1950;

    static const int MAX_YEAR = 2049;

public:

    // default constructor

    Date();

    // parameterized constructor

    Date(int, int, int);

    // creating mutators

    void SetDay(unsigned int);

    void SetMonth(unsigned int);

    void SetYear(unsigned int);

    // creating accessors

    unsigned int getDay();

    unsigned int getMonth();

    unsigned int getYear();

    // methods for printing date format

    void printUS();

    void printShort();

    void printInternational();

};

#endif // DATE_H

Date.cpp:

Code:

#include<iostream>
#include<iomanip>
#include "Date.h"

using namespace std;

// Constructor
Date::Date()
{

    day = MIN_DAY;

    month = MIN_MONTH;

    year = MIN_YEAR;

}

// Parametrized constructor
Date::Date(int m, int d, int y){

    day = d;

    month = m;

    year = y;

}


// Mutators
void Date::SetDay(unsigned int d){

    day = d;

}

void Date::SetMonth(unsigned int m){

    month = m;

}

void Date::SetYear(unsigned int y){

    year = y;

}

// Accessors
unsigned int Date::getDay(){

    return day;

}

unsigned int Date::getMonth(){

    return month;

}

unsigned int Date::getYear(){

    return year;

}

// US format
void Date::printUS(){



    cout << setfill('0') << setw(2) << getMonth()<<"/";

    cout << setfill('0') << setw(2) << getDay()<<"/";

    cout << setfill('0') << setw(4) << getYear();

}

// Short format
void Date::printShort(){


    cout<<getMonth() << "/";

    cout<<getDay() << "/";

    cout<< setfill('0') << setw(2) << getYear()%100;

}


// International format
void Date::printInternational(){


    cout<< setfill('0') << setw(4) << getYear()<<"-";

    cout<< setfill('0') << setw(2) << getMonth()<<"-";

    cout<< setfill('0') << setw(2) << getDay();

}

Calender.cpp:

Code:

#include <iostream>
#include <time.h>
#include "Date.h"

using namespace std;
static const int SIZE = 5;

int main(){

    // Seed time to generate numbers
    srand(time(0));

    // Create an array of size SIZE
    Date dates[SIZE];

    // Create a loop
    for (int i = 0; i < SIZE; ++i) {

        // Generate a random year between [1950 and 2049]
        int year = (rand() % (dates[i].MAX_YEAR - dates[i].MIN_YEAR + 1)) + dates[i].MIN_YEAR;

        // Prompt user take month input, day input
        cout << "Please enter month: ";
        int month;
        cin >> month;


        cout << "Please enter day: ";
        int day;
        cin >> day;

        // Set year
        dates[i].SetYear(year);

        // Try catch blocks for month, validate month
        try {
            if(month >= dates[i].MIN_MONTH && month <= dates[i].MAX_MONTH) {
                dates[i].SetMonth(month);
            }
            else{
                throw(month);
            }
        }
        catch (int invalidMonth) {
            cout << "Invalid Month!" << endl;
            if(invalidMonth < dates[i].MIN_MONTH){
                cout << "Values must be greater than 0" << endl;
            }
            else{
                cout << "Value must me less than equal to 12" << endl;
            }
        }


        // Validate day
        try {
            if(day >= dates[i].MIN_DAY && day <= dates[i].MAX_DAY) {
                dates[i].SetDay(day);
            }
            else{
                throw(day);
            }
        }
        catch (int invalidDay) {
            cout << "Invalid Month!" << endl;
            if(invalidDay < dates[i].MIN_DAY){
                cout << "Values must be greater than 0" << endl;
            }
            else{
                cout << "Value must me less than equal to 31" << endl;
            }
        }
    }


    // Print output on console
    cout << "\nDate in US Format" << endl;
    for(int i=0;i<SIZE;i++){
        dates[i].printUS();
        cout << endl;
    }

    cout << "\nDate in Short Format" << endl;
    for(int i=0;i<SIZE;i++){
        dates[i].printShort();
        cout << endl;
    }

    cout << "\nDate in International Format" << endl;
    for(int i=0;i<SIZE;i++){
        dates[i].printInternational();
        cout << endl;
    }
    cout << endl;

    return 0;
}

Output:

#Please ask for any doubts. Thanks.


Related Solutions

How would I write a program for C++ that checks if an entered string is an...
How would I write a program for C++ that checks if an entered string is an accepted polynomial and if it is, outputs its big-Oh notation? Accepted polynomials can have 3 terms minimum and no decimals in the exponents.
How would I get this started: Code a logic program that uses a for loop to...
How would I get this started: Code a logic program that uses a for loop to call your method 5 times successively on the values 1 through 5. (i.e. it would display the val and it’s square…)
java Write our Test Driver program that tests our Classes and Class Relationship How we calculate...
java Write our Test Driver program that tests our Classes and Class Relationship How we calculate Net Pay after calculating taxes and deductions taxes: regNetPay = regPay - (regPay * STATE_TAX) - (regPay * FED_TAX) + (dependents * .03 * regPay ) overtimeNetPay = overtimePay - (overtimePay * STATE_TAX) - (overtimePay * FED_TAX) + (dependents * .02 * overtimePay ) Example printPayStub() output: Employee: Ochoa Employee ID: 1234 Hourly Pay: $25.00 Shift: Days Dependents: 2 Hours Worked: 50 RegGrossPay: $1,000.00...
In a Java program, how could I write a program that can assign values that would...
In a Java program, how could I write a program that can assign values that would make a rock paper scissors game work? I have a program that will generate a computer response of either rock, paper, or scissors but how can I compare a user input of "rock", "paper", or "scissors" so that we can declare either the user or the computer the winner.
Write a C++ die roller program. We need you to write a program that will roll...
Write a C++ die roller program. We need you to write a program that will roll dice for them, but not just six-sided dice. Your program needs to be able to take an input in for form nDx or ndx where the d is the letter d or D. n may or not be present. If it is, it represents the number of dice. If not, assume it is a 1. x may or may not be present. If it...
I am writing a shell program in C++, to run this program I would run it...
I am writing a shell program in C++, to run this program I would run it in terminal like the following: ./a.out "command1" "command2" using the execv function how to execute command 1 and 2 if they are stored in argv[1] and argv[2] of the main function?
How do I create this program? Using C++ language! Write a program that reads data from...
How do I create this program? Using C++ language! Write a program that reads data from a text file. Include in this program functions that calculate the mean and the standard deviation. Make sure that the only global varibles are the mean, standard deviation, and the number of data entered. All other varibles must be local to the function. At the top of the program make sure you use functional prototypes instead of writing each function before the main function....ALL...
how to write a c++ program that replaces certain words with two "**" input: I went...
how to write a c++ program that replaces certain words with two "**" input: I went to the store To buy some fruit Apples are red I like oranges better I saw grapes are green apples are red, oranges, grapes are green (sub strings that need to be replaced) iterate through the string input and get an output that replaces the sub strings with ** If the sub strings are not in the input just return the string as it...
Hi, I would like to test a java program. I am learning linked list and going...
Hi, I would like to test a java program. I am learning linked list and going to make a linked lists for integer nodes. For instance, I am going to add the numbers 12, 13, and 16 to the list and then display the list contents and add 15 to the list again and display the list contents and delete 13 from the list and display the list contents and lastly delete 12 from the list and display the list...
Writing Classes I Write a Java program containing two classes: Dog and a driver class Kennel....
Writing Classes I Write a Java program containing two classes: Dog and a driver class Kennel. A dog consists of the following information: • An integer age. • A string name. If the given name contains non-alphabetic characters, initialize to Wolfy. • A string bark representing the vocalization the dog makes when they ‘speak’. • A boolean representing hair length; true indicates short hair. • A float weight representing the dog’s weight (in pounds). • An enumeration representing the type...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT