Question

In: Computer Science

I have a homework class, but I don't really understand anything and I have to submit...

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.

Solutions

Expert Solution

In this problem, we have to define two classes Time and Date and a few methods related to them:

1.1)

The Date class:

  • The Date class will contain three data members: day, month, year
  • Two constructors: one default and one parametrized
  • getter and setter methods

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)

  • This method calculates total number of days betweem any two dates, assuming that each month has 30 days in total
  • The formula for number of days between two dates day1/month1/year1 and day2/month2/years is:

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)

  • This month finds the date after the current date.
  • This method will deal with two special cases:
    • last day of the month: in this case we have increment the month and set day to 1
    • last day of the year: in this case we have to increment year and set month and day to 1.
    • otherwise, increment the date

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)

  • create Time class with:
    • data members: hour and minute (integers)
    • constructors: one default and one parametrized.
    • method: getters and setters for each data member

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)

  • this method the time after one minute
  • This method deal with two cases:
    • last minute of the hour: increment hour and set minute to 0
    • last minute of day: set hour and day to 0.
    • otherwise increment minute

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)

  • This method will calculate time and date after one minute
  • this method has five cases:
    • last minute of year: increment year, set month and day to 1, set hour and minute to 0
    • last minunte of month: increment month, set day to 1, set hour and minute to 0
    • last minute of day: increment day, set hour and minute to 0
    • last minute of hout: increment hour and set minute to 0
    • otherwise increment minute

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.


Related Solutions

please I don't really know how to start answering this question I really need to understand...
please I don't really know how to start answering this question I really need to understand it please show the work with a clear handwriting A collision in one dimension A mass m1 = 2 kg moving at v1i = 3 ms−1 collides with another mass m2 = 4 kg moving at v2i = −2 ms−1. After the collision the mass m1 moves at v1f = −3.66 ms−1. (a) Calculate the final velocity of the mass m2. (b) After the...
HI, I hope you are doing well. I really don't understand this question and don't know...
HI, I hope you are doing well. I really don't understand this question and don't know how to solve it at all because I am completely new to this c++ programming. can you please explain each line of code with long and clear comments? please think of me as someone who doesn't know to code at all. and I want this code to be written in c++ thank you very much and I will make sure to leave thumbs up....
I don't really understand how to estimate revenue. I have been given revenues from previous years...
I don't really understand how to estimate revenue. I have been given revenues from previous years and I am being asked to estimate the revenue for FY2020 and explain my rationale. Please help thank you! Please show all relevant work so I can understand how you got to the answer. Thank you! PS: if possible please use regression including a graph. Year Revenue Collections 2002 $                      329,687 2003 $                      384,829 2004 $                      378,109 2005 $                      447,858 2006 $                      523,845 2007 $                      621,253 2008 $                      824,460 2009 $                      739,140...
I really don't understand what the following question is trying to ask. In regards to medical...
I really don't understand what the following question is trying to ask. In regards to medical marijuana was there a trade-off between science and politics?
This is the homework given by the teacher. I don't have more information Complete the below...
This is the homework given by the teacher. I don't have more information Complete the below code so that your program generates a random walk on the given graph. The length of your walk should also be random. /******************************************************************/ #include #include #include typedef struct NODE_s *NODE; typedef struct NODE_s{    char name;    NODE link[10]; }NODE_t[1]; #define nodeA 0 #define nodeB 1 #define nodeC 2 #define nodeD 3 #define nodeE 4 #define nodeF 5 int main() {    srandom(time(NULL));   ...
HOMEWORK PROJECT #1 – SHOPPING CART Part I. Create two files to submit: ItemToPurchase.java – Class...
HOMEWORK PROJECT #1 – SHOPPING CART Part I. Create two files to submit: ItemToPurchase.java – Class Definition ShoppingCartPrinter.java – Contains main() method Build the ItemToPurchase class with the following specifications: Specifications Description ItemToPurchase(itemName) itemName – The name will be a String datatype and Initialized in default constructor to “none”. ItemToPurchase(itemPrice) itemPrice – The price will be integer datatype and Initialized in default constructor to 0. ItemToPurchase(itemQuantity) itemQuantity – The quantity will be integer datatype Initialized in default constructor to 0....
More than anything I need 5 - 7 of this homework. You have been asked by...
More than anything I need 5 - 7 of this homework. You have been asked by your supervisors at A&L Engineering to design a roller coaster for a new theme park. Because this design is in the initial stages, you have been asked to create a track for the ride. Your coaster should have at least two peaks and two valleys, and launch from an initial height of 75 meters. Each peak and valley should represent a vertical change of...
Hi, I have the answers, but I don't understand how to get the answers. Please explain...
Hi, I have the answers, but I don't understand how to get the answers. Please explain thoroughly. Bob earns ($25,000) in passive losses from BHI partnership. He has an outside basis of $40,000 of which $30,000 comes from non-recourse debt, and he has passive income of $50,000. What are the tax consequences to Bob? $10,000 deductible loss What basis does Bob take in his partnership interest? $15,000 How much is Bob at-risk after the allocation? 0 How much, if any...
I have trouble when solving this problem. I don't understand "There are more than half of...
I have trouble when solving this problem. I don't understand "There are more than half of the Final scores " could refer to which? Please help me in solving this. Test the hypothesis that there are more than half of the Final scores that are 5 or below, at the significance level of 5%. Final Scores 9.2 4.8 6.6 5.8 3.4 5 5.8 3.4 2.2
I have the answers for these questions, according to my study guide. I don't understand how...
I have the answers for these questions, according to my study guide. I don't understand how the answers were obtained, though, so please show work! A) You are planning to take two exams. According to the records, the failure rates for the two exams are 15% and 25%, respectively. Additionally, 80% of the student who passed the exam 1 passed exam 2. (The 80% is based on the given condition.) What will be the probability that you fail the 1st...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT