In: Computer Science
I have a homework class, but I don't really understand anything and I have to submit my homework next week. Homework must be written in C ++ program language.
Can someone help me please...
Working with classes (everything written below is one task):
Define a class Date that contains integer variables for day, month, and year.
1.1. Create the necessary methods for the class: set, get, default constructor, constructor with arguments.
1.2. Create a method that calculates the number of days between two dates (objects) submitted to it.
1.3. Create a dateUpdate method that calculates the next day (assuming 30 days in each month)
Define a Time class that contains integer variables for hours and minutes. Class Time inherits class Date members
2.1. Create the necessary methods for the class: set, get, default constructor, constructor with arguments.
2.2. Create a timeUpdate method that calculates time after one minute.
2.3. Create a method dateAndTime, which calculates the time after one minute taking into account the date (if it is 23:59, then after one minute the time will be 00:00 the next day; if the same time is on December 31, then after one minute the new year will be ).
To write an implementation of the program, which clearly demonstrates all the methods in both classes.
In this problem, we have to define two classes Time and Date and a few methods related to them:
1.1)
The Date class:
Program:
class Date{
int date, month, year;
public:
Date(){
date = 1;
month = 1;
year = 2000;
}
Date(int d, int m, int y){
date = d;
month = m;
year = y;
}
void set(int d, int m, int y)
{
date = d;
month = m;
year = y;
}
int getDay(){
return date;
}
int getMonth(){
return month;
}
int getYear(){
return year;
}
};
1.2)
n = (year2-year1)*360 + (month2-month1)*30 + (date2-date1)
program:
int daysBetween(Date d1, Date d2){
long int n1 = d1.getYear()*360 + d1.getDay();
for (int i=0; i<d1.getMonth() - 1; i++)
n1 += 30;
long int n2 = d2.getYear()*360 + d2.getDay();
for (int i=0; i<d2.getMonth() - 1; i++)
n2 += 30;
return (n2 - n1);
}
1.3)
program:
void dateUpdate(Date d1){
int d = d1.getDay();
int m = d1.getMonth();
int y = d1.getYear();
if (d != 30)
{
d = d + 1;
m = m;
y = y;
}
if (d == 30 && m!=12)
{
d = 1;
m = m + 1;
y = y;
}
if ((m == 12) && (d == 30));
{
d = 1;
m = 1;
y = y + 1;
}
cout<<"\n"<<d<<"/"<<m<<"/"<<y;
}
2.1)
program:
class Time{
int hour, minute;
public:
Time(){
hour = 0;
minute = 0;
}
Time(int h, int m){
hour = h;
minute = m;
}
void set(int h, int m){
hour = h;
minute = m;
}
int getHour(){
return hour;
}
int getMinute(){
return minute;
}
};
2.2)
program:
void timeUpdate(Time t){
int m = t.getMinute();
int h = t.getHour();
if(m < 59){
m++;
}
if(m == 59 && h!=23){
m = 0;
h++;
}
if(m == 59 && h==23){
m = 0;
h = 0;
}
cout<<"\n"<<h<<":"<<m;
}
2.3)
program:
void dateAndTime(Time t, Date d1){
int d = d1.getDay();
int mth = d1.getMonth();
int y = d1.getYear();
int min = t.getMinute();
int h = t.getHour();
if(min == 59 && h == 23){
min = 0;
h = 0;
if(d == 30){
if(mth == 12){
y++;
mth = 1;
d = 1;
}
else{
mth++;
d = 1;
}
}
else{
d++;
}
}
else{
min++;
h++;
}
cout<<"\ntime: "<<min<<":"<<h;
cout<<"\ndate:
"<<d<<"/"<<mth<<"/"<<y;
}
Complete program with sample main():
#include<iostream>
using namespace std;
class Date{
int date, month, year;
public:
Date(){
date = 1;
month = 1;
year = 2000;
}
Date(int d, int m, int y){
date = d;
month = m;
year = y;
}
void set(int d, int m, int y)
{
date = d;
month = m;
year = y;
}
int getDay(){
return date;
}
int getMonth(){
return month;
}
int getYear(){
return year;
}
};
class Time{
int hour, minute;
public:
Time(){
hour = 0;
minute = 0;
}
Time(int h, int m){
hour = h;
minute = m;
}
void set(int h, int m){
hour = h;
minute = m;
}
int getHour(){
return hour;
}
int getMinute(){
return minute;
}
};
int daysBetween(Date d1, Date d2){
long int n1 = d1.getYear()*360 + d1.getDay();
for (int i=0; i<d1.getMonth() - 1; i++)
n1 += 30;
long int n2 = d2.getYear()*360 + d2.getDay();
for (int i=0; i<d2.getMonth() - 1; i++)
n2 += 30;
return (n2 - n1);
}
void dateUpdate(Date d1){
int d = d1.getDay();
int m = d1.getMonth();
int y = d1.getYear();
if (d != 30)
{
d = d + 1;
m = m;
y = y;
}
if (d == 30 && m!=12)
{
d = 1;
m = m + 1;
y = y;
}
if ((m == 12) && (d == 30));
{
d = 1;
m = 1;
y = y + 1;
}
cout<<"\n"<<d<<"/"<<m<<"/"<<y;
}
void timeUpdate(Time t){
int m = t.getMinute();
int h = t.getHour();
if(m < 59){
m++;
}
if(m == 59 && h!=23){
m = 0;
h++;
}
if(m == 59 && h==23){
m = 0;
h = 0;
}
cout<<"\n"<<h<<":"<<m;
}
void dateAndTime(Time t, Date d1){
int d = d1.getDay();
int mth = d1.getMonth();
int y = d1.getYear();
int min = t.getMinute();
int h = t.getHour();
if(min == 59 && h == 23){
min = 0;
h = 0;
if(d == 30){
if(mth == 12){
y++;
mth = 1;
d = 1;
}
else{
mth++;
d = 1;
}
}
else{
d++;
}
}
else{
min++;
h++;
}
cout<<"\ntime: "<<min<<":"<<h;
cout<<"\ndate:
"<<d<<"/"<<mth<<"/"<<y;
}
int main(){
Date d1(30,12,2012), d2(12, 2, 2009);
Time t1(23, 59);
cout<<"\n days betweem d1 and d2:
"<<daysBetween(d1,d2);
cout<<"\n day after d1: "; dateUpdate(d1);
cout<<"\n minute after t1: "; timeUpdate(t1);
cout<<"\n minute after t1 at d1: ";dateAndTime(t1, d1);
cout<<"\n";
return 0;
}
Output:
That concludes our solution, if you need more information please reach out to me in comments.