Java class definition create Date class

Question

We would like to create a class called Date.

It 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.

Provide a set and a get method for each instance variable.

Provide a method displayDate that displays the month, day and year separated by forward slashes (/).

Write a test app named Main that demonstrates class Date's capabilities.

class Date
{
  //your code here
}

public class Main
{
  public static void main( String[] args )
  {/*from  ww  w  .j a v a  2  s  . c om*/
    Date myDate = new Date(2, 4, 2016);
    
    myDate.displayDate();
  }
}



class Date
{
  private int day;
  private int month;
  private int year;
  
  public Date(int day, int month, int year)
  {
    setDay(day);
    setMonth(month);
    setYear(year);
  }
  
  public void setDay( int day )
  {
    this.day = day;
  }
  
  public int getDay()
  {
    return day;
  }
  
  public void setMonth( int month )
  {
    this.month = month;
  }
  
  public int getMonth()
  {
    return month;
  }
  
  public void setYear( int year )
  {
    this.year = year;
  }
  
  public int getYear()
  {
    return year;
  }
  
  public void displayDate()
  {
    System.out.printf( "%d / %d / %d", getDay(), getMonth(), getYear() );
  }
}

public class Main
{
  public static void main( String[] args )
  {
    Date myDate = new Date(2, 4, 2016);
    
    myDate.displayDate();
  }
}



PreviousNext

Related