Pages

Labels

Create a class called Date that includes three pieces of information as instance variables—a month (type int), a day (type int) and a year (type int). Your class should have a constructor that initializes the three instance variables and assumes that the values provided are correct. 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 application named DateTest that demonstrates classDate’s capabilities.

import java.io.*;
class Date
{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int dat;
    int month;
    int year;

    Date()
    {
        dat=01;
        month=01;
        year=2001;
    }
    public void get_dat()throws IOException
    {
        System.out.println("Enter date");
        dat=Integer.parseInt(br.readLine());
    }
    public void get_month()throws IOException
    {
        System.out.println("Enter month");
        month=Integer.parseInt(br.readLine());
    }
    public void get_year()throws IOException
    {
        System.out.println("Enter year");
        year=Integer.parseInt(br.readLine());
    }
    public void set_dat(int dat)
    {
        this.dat=dat;
        //System.out.println(dat);
        //return dat;
    }
    public void set_month(int mon)
    {
        month=mon;
    }
    public void set_year(int year)
    {
        this.year=year;
    }
    public void display()
    {
        System.out.println("Entered Date : "+dat+"/"+month+"/"+year);
    }
}
public class Testdate
{
    public static void main(String [] args)throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        Date d1= new Date();
        Date d2 = new Date();

        d1.get_dat();
        d1.get_month();
        d1.get_year();

        d2.set_dat(12);
        d2.set_month(12);
        d2.set_year(12);

        System.out.println("the date for get method");
        d1.display();
        System.out.println("the date for set method");
        d2.display();

    }
}