Question
In Java: Holiday:An object of class Holiday represents a holiday during the year. This class has three instance variables: name, which is a String representing
In Java:
Holiday: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
public class Holiday {
private String name
private int day
private String month
// your code goes here
}
- 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.
- Write a static 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. Show how it is used
- Write a static 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). Show how it is used
- Write getters and setters to access the name, day, and month fields
- Write a method toString that returns the name of the holiday followed by the dat For example, if in the Holiday object name = Mayday, month = May, and day = 1, then toString should return Mayday: May 1
- Write a piece of code that creates a Holiday instance with the name Independence Day, with the day 4, and with the month July.
Here is the UML:
Once again, in standard UML parlance, + indicates that a field or method is public and - indicates that a field or method is private. An underlined field or method indicates it is static.
I have written the code to create a few holidays and put them in an array to get you started so you can test out the methods.
public class Holiday { private String name; private int day; private String month; public static void main(String[] args) { Holiday[] holidays = new Holiday[5]; holidays[0] = new Holiday("May Day", 1, "May"); } }
Holiday -name: String -day: int -month: String +Holiday(name: String, day:int, month: String) +inSameMonth(h1 Holiday, h2:Holiday): boolean +avgDate(holidays: Holidayl): double +getName(): String +setName(name: String) +getDay(): int +setDay(day: int) +getMonth(): String +setMonth(month: String) +toString(): StringStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started