C++ Program (The MyDate class) Design a class named MyDate. The class contains:
ID: 3601441 • Letter: C
Question
C++ Program (The MyDate class) Design a class named MyDate. The class contains:
The data fields year , month , and day that represent a date. month is 0-based, i.e., 0 is for January.
A no-arg constructor that creates a MyDate object for the current date.
A constructor that constructs a MyDate object with a specified elapsed time since midnight, January 1, 1970, in seconds.
A constructor that constructs a MyDate object with the specified year, month, and day.
Three constant getter methods for the data fields year , month , and day , respectively.
Three setter methods for the data fields year , month , and day , respectively.
A function named setDate(long elapsedTime) that sets a new date for the object using the elapsed time.
Write a test program that creates two MyDate objects (using new MyDate() and new MyDate(34355555133101L)) and displays their year, month, and day. (Hint: The first two constructors will extract the year, month, and day from the elapsed time. For example, if the elapsed time is 561555550000 milliseconds, the year is 1987, the month is 9, and the day is 18.)
Explanation / Answer
note: I mistakenly developed the code in java..while am submitting i realized that it should be n c++.No am converting to c++
_____________
MyDate.java
import java.util.Calendar;
public class MyDate {
//Declaring instance variables
private int day;
private int month;
private int year;
//Parameterized constructor
public MyDate(int day, int month, int year) {
super();
this.day = day;
this.month = month;
this.year = year;
}
//Parameterized constructor
public MyDate(long milliSecs) {
super();
setDate(milliSecs);
}
//getters and setters
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
//This method will convert the milliseconds to corresponding date
private void setDate(long elapsedTime) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(elapsedTime);
this.year = calendar.get(Calendar.YEAR);
this.month = calendar.get(Calendar.MONTH);
this.day = calendar.get(Calendar.DAY_OF_MONTH);
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "dd/mm/yy :" + day + "/" + month + "/" + year;
}
}
_______________
Test.java
import java.util.Date;
public class Test {
public static void main(String[] args) {
//Creating an Instance of MyDate class
MyDate md = new MyDate(561555550000 L);
//Displaying the date
System.out.println(md);
}
}
_________________
Output:
dd/mm/yy :18/9/1987
_____________
#include <iostream>
#include <sys/timeb.h>
using namespace std;
class MyDate
{
private :
//Declaring instance variables
int day;
int month;
int year;
public :
//Parameterized constructor
MyDate(int day, int month, int year) {
this->day = day;
this->month = month;
this->year = year;
}
//Parameterized constructor
MyDate(long milliSecs) {
setDate(milliSecs);
}
//getters and setters
int getDay() {
return day;
}
void setDay(int day) {
this->day = day;
}
int getMonth() {
return month;
}
void setMonth(int month) {
this->month = month;
}
int getYear() {
return year;
}
void setYear(int year) {
this->year = year;
}
//This method will convert the milliseconds to corresponding date
void setDate(long elapsedTime)
{
}
//toString method is used to display the contents of an object inside it
void display() {
cout<< "dd/mm/yy :"<< day << "/" << month << "/" << year<<endl;
}
};
int main()
{
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.