Question: Demo: package ArrayList; import java.util.ArrayList; public class HolidayList { //arraylist of object of Date class private ArrayList holidays; public HolidayList() { //everything is null holidays
Demo:
package ArrayList;
import java.util.ArrayList;
public class HolidayList {
//arraylist of object of Date class
private ArrayList holidays;
public HolidayList()
{
//everything is null
holidays = new ArrayList();
}
public HolidayList(ArrayList aListOfHolidays)
{
if(aListOfHolidays.isEmpty())
{
System.out.println("Error: empty");
System.exit(0);
}
holidays = new ArrayList();
for(int i = 0 ; i < aListOfHolidays.size();i++)
{
holidays.add(new Date(aListOfHolidays.get(i))) }
for(Date d: aListOfHolidays)
{
holidays.add(new Date(d));
}
}
//copy constructor
public HolidayList(HolidayList H)
{
holidays = new ArrayList();
//deep copy
for(Date d : H.holidays)
{ //deep copy
holidays.add(new Date(d));
}
}
//Returns contents of the object as a String that can be displayed
public String toString()
{
if(holidays.isEmpty())
{
return "No more holidays";
}
else
{
String S="";
for(Date d: holidays)
{
S= S + d.toString() + " ";
}
return S;
}
}
//accessor
// should return copy of member variable holidays[]
public ArrayList getHolidays()
{
ArrayList copyOfHolidays = new ArrayList();
holidays.trimToSize();
for(Date d: holidays)
{
copyOfHolidays.add(new Date(d));
}
return copyOfHolidays;
}
//Change a particular Date object in holidays using index
public void ChangeHoliday(int which, Date d)
{
if(which < 0 || which >=holidays.size() || d == null)
{
System.out.println("Error: Illegal or unused index");
System.exit(0);
}
holidays.set(which, d); // replacing
}
public Date getHoliday(int which)
{
return new Date(holidays.get(which));
}
//delete a particular holiday date in holidays by index
public void deleteHoliday(int which)
{
if(which < 0 || which >=holidays.size())
{
System.out.println("Error: Illegal or unused index");
System.exit(0);
}
holidays.remove(which);
}
public void addHoliday(Date newHoliday)
{
holidays.add(new Date(newHoliday));
}
public void addHoliday(Date newHoliday, int which)
{
if(which < 0 || which >=holidays.size())
{
System.out.println("Error: Illegal or unused index");
System.exit(0);
}
holidays.add(which, new Date(newHoliday));
}
//returns an arrayList of Data objects which has the same @param month
public ArrayList getHolidayByMonth(int month)
{
ArrayList FoundHolidays = new ArrayList();
for(Date d: holidays)
{
if(d.getMonthInt() == month)
{
FoundHolidays.add(new Date(d));
}
}
System.out.println("Number of holidays found in month " + month + " = " + FoundHolidays.size());
return FoundHolidays;
}
}
********************************************
Question 1:
Based on the demo, write a public method in the class 'HolidayList' that accepts two different 'Date' Objects (Date D1 and Date D2), and returns the number of holidays (in 'holidays' arraylist member variable) which occur between the two Date Objects D1 and D2. You can assume that D1 occurs before D2.
Question 2:
Based on the demo, write a public method in the class 'HolidayList' that accepts one 'Date' Object (Date D), and returns true if 'holiday' arraylist member variable contains an object with the same date as argument Date D, otherwise returns false.
Question 3:
Based on the demo, write a public method in the class 'HolidayList' that accepts one 'Date' Object (Date D), and returns all holiday dates ( in the 'holiday' arraylist member variable) that occur after the argument Date object D. Return type of the method should be "ArrayList "
Question 4:
Based on the demo, write a public method in the class 'HolidayList' that accepts one 'Date' Object (Date D), and returns all holiday dates ( in the 'holiday' arraylist member variable) that occur before the argument Date object D. Return type of the method should be "ArrayList"
Question 5:
Based on the demo for lecture 8 on BB, write a piece of java code in the main method to create an ArrayList of Date objects that contains the following three Date objects:
- month = 'January', day = 1, year = 2019
- month = 'November', day = 24, year = 2019
- month = 'March', day = 15, year = 2019
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
