Need help with creating this java program Create a package n

Need help with creating this java program

Create a package named date_array and you will need 2 classes. Create a class called MyDate. This class only needs the following things:

Instance variables for month, day and year

Constructor: MyDate(String month, int day, int year) - the code stores the values in 3 instance variables

String toString() method - returns a String containing your 3 instance variables.

Create another class called DateArray that creates an array of \"MyDate\" with the name of \"dateArr\".

The array should have 4 entries and the entries should be filled with MyDate classes representing the dates:

The DateArray class should print the values of the dateArr array backwards. Your MyDate classes can be printed using a toString() method in the MyDate class.

Solution

DateArray.java


public class DateArray {

  
   public static void main(String[] args) {
       MyDate dateArr[] = new MyDate[4];
       MyDate date1 = new MyDate(\"May\", 16, 1984);
       MyDate date2 = new MyDate(\"November\", 14, 1978);
       MyDate date3 = new MyDate(\"September\", 21, 1980);
       MyDate date4 = new MyDate(\"July\", 3, 1987);
       dateArr[0] = date1;
       dateArr[1] = date2;
       dateArr[2] = date3;
       dateArr[3] = date4;
       for(int i=dateArr.length-1; i>=0; i--){
           System.out.println(dateArr[i].toString());
       }
   }

}

MyDate.java


public class MyDate {
   private int day , year;
   private String month;
   public MyDate(String month, int day, int year) {
       this.month = month;
       this.day = day;
       this.year = year;
   }
   public String toString() {
       return month+\" \"+day+\", \"+year;
   }
}

Output:

July 3, 1987
September 21, 1980
November 14, 1978
May 16, 1984

Need help with creating this java program Create a package named date_array and you will need 2 classes. Create a class called MyDate. This class only needs the

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site