12.2 LAB 12.2: Classes (Date) C++
Rewrite the class definition of Date class (From Lab 12.1) including 4 added member functions:
Write the definition of the member functions of the Date class. Please take a input from user for month, day and year. Create a Date object with them. Output whether the date is a leap year, how many days has been passed in this year and how many days is left. Take input for another date (month, day and year). Output the difference between the current date and the second date.
Ex. If the input is: 1 1 2012 1 1 2013, Then output is
2012 is a leap year 1 day has been passed in this year 365 days are left for this year Difference is: 0 days
//SINCE LAB 12.1 WAS NOT GIVEN SO HERE A NEW CLASS IS USED
#include<iostream>
using namespace std;
//body of the class
class date
{
int mm,dd,yy; //data members
to srtore the first date
int bd,bm,by;//data members
to srtore the second date
public:
void input(); //method to
input the date
void isleap(); //method to
check leap or not
int dayspassed(); //method to
return numberof days passed
int daysleft(); //method to
return number of days left
void difference(); //method
to comput the difference
};
void date :: input()
{
//read the first date
cout<<endl<<"Enter the first date";
cout<<"\nEnter the day";
cin>>dd;//reed the day
cout<<endl<<"Enter the month";
cin>>mm;//read the month
cout<<endl<<"Enter the year";
cin>>yy;//read the year
//read the second date
cout<<endl<<"Enter the second date";
cout<<"\nEnter the day";
cin>>bd;//reed the day
cout<<endl<<"Enter the month";
cin>>bm;//read the month
cout<<endl<<"Enter the year";
cin>>by;//read the year
}
void date :: isleap()
{
if((yy%4==0) &&
(yy%100!=0)||(yy%400==0)) //condition for leap year
{
cout<<endl<<yy<<"
is a leap year";
}
else
{
cout<<endl<<yy<<"
is not a leap year";
}
}
int date :: dayspassed()
{
int mdays[]={31,28,31,30,31,30,31,31,30,31,30,31};
//assign the maximum days of the months
int day,dayspass=0;
int i;
if((yy%4==0) && (yy%100!=0)||(yy%400==0))
//check for leap years
{
day=366; //set the number of days for
this year
mdays[1]=29;//set the days for february
month
}
else
day=365; //set the number of days
for this year
for(i=0;i<mm-1;i++) //add number
of days left as a whole month
dayspass=dayspass+mdays[i];
dayspass = dayspass+dd;//add
remaining days
return dayspass; //return the
num,ber of days passed
}
int date :: daysleft()
{
int dayleft;
int n= dayspassed();
//compuet the number of days passed
if((yy%4==0) &&
(yy%100!=0)||(yy%400==0)) //check for leap year
dayleft=366-n;//number of days left when
leap year
else
dayleft = 365-n;//number of days left
when not a leap year
return dayleft; //return the number of
days left
}
void date::difference()
{
int z,r,s;
//block to compte the number of days difference
if(dd >= bd)
z=bd-bd;
else
if(yy%4==0 && yy%100!=0 || yy%400==0) //for leap year
{
if(mm==2) //february month
{
dd=dd+29;
mm=mm-1;
z=dd-bd;
}
}
else
if(mm==2)//february month
{
dd=dd+28;
mm=mm-1;
z=dd-bd;
}
else //months having 31 days
if(mm==1 || mm==3 || mm==5 || mm==7 || mm==8 || mm==10 ||
mm==12)
{
dd=dd+31;
mm=mm-1;
z=dd-bd;
}
else
{ //months
having 30 days
dd=dd+30;
mm=mm-1;
z=dd-bd;
}
if(mm<bm)
{
mm=mm+12;
yy=yy-1;
r=mm-bm;
}
else
r=mm-bm;
//difference between the months
s = yy-by; //difference of
years
cout<<endl<<"Difference
is "<<z<<" days "<<r<<" months
"<<s<<" years.";
}
//driver program
int main()
{
date d;
int p,l;
//call to input()
d.input();
//call to check leap or not
d.isleap();
//call to dayspassed()
p=d.dayspassed();
//call to daysleft()
l=d.daysleft();
//display the details
cout<<endl<<p<<" days has been
passed in this year";
cout<<endl<<l<<" days left for this
year";
d.difference();
}
OUTPUT
12.2 LAB 12.2: Classes (Date) C++ Rewrite the class definition of Date class (From Lab 12.1)...
Use Java and creat proper classes to finish this problem Write a class called Date that represents a date consisting of a year, month, and day. A Date object should have the following methods: public Date(int year, int month, int day) Constructs a new Date object to represent the given date. public void addDays(int days) Moves this Date object forward in time by the given number of days. public void addWeeks(int weeks) Moves this Date object forward in time by...
Create a new project called Date In This new project you are going to design a class of Date. This mean that you need to create functions within the class to validate the Date for example: is it a leap year? How many days a months has? How many months are? Create a simple main menu capable of entering a date and validating a date. Instructions • Create a class of Date with attribues and member functions (remember the use...
- Classes Write a C++ program that creates a class called Date that includes three pieces of information as data members, namely: month (type int) day (type int) year (type int). Program requirements: - Provide set and a get member functions for each data member. - For the set member functions assume that the values provided for the year and day are correct, but in the setMonth member function ensure that the month value is in the...
Write a class called Date that represents a date consisting of a year, month, and day. A Date object should have the following methods: public Date(int year, int month, int day) Constructs a new Date object to represent the given date. public void addDays(int days) Moves this Date object forward in time by the given number of days. public void addWeeks(int weeks) Moves this Date object forward in time by the given number of seven-day weeks. public int daysTo( Date...
Please write the program in C++ Problem Description: Design a class called Date. The class should store a date in three integers: month, day, and year. Create the necessary set and get methods. Design member functions to return the date as a string in the following formats: 12/25/2012 December 25, 2012 25 December 2012 Input Validation: Do not accept values for day greater than 30 or less than 1. Do not accept values for month...
Design and implement a C++ class called Date that has the following private member variables month (int) day (nt) . year (int Add the following public member functions to the class. Default Constructor with all default parameters: The constructors should use the values of the month, day, and year arguments passed by the client program to set the month, day, and year member variables. The constructor should check if the values of the parameters are valid (that is day is...
Given the following date class interface: class date {private: int month;//1 - 12 int day;//1 - 28. 29. 30. 31 depending on month & year int year;//4-digit, e.g.. 2017 public: date();//Default constructor (investigate; find what it is used for)//Postcondition: the newly declared date object is initialized to 01/01/2000 date(int mm, int dd, int yyyy);//Second constructor//Postcondition: the newly declared data object is initialized to mm/dd/yyyy void setDate(int mm. int dd. int yyyy);//Postcondition: set the contents of the calling date object to...
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...
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...
A year in the modern Gregorian Calendar consists of 365 days. In reality, the earth takes longer to rotate around the sun. To account for the difference in time, every 4 years, a leap year takes place. A leap year is when a year has 366 days: An extra day, February 29th. The requirements for a given year to be a leap year are: 1) The year must be divisible by 4 2) If the year is a century year...