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…)
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.
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...
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...
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...
I need specific codes for this C program assignment. Thank you! C program question: Write a...
I need specific codes for this C program assignment. Thank you! C program question: Write a small C program connect.c that: 1. Initializes an array id of N elements with the value of the index of the array. 2. Reads from the keyboard or the command line a set of two integer numbers (p and q) until it encounters EOF or CTL - D 3. Given the two numbers, your program should connect them by going through the array and...
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...
How do I write a C++ program to call a frequency table from a csv file,...
How do I write a C++ program to call a frequency table from a csv file, using vector? Data given is in a csv file. Below is part of the sample data. Student ID English Math Science 100000100 80 90 90 100000110 70 60 70 100000120 80 100 90 100000130 60 60 60 100000140 90 80 80
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT