Sheet 9
Sheet 9
Sheet 9
1. Write a Java class called Holiday as stated below. An object of class Holiday represents a
holiday during the year. This class has three instance variables:
name, which is a String representing the name of the holiday
day, which is an int representing the day of the month of the holiday
month, which is a String representing the month the holiday is in.
a. Write a constructor for the class Holiday, which takes a String representing the name,
an int representing the day, and a String representing the month as its arguments, and
sets the class variables to these values.
b. Write a method inSameMonth, which compares two instances of the class Holiday,
and returns the Boolean value true if they have the same month, and false if they do
not.
c. Write a method avgDate which takes an array of base type Holiday as its argument,
and returns a double that is the average of the day variables in the Holiday instances
in the array. You may assume that the array is full (i.e. does not have any null
entries).
d. Write a piece of code that creates a Holiday instance with the name “Independence
Sina Day”, with the day “25”, and with the month “April”.
public class Holiday {
String name;
int day;
String month;
public Holiday(String n,int d,String m)
{
name = n;
day = d;
month = m;
}
public boolean inSameMonth(Holiday h)
{
if(h.month.equals(month))
return true;
return false;
}
public static double avgDate (Holiday[] a)
{
double total = 0;
for(int i=0;i<a.length;i++)
{
total = total + a[i].day;
}
return total;
}
}
}