Question

In: Computer Science

Review the General Programming Assignment instructions. In this chapter, the class dateType was designed to implement...

  1. Review the General Programming Assignment instructions.
  2. In this chapter, the class dateType was designed to implement the date in a program, but the member function setDate and the constructor do not check whether the date is valid before storing the date in the member variables. Rewrite the definitions of the function setDate and the constructor so that the values for the month, day, and year are checked before storing the date into the member variables.
  3. Add a member function, isLeapYear, to check whether a year is a leap year. Moreover, write a test program to test your class.
  4. 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:
    1. Set the month.
    2. Set the day.
    3. Set the year.
    4. Return the month.
    5. Return the day.
    6. Return the year.
    7. Test whether the year is a leap year.
    8. Return the number of days in the month. For example, if the date is 3-12-2017, the number of days to be returned is 31 because there are 31 days in March.
    9. Return the number of days passed in the year. For example, if the date is 3-18-2017, the number of days passed in the year is 77. Note that the number of days returned also includes the current day.
    10. Return the number of days remaining in the year. For example, if the date is 3-18-2017, the number of days remaining in the year is 288.
    11. Calculate the new date by adding a fixed number of days to the date. For example, if the date is 3-18-2017 and the days to be added are 25, the new date is 4-12-2017.
  5. Write the definitions of the functions to implement the operations defined for the latest version of the class dateType.
  6. The class dateType prints the date in numerical form. Some applications might require the date to be printed in another form, such as March 24, 2017.
    1. Derive the class extDateType so that the date can be printed in either form.
    2. Add a member variable to the class extDateType so that the month can also be stored in string form.
    3. Add a member function to output the month in the string format, followed by the year—for example, in the form March 2017.
    4. Write the definitions of the functions to implement the operations for the class extDateType.
    5. Using the classes extDateType and dayType (See Chap 10 Prog Ex 5), design the class calendarType so that, given the month and the year, we can print the calendar for that month. To print a monthly calendar, you must know the first day of the month and the number of days in that month. Thus, you must store the first day of the month, which is the form dayType, and the month and the year of the calendar. Clearly, the month and the year can be stored in an object of the form extDateType by setting the day component of the date to 1 and the month and year as specified by the user. Thus, the class calendarType has two member variables: an object of the type dayType and an object of the type extDateType.
    6. Design the class calendarType so that the program can print a calendar for any month starting January 1, 1500. Note that the day of January 1 of the year 1500 is a Monday. To calculate the first day of a month, you can add the appropriate days to Monday of January 1, 1500.
  7. For the class calendarType, include the following operations:
    1. Determine the first day of the month for which the calendar will be printed. Call this operation firstDayOfMonth.
    2. Set the month.
    3. Set the year.
    4. Return the month.
    5. Return the year.
    6. Print the calendar for the particular month.
    7. Add the appropriate constructors to initialize the member variables.
  8. Write the definitions of the member functions of the class to implement the operations of the class calendarType.
  9. Write a test program to print the calendar for either a particular month or a particular year.

    For example, the calendar for September 2017 is:

    September 2017

    Sun      Mon     Tue      Wed    Thu      Fri        Sat
    1 2
    3          4          5          6          7          8         9
    10        11        12        13        14        15        16       
    17        18        19        20        21        22        23       
    24        25        26        27        28        29        30

Solutions

Expert Solution

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

// Function that returns the index of the day of the date
// based on the parameter day - month - and year
int getDayNumberIndex(int day, int month, int year)
{
// Declares an array for term
static int term[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
year -= month < 3;
// Checks for leap year and calculates the index and returns
return ( year + year / 4 - year / 100 + year / 400 + term[month - 1] + day) % 7;
}// End of function

// Function that returns the name of the month based on the parameter month
string getMonthName(int month)
{
// Creates an array to store month names
string monthNames[] =
{
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
};// End of initialization

return (monthNames[month]);
}

// Function to return the number of days in a month
// from give parameter month and year
int getNumberOfDays(int month, int year)
{
// Checks if month is January then return 31 days
if (month == 0)
return (31);

// Checks if month is February then checks for leap year
// and return days based on leap and non leap year
if (month == 1)
{
// Checks if the year is leap then return 29 days
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
return (29);
// Otherwise return 28 days
else
return (28);
}// End of if condition

// Checks if month is March then return 31 days
if (month == 2)
return (31);

// Checks if month is April then return 30 days
if (month == 3)
return (30);

// Checks if month is May then return 31 days
if (month == 4)
return (31);

// Checks if month is June then return 30 days
if (month == 5)
return (30);

// Checks if month is July then return 31 days
if (month == 6)
return (31);

// Checks if month is August then return 31 days
if (month == 7)
return (31);

// Checks if month is September then return 30 days
if (month == 8)
return (30);

// Checks if month is October then return 31 days
if (month == 9)
return (31);

// Checks if month is November then return 30 days
if (month == 10)
return (30);

// Checks if month is December then return 31 days
if (month == 11)
return (31);
}// End of function

// Function to print the calendar of the given year
void printCalendar(int year, int month)
{
// Displays month name and year
cout<<endl<<endl<<setw(20)<<getMonthName (month-1).c_str()<<" "<<year<<endl;
// To store number of days
int numberOfDays;

// Calls the function to get the index of the day from 0 to 6
int dayPosition = getDayNumberIndex(1, month, year);
// Calls the function to get number of days for the given month and year
numberOfDays = getNumberOfDays(month - 1, year);

// Displays the day names
cout<<setw(5)<<"SUN"<<setw(5)<<"MON"<<setw(5)<<"TUE"<<setw(5)<<"WED"<<setw(5)<<"THU"
<<setw(5)<<"FRI"<<setw(5)<<"SAT"<<endl;

// Loops variable
int dayCounter;
// Loops to give space for the first date
for (dayCounter = 0; dayCounter < dayPosition; dayCounter++)
cout<<" ";

// Loops for number of days of the given parameter month and year
for (int day = 1; day <= numberOfDays; day++)
{
// Sets the display width of each day to 5 and displays the day number
cout<<setw(5)<<day;
// Increase the counter by one and checks if it is greeter than 6
// (for 7 days of a week)
if (++dayCounter > 6)
{
// Resets the counter to 0
dayCounter = 0;
// Move to next line
cout<<endl;
}// End of if condition
}// End of for loop
}// End of function

// main function definition
int main()
{
// To store year and month number
int yearNumber, monthNumber;

// Accepts the year and month number
cout<<"\n Enter the year: ";
cin>>yearNumber;
cout<<"\n enter the month: ";
cin>>monthNumber;

// Calls the function to display calendar
printCalendar(yearNumber, monthNumber);
return 0;
}// End of main function

Sample Output:


Related Solutions

Programming Exercise 11-2 QUESTION: In this chapter, the class dateType was designed to implement the date...
Programming Exercise 11-2 QUESTION: In this chapter, the class dateType was designed to implement the date in a program, but the member function setDate and the constructor do not check whether the date is valid before storing the date in the member variables. Rewrite the definitions of the function setDate and the constructor so that the values for the month, day, and year are checked before storing the date into the member variables. Add a member function, isLeapYear, to check...
C++ problem 11-2 In this chapter, the class dateType was designed to implement the date in...
C++ problem 11-2 In this chapter, the class dateType was designed to implement the date in a program, but the member function setDate and the constructor do not check whether the date is valid before storing the date in the member variables. Rewrite the definitions of the function setDate and the constructor so that the values for the month, day, and year are checked before storing the date into the member variables. Add a member function, isLeapYear, to check whether...
Code in C Instructions For this programming assignment you are going to implement a simulation of...
Code in C Instructions For this programming assignment you are going to implement a simulation of Dijkstra’s solution to the Dining Philosophers problem using threads, locks, and condition variables. Dijkstra’s Solution Edsgar Dijkstra’s original solution to the Dining Philosophers problem used semaphores, but it can be adapted to use similar mechanisms: • Each philosopher is in one of three states: THINKING, HUNGRY, or EATING. • Every philosopher starts out in the THINKING state. • When a philosopher is ready to...
Instructions Write a program to implement the algorithm that you designed in Exercise 19 of Chapter...
Instructions Write a program to implement the algorithm that you designed in Exercise 19 of Chapter 1. Your program should allow the user to buy as many items as the user desires. Instructions for Exercise 19 of Chapter 1 have been posted below for your convenience. TEXT ONLY PLEASE (PLEASE NO PDF OR WRITING) C++ CODE Exercise 19 Jason typically uses the Internet to buy various items. If the total cost of the items ordered, at one time, is $200...
Implement the Producer-Consumer Problem programming assignment at the end of Chapter 5 in the textbook using...
Implement the Producer-Consumer Problem programming assignment at the end of Chapter 5 in the textbook using the programming language Java instead of the described C code. You must use Java with Threads instead of Pthreads.A brief overview is below. This program should work as follows: The user will enter on the command line the sleep time, number of producer threads, and the number of consumer threads. One example of the Java application from the command line could be “java ProducerConsumer...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for managing a singly linked list that cannot contain duplicates. Default constructor Create an empty list i.e., head is null. boolean insert(int data) Insert the given data into the end of the list. If the insertion is successful, the function returns true; otherwise, returns false. boolean delete(int data) Delete the node that contains the given data from the list. If the deletion is successful, the...
C++ Programming Chapter 7 Assignment: Assignment #4 – Student Ranking : In this assignment you are...
C++ Programming Chapter 7 Assignment: Assignment #4 – Student Ranking : In this assignment you are going to write a program that ask user number of students in a class and their names. Number of students are limited to 100 maximum. Then, it will ask for 3 test scores of each student. The program will calculate the average of test scores for each student and display with their names. Then, it will sort the averages in descending order and display...
Assignment 1 – Writing a Linux Utility Program Instructions For this programming assignment you are going...
Assignment 1 – Writing a Linux Utility Program Instructions For this programming assignment you are going to implement a simple C version of the UNIX cat program called lolcat. The cat program allows you to display the contents of one or more text files. The lolcat program will only display one file. The correct usage of your program should be to execute it on the command line with a single command line argument consisting of the name you want to...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
Instructions Before beginning work on this assignment, please review the expanded grading rubric for specific instructions...
Instructions Before beginning work on this assignment, please review the expanded grading rubric for specific instructions relating to content and formatting. Risk-Based Reimbursement For your assignment, a primary care physician is often reimbursed by Health Maintenance Organizations (HMOs) via capitation, fee-for-service, relative value scale, or salary. Capitation is considered as a risk based compensation. In an effort to understand the intricacies involved with physician reimbursement, particularly in an era of health care reform, identify and interview an expert in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT