(IN JAVA )
Given the class Date that can prints the date in numerical form.
Some applications might require the date to be printed in another
form, such as March 24, 2005. Please do the following:
Derive the class ExtDate so that the date can be printed either
form.
Add a data member to the class ExtDate so that the month can also
be stored in string form. Add a method to output the month in the
string format followed by the year, for instance, in the form March
2005.
Write the definition of the methods to implement the operations for
the class ExtDate and write t test program to test your
program.
Below is the date class.
public class Date
{
private int dMonth; //variable to store the month
private int dDay; //variable to store the day
private int dYear; //variable to store the year
//Default constructor
//Data members dMonth, dDay, and dYear are set to
//the default values
//Postcondition: dMonth = 1; dDay = 1; dYear = 1900;
public Date()
{
dMonth = 1;
dDay = 1;
dYear = 1900;
}
//Constructor to set the date
//Data members dMonth, dDay, and dYear are set
//according to the parameters
//Postcondition: dMonth = month; dDay = day;
// dYear = year;
public Date(int month, int day, int year)
{
setDate(month, day, year);
}
//Method to set the date
//Data members dMonth, dDay, and dYear are set
//according to the parameters
//Postcondition: dMonth = month; dDay = day;
// dYear = year;
public void setDate(int month, int day, int year)
{
if (year >= 1)
dYear = year;
else
dYear = 1900;
if (1 <= month && month <= 12)
dMonth = month;
else
dMonth = 1;
switch (dMonth)
{
case 1: case 3: case 5: case 7:
case 8: case 10: case 12: if (1 <= day && day <=
31)
dDay = day;
else
dDay = 1;
break;
case 4: case 6:
case 9: case 11: if (1 <= day && day <= 30)
dDay = day;
else
dDay = 1;
break;
case 2: if (isLeapYear())
{
if (1 <= day && day <= 29)
dDay = day;
else
dDay = 1;
}
else
{
if (1 <= day && day <= 28)
dDay = day;
else
dDay = 1;
}
}
}
//Method to return the month
//Postcondition: The value of dMonth is returned
public int getMonth()
{
return dMonth;
}
//Method to return the day
//Postcondition: The value of dDay is returned
public int getDay()
{
return dDay;
}
//Method to return the year
//Postcondition: The value of dYear is returned
public int getYear()
{
return dYear;
}
//Method to return the date in the form mm-dd-yyyy
public String toString()
{
return (dMonth + "-" + dDay + "-" + dYear);
}
public boolean isLeapYear()
{
if ((dYear % 4 == 0 && dYear % 100 != 0) || (dYear % 400 ==
0))
return true;
else
return false;
}
public void setMonth(int m)
{
dMonth = m;
}
public void setDay(int d)
{
dDay = d;
}
public void setYear(int y)
{
dYear = y;
}
public int getDaysInMonth()
{
int noOfDays = 0;
switch (dMonth)
{
case 1: case 3: case 5:
case 7: case 8: case 10: case 12: noOfDays = 31;
break;
case 4: case 6: case 9: case 11: noOfDays = 30;
break;
case 2: if (isLeapYear())
noOfDays = 29;
else
noOfDays = 28;
}
return noOfDays;
}
public int numberOfDaysPassed()
{
int[] monthArr = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int sumDays = 0;
int i;
for (i = 1; i < dMonth; i++)
sumDays = sumDays + monthArr[i];
if (isLeapYear() && dMonth > 2)
sumDays = sumDays + dDay + 1;
else
sumDays = sumDays + dDay;
return sumDays;
}
int numberOfDaysLeft()
{
if (isLeapYear())
return 366 - numberOfDaysPassed();
else
return 365 - numberOfDaysPassed();
}
public void incrementDate(int nDays)
{
int[] monthArr = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int daysLeftInMonth;
daysLeftInMonth = monthArr[dMonth] - dDay;
if (daysLeftInMonth >= nDays)
dDay = dDay + nDays;
else
{
dDay = 1;
dMonth++;
nDays = nDays - (daysLeftInMonth + 1);
while (nDays > 0)
if (nDays >= monthArr[dMonth])
{
nDays = nDays - monthArr[dMonth];
if ((dMonth == 2) && isLeapYear())
nDays--;
dMonth++;
if (dMonth > 12)
{
dMonth = 1;
dYear++;
}
}
else
{
dDay = dDay+nDays;
nDays = 0;
}
}
}
public void makeCopy(Date otherDate)
{
dMonth = otherDate.dMonth;
dDay = otherDate.dDay;
dDay = otherDate.dDay;
}
public Date getCopy()
{
Date temp = new Date();
temp.dMonth = dMonth;
temp.dDay = dDay;
temp.dYear = dYear;
return temp;
}
}
/*
* The java tester class that creates an instance of ExtDate class
with month number, day and year values. Then calls toString method
to print the date as March 24, 2005 format. Similarly, another
object of the ExtDate class is created with a month name and year.
Then call the method, getMonthYearFormat to print the date as March
2005 format
* */
//Tester.java
public class Tester
{
public static void main(String[] args) {
/*Create an instance of
ExtDate class with
* month=3,day=24 and year
=2005*/
ExtDate extdate=new ExtDate(3, 24,
2005);
/*Call toString method on
extdate object*/
System.out.println(extdate.toString());
//Create an ExtDate class
with month name and year
ExtDate ext=new
ExtDate("March",2005);
/*Call toString method on
extdate object*/
System.out.println(ext.getMonthYearFormat());
}//end of the main method
} //end of the class
----------------------------------------------------------------------------------------------------------------
//ExtDate.java
/*This class extends the base class.Date*/
public class ExtDate extends Date
{
//declare an instance variable
,month
private String month;
/*Constructor that takes month and
year*/
public ExtDate(String month,int year)
{
//call setYear of base class,Date
to set year
setYear(year);
//set month value
this.month=month;
}
/*Constructor that takes month,day and year
*/
public ExtDate(int month, int day, int year)
{
//calling super class Date
constructor
//with month,day and year
super(month, day, year);
}
/*Method to return the date in the form month
Name day , year*/
public String toString()
{
String monthNames[]= {"January",
"February", "March", "April", "May", "June", "July",
"August", "September", "October",
"November", "December"};
return monthNames[getMonth()-1] + "
" + getDay() + ", " + getYear();
}
/*Method to return the date in the form month
Name year*/
public String getMonthYearFormat()
{
return month+ " " +
getYear();
}
} //end of the class,ExtDate
----------------------------------------------------------------------------------------------------------------
Sample Output :
March 24, 2005
March 2005
(IN JAVA ) Given the class Date that can prints the date in numerical form. Some...
Programming Exercise 11-8 PROBLEM:The class dateType defined in Programming Exercise 6 prints the date in numerical form. Some applications might require the date to be printed in another form, such as March 24, 2019. Derive the class extDateType so that the date can be printed in either form. Add a member variable to the class extDateType so that the month can also be stored in string form. Add a member function to output the month in the string format, followed...
The class dateType is 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 data members. Rewrite the definitions of the function setDate and the constructor so that the values for the month, day, and the year are checked before storing the date into the data members. Add a function member, isLeapYear, to check whether a year is a leap...
Can
someone please help me. i keep getting an error in my code. I need
it to be on three seperate files!
attached is my assignment and my code. c++
The class date Type implements the date in a program. Write the class functions as specified in the UML class diagram. The values for the month, day, and year should be validated (in the class functions) before storing the date into the data members. Set the invalid member data to...
#include "stdafx.h" #include <iostream> using namespace std; class dateType { private: int dmonth; int dday; int dyear; public: void setdate (int month, int day, int year); int getday()const; int getmonth()const; int getyear()const; int printdate()const; bool isleapyear(int year); dateType (int month=0, int day=0, int year=0); }; void dateType::setdate(int month, int day, int year) { int numofdays; if (year<=2008) { dyear=year;...
Hello, I have a bug in my code, and when I run it should ask the user to enter the month and then the year and then print out the calendar for the chosen month and year. My problem with my code is that when I run it and I enter the month, it doesn't ask me for the year and it prints all the years of the month I chose. Please help! Code: #include "calendarType.h" #include <iostream> using namespace...
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...
I need to create a Java class file and a client file that does the following: Ask user to enter Today’s date in the form (month day year): 2 28 2018 Ask user to enter Birthday in the form (month day year): 11 30 1990 Output the following: The date they were born in the form (year/month/day). The day of the week they were born (Sunday – Saturday). The number of days between their birthday and today’s...
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...
public class Date { private int month; private int day; private int year; /** default constructor * sets month to 1, day to 1 and year to 2000 */ public Date( ) { setDate( 1, 1, 2000 ); } /** overloaded constructor * @param mm initial value for month * @param dd initial value for day * @param yyyy initial value for year * * passes parameters to setDate method */ public Date( int mm, int dd, int yyyy )...
Need help figuring out what would be the all 12 days output public class Date { private int day; private int month; private int year; private static int tracker; public Date() { this(1, 1, 2000); System.out.println("4"); } public Date(int year) { this(1, 1, year); System.out.println("3"); } public Date(int month, int year) { this(1, month,...