2.2 The following sub-questions are interdependent. This means that, to derive a
ID: 3906667 • Letter: 2
Question
2.2 The following sub-questions are interdependent. This means that, to derive a solution to the current question, you might need to refer back to your response to the previous question(s). For each question, write down code segment that perform the specified action. (a) Define a Java class called Date which has 3 properties: month, day, and year. (b) The Date class should, in addition, provides the following methods: getter and a setter for the data field year only (i.e. no need to write getters and setters for the other data fields); i. i. a method called isLeapYear which determines if the year contained in the data field year is a leap year. A leap year is a year that is divisible by 4 and also divisible by 100. Note however every year divisible by 100 is NOT a leap year, unless the year is also divisible by 400. This means, for instance, year 1800, 1900, 2100, 2200, 2300 and 2500 are NOT leap years, while year 2000 and 2400 are leap years;Explanation / Answer
(A)
class Date
{
private int day,month,year;
public Date()
{
}
public Date(int day,int month,int year )
{
this.day=day;
this.month=month;
this.year=year;
}
}
(B)
(i)
public void setYear(int year)
{
this.year=year;
}
public int getYear()
{
return this.year;
}
(ii)
public void isLeapYear()
{
if(this.year%4==0&&this.year%100!=0||this.year%400==0)
System.out.println("leap year..");
else
System.out.println("not a leap year");
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.