Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java: (Date Class ) Create a class called Date that includes three instance vari

ID: 3666569 • Letter: J

Question

Java:

(Date Class ) Create a class called Date that includes three instance variables -a month (type  int ), a day (type  int ) and a year (type  int ). Provide a constructor that initializes the three instance variables and assumes that the values provided are correct. Provide a set and a getmethod for each instance variable . Provide a method displayDate that displays the month, day and year separated by forward slashes (/). Write a test app named DateTest that demonstrates class Date's capabilities.

Explanation / Answer


// create class DateTest
public class DateTest
{
   public static void main(String[] args)
   {
       // passing parameters
       Date date = new Date(8, 7, 2016);
  
       //passing parameters
       date.setDay(8);
       System.out.printf("Day: %d%n", date.getDay());

       date.setMonth(7);
       System.out.printf("Month: %d%n", date.getMonth());

       date.setYear(2016);
       System.out.printf("Year: %d%n", date.getYear());

       // call function
       date.displayDate();
   }
}

Date.java
// create class Date

public class Date
{
   // declare variables
   private int day, month, year;

   // Assume values and create constructor
   public Date(int day, int month, int year)
   {
       this.day = day;
       this.month = month;
       this.year = year;
   }
   // using getter and setter method
   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;
   }
  
   //display date
   public void displayDate()
   {
       System.out.printf("%d/%d/%d%n", day, month, year);
   }
}

Sample output
                                                                                                                   
Day: 8                                                                                                                                                      
Month: 7                                                                                                                                                    
Year: 2016                                                                                                                                                  
8/7/2016                                                                                                                                                    

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote