166k views
0 votes
1. 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 get method for each instance variable. Provide a method displayDate that displays the month, day and year separated by forward slashes (/). Write a test class named DateTest that demonstrates class Date’s capabilities (see the examples on Java Slides 4.27-4.36)(10 points).

1 Answer

6 votes

Answer:

public class Date {

private int month;

private int day;

private int year;

public Date(int m, int d, int y){

month=m;

day=d;

year=y;

}

public void setMonth(int Months){

if(Months>=0 && Months<=12)

month=Months;

else{

month=0;

}

}

public int getMonth(){

return month;

}

public void setDay(int Days){

if(Days>=0 && Days<=30)

day=Days;

else{

day=0;

}

}

public int getDay(){

return day;

}

public void setYear(int Years){

year=Years;

}

public int getYear(){

return year;

}

public void displayDate(){

System.out.printf("%d/%d/%d\\",getMonth(),getDay(),getYear());

}

}

import java.util.Scanner;

public class DateTest {

public static void main(String args[]){

Date date=new Date(0,0,0);

Scanner input=new Scanner(System.in);

System.out.println("Enter Month");

int m=input.nextInt();

date.setMonth(m);

System.out.println("Enter Day ");

int d=input.nextInt();

date.setDay(d);

System.out.println("Enter Year");

int y=input.nextInt();

date.setYear(y);

date.displayDate();

}

}

User Sepehr Mahmoudian
by
5.1k points