Question

Design a class Holiday that represents a holiday during the year. This class has three private...

Design a class Holiday that represents a holiday during the year. This class has three private data members:

  • name: A string that represents the name of holiday.
  • day: An integer that holds the day of the month of holiday.
  • month: A string that holds the month the holiday is in.
  1. Write a default constructor that initializes each data member of class such that name with NULL, day with 0 and month with NULL

Holiday()

  1. Write a constructor that accepts the arguments for each data member such that string n assigned to name, int d to day and string m to month.

Holiday(string &n, int d, string &m)

Note*:Use member function initialization for all data members.

  1. Generate getter setter of each member variable: such that name should never be greater than 50 characters, day should never be negative and month should not be greater than 10 characters.
  • bool setName(string &s)
  • string getName()
  • bool setDay(int u)
  • int getDay()
  • bool setMonth(string &p)
  • string getMonth()
  1. Write a function inSameMonth (outside class) which takes two Holiday objects as arguments, compares two objects of the class Holiday, and returns true if they have the same month otherwise false.

bool inSameMonth (Holiday &a, Holiday &b)

  1. Write a function avgDate (outside class) which takes an array of type Holiday and its size as its argument and returns a double value that is the average of the entire day data member in the Holiday array arr. You may assume that the array is full (i.e. does not have any NULL entries).

double avgDate(Holiday arr[], int size)


//Task 2

Holiday();
Holiday( string &n, int d, string &m);
bool setName( string &s);
string getName() ;
bool setDay(int u);
int getDay() ;
bool setMonth( string &p);
string getMonth() ;
bool inSameMonth ( Holiday &a, Holiday &b);//globle function
double avgDate(Holiday arr[], int size);//globle function


Use only the given function as it will be run in gtest.Write the code in c++.

Gtest:


TEST(Holiday, Constructors) {
Holiday a ("Day 1",1,"September");

ASSERT_EQ(0, a.getName().compare("Day 1"));
ASSERT_EQ(0, a.getMonth().compare("September"));
ASSERT_EQ(1, a.getDay());
}
TEST(Holiday, inSameMonth) {
Holiday a ("Day 1",1,"Sep");
Holiday b ("Day 2",2,"Feb");
Holiday c ("Day 1",1,"Sep");
  
  
ASSERT_EQ(0, inSameMonth(a,b));
ASSERT_EQ(1, inSameMonth(a,c));
  

}
TEST(Holiday, avgDate) {
Holiday a ("Day 1",1,"September");
Holiday b ("Day 2",2,"September");
//cout<<inSameMonth(a,b)<<endl;
Holiday * ptr=new Holiday [2];
*ptr=a;
*(ptr+1)=b;
  
  
ASSERT_EQ(1.5, avgDate(ptr, 2));
  

}


int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

It is mandatory to pass the gtest part.If not then create a main file and put the test data in there to confirm.

Write the code in c++.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//********************************************START OF PROGRAM*****************************************

#include <iostream>

using namespace std;

class Holiday{
string name;
int day;
string month;

public:
Holiday(){
name = ""; //setting name to null
day = 0;
month = ""; //setting month to null
}

Holiday(string const &n, int d, string const &m){
name = n;
day = d;
month = m;
}

bool setName(string const &s);
string getName();
bool setDay(int u);
int getDay();
bool setMonth(string const &p);
string getMonth();

};

bool Holiday::setName(string const &s){
if(s.size() > 50){
cout << "\nName is too big!";
return false;
}
else{
name = s;
return true;
}
}

string Holiday::getName(){
return name;
}

bool Holiday::setDay(int u){
if(u < 0){
cout << "\nInvalid date!";
return false;
}
else{
day = u;
return true;
}
}

int Holiday::getDay(){
return day;
}

bool Holiday::setMonth(string const &p){
if(p.size() > 10){
cout << "\nInvalid month!";
return false;
}
else{
month = p;
return true;
}
}

string Holiday::getMonth(){
return month;
}

bool inSameMonth(Holiday &a, Holiday &b){
return (a.getMonth() == b.getMonth());
}

double avgDate(Holiday arr[], int s){
double avg = 0;
for(int i = 0; i < s; i++) avg += arr[i].getDay();

return (avg / s);
}

int main(){

//constructor test
Holiday a("Day 1", 1, "September");

cout << "TEST FOR Constructors: " << endl;
cout << a.getName() << ", " << a.getDay() << ", " << a.getMonth() << endl;

//test for inSameMonth()
Holiday b("Day 1", 1, "Sep");
Holiday c("Day 2", 2, "Feb");
Holiday d("Day 1", 1, "Sep");

cout << "TEST FOR inSameMonth(): " << endl;
cout << "inSameMonth(b, c) = " << inSameMonth(b, c) << endl;
cout << "inSameMonth(b, d) = " << inSameMonth(b, d) << endl;

//test for avgDate()
Holiday e("Day 1", 3, "September");
Holiday *ptr = new Holiday[2];
*ptr=a;
*(ptr+1)=e;

cout << "TEST FOR avgDate(): " << endl;
cout << "avgDate(a, e) = " << avgDate(ptr, 2);

}
//*************************************************END OF CODE********************************************

REMARK:

It is not possible to pass constants to call by reference methods, hence in all functions using call by reference I have replaced "datatype &var" with "datatype const &var" to avoid errors.

Add a comment
Know the answer?
Add Answer to:
Design a class Holiday that represents a holiday during the year. This class has three private...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • public class Date { private int month; private int day; private int year; /** default constructor...

    public class Date { private int month; private int day; private int year; /** default constructor * sets month to 1, day to 1 and year to 2000 */ public Date( ) { setDate( 1, 1, 2000 ); } /** overloaded constructor * @param mm initial value for month * @param dd initial value for day * @param yyyy initial value for year * * passes parameters to setDate method */ public Date( int mm, int dd, int yyyy )...

  • The Java class called Holiday is started below. An object of class Holiday represents a holiday...

    The Java class called Holiday is started below. An object of class Holiday represents a holiday during the year. This class has three instance variables: name, which is a String representing the name of the holiday day, which is an int representing the day of the month of the holiday month, which is a String representing the month the holiday is in public class Holiday { private String name; private int day; private String month; // your code goes here...

  • 2. Define a class named “Holiday” that manages one holiday info such as month (integer), day...

    2. Define a class named “Holiday” that manages one holiday info such as month (integer), day (integer) and name (string). For example, 7, 4 and “Independence Day” • “toString” method to return holiday info as a string in the format: holiday name (month/day). For example, “Independence Day (7/4)” • “isLater” method that compares with another Holiday object and return true if the date of the holiday is later (month and day) and false otherwise. • “getMonth” method to return the...

  • Create a class named Date in C++ Class has 3 private data items (name the data...

    Create a class named Date in C++ Class has 3 private data items (name the data items month, day, year) int month int day int year Member functions ‘getters’   a getter for each data item Use ‘get’ and the data items name The data item name must be capitalized Return the data item Example: int getMonth()        {return month;} ‘setters’   a setter for each data item Use ‘set’ and the data items name The data item name must be capitalized Change...

  • Write a full class definition for a class named Player , and containing the following members: A data member name of t...

    Write a full class definition for a class named Player , and containing the following members: A data member name of type string . A data member score of type int . A member function called setName that accepts a parameter and assigns it to name . The function returns no value. A member function called setScore that accepts a parameter and assigns it to score . The function returns no value. A member function called getName that accepts no...

  • Programming Exercise 11-8 PROBLEM:The class dateType defined in Programming Exercise 6 prints the date in numerical...

    Programming Exercise 11-8 PROBLEM:The class dateType defined in Programming Exercise 6 prints the date in numerical form. Some applications might require the date to be printed in another form, such as March 24, 2019. Derive the class extDateType so that the date can be printed in either form. Add a member variable to the class extDateType so that the month can also be stored in string form. Add a member function to output the month in the string format, followed...

  • You are to create a class Appointment.java that will have the following: 5 Instance variables: private...

    You are to create a class Appointment.java that will have the following: 5 Instance variables: private String month private int day private int year private int hour private int minute 1 default constructor public Appointment() 1 non-default constructor that accepts arguments for all instance variables, your constructor must call the setter methods below to set the values of the instance variables public Appointment(String monthPassed, int dayPassed, int yearPassed, int hourPassed, int minutePassed) 5 setter methods (one for each instance variable)...

  • Code Lab Help (C++)

    Write the interface (.h file) of a class Player containing:A data member name of type string .A data member score of type int .A member function called setName that accepts a parameter and assigns it to name . The function returns no value.A member function called setScore that accepts a parameter and assigns it to score . The function returns no value.A member function called getName that accepts no parameters and returns the value of name .A member function called...

  • #include "stdafx.h" #include <iostream> using namespace std; class dateType {    private:        int dmonth;...

    #include "stdafx.h" #include <iostream> using namespace std; class dateType {    private:        int dmonth;        int dday;        int dyear;       public:       void setdate (int month, int day, int year);    int getday()const;    int getmonth()const;    int getyear()const;    int printdate()const;    bool isleapyear(int year);    dateType (int month=0, int day=0, int year=0); }; void dateType::setdate(int month, int day, int year) {    int numofdays;    if (year<=2008)    {    dyear=year;...

  • C++ problem 11-6 In Programming Exercise 2, the class dateType was designed and implemented to keep...

    C++ problem 11-6 In Programming Exercise 2, the class dateType was designed and implemented to keep track of a date, but it has very limited operations. Redefine the class dateType so that it can perform the following operations on a date, in addition to the operations already defined: Set the month. Set the day. Set the year. Return the month. Return the day. Return the year. Test whether the year is a leap year. Return the number of days in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT