Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Part 1: Using the code below write a class called SpecialDate which is a subclass of Date class (this code below). It has a new

Part 1:

Using the code below write a class called SpecialDate which is a subclass of Date class (this code below).

It has a new variable called description which describes the nature of the special date (such as Moms birthday, Graduation date, etc.). Include get/set methods for this variable. For the default constructor, set the description to A Special Date.

Include two constructors for the Special Date class, a default constructor, and a four-parameter constructor (with a given month, day, year, and the description).

Write the overridden method toString to return the string in the format of month/day/year followed by the description (such as 5/20/1970, Moms birthday)

Write the overridden method printDate to print the date in the same format as toString method.

Part 2:

Write SpecialDateTest class to test the SpecialDate and Date class.

Create two objects of SpecialDate, one using the default constructor and another one using the four-parameter constructor. Both objects use SpecialDate as the data type.

Create another object of SpeicalDate; but use Date as the data type for the variable.

Compare the order of the three dates using compareTo method, one pair at a time.

Change the description of one of the dates (using set method) and print the new description using the get method.

Print all the dates using the overridden toString method.

Print all the dates using the overridden printDate method.

package date;

public class Date implements Comparable {

private int month, day, year;

//default constructor date

public Date() {

this.month = 1;

this.day = 1;

this.year = 2000;

}

//the three parameter constructor include the month, day and year

public Date(int month, int day, int year) {

this.month = month;

this.day = day;

this.year = year;

}

public int getMonth() {

return month;

}

public void setMonth(int month) {

this.month = month;

}

public int getDay() {

return day;

}

public void setDay(int day) {

this.day = day;

}

public int getYear() {

return year;

}

public void setYear(int year) {

this.year = year;

}

//returns the date into a string

public String toString() {

return month + "/" + day + "/" + year;

}

public void printDate() {

System.out.println(month + "/" + day + "/"+ year);

}

@Override

public int compareTo(Date date)

{

if(this.year < date.year) {

return -1;

}

if(this.year > date.month) {

return 1;

}

if(this.month < date.month) {

return -1;

}

if(this.month > date.month) {

return 1;

}

if(this.day < date.day) {

return -1;

}

if(this.day > date.day) {

return 1;

}

return 0;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

2. Outline the business case for a diverse workforce.

Answered: 1 week ago