Question

The Java class called Holiday is started below. An object of class Holiday represents a holiday...

The Java class called Holiday is started 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

public class Holiday {

private String name;

private int day;

private String month;

// your code goes here

}

  1. Write a first constructor for the class Holiday, which takes a String representing the name and sets the attribute name to this value.
  2. Write a second 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 instance variables to these values. This constructor should call the constructor of question A)
  3. Write a mutator/Accessors to all data attributes
  4. Write a method isSameMonth, 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.
  5. Write a method SameHolidayDate, which compares two instances of the class Holiday, and returns the Boolean value true if they have the same day, month, and false if they do not.

In a new class HolidayDemo with main method:

  1. Write a piece of code that creates a Holiday instance H1 with the name “Independence Day”, with the day “4”, and with the month “July”.
  2. Write a piece of code that creates a Holiday instance H2 with the name “Fourth of July”, with the day “4”, and with the month “July”.
  3. Check if H1 and H2 are reflecting the same holiday.
  4. Change the name, day and month of H2 to “christmas”, “12” and “December”
  5. Check if H1 and H2 fall in the same month.
  6. Write a piece of code that creates a Holiday instance H3 with the name “New Year’s Eve”, with the day “31”, and with the month “December”.
  7. Check if H2 and H3 fall in the same month.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program: In this program, we create a class Holiday in which we have three instance variables, two constructors , mutator/accessor methods and two boolean methods to check for same month and same holiday date. We have another class (driver class) HolidayTest in which we create the main() method and create objects of the class Holiday and test our methods.

Holiday.java:

Code:

public class Holiday {

    private String name;

    private int day;

    private String month;


    public Holiday(String name) {
        this.name = name;
    }

    public Holiday(String name, int day, String month) {
        this.name = name;
        this.day = day;
        this.month = month;
    }

    //Mutators and accessors
    //Set name
    public void setName(String name) {
        this.name = name;
    }

    //Get name
    public String getName() {
        return name;
    }

    //Set day
    public void setDay(int day) {
        this.day = day;
    }

    //Get day
    public int getDay() {
        return day;
    }

    //Set name
    public void setMonth(String month) {
        this.month = month;
    }

    //Get name
    public String getMonth() {
        return month;
    }

    //Check for same month
    public boolean isSameMonth(Object obj) {
        // check for if it the same reference
        if (this == obj) return true;

        // check if it is not instance of this class
        if (!(obj instanceof Holiday)) return false;

        // cast the compared object to Holiday class
        Holiday other = (Holiday) obj;

        //Return true if month is same
        return this.month.equals(other.month);
    }

    //Check for same holiday date
    public boolean SameHolidayDate(Object obj) {
        // check for if it the same reference
        if (this == obj) return true;

        // check if it is not instance of this class
        if (!(obj instanceof Holiday)) return false;

        // cast the compared object to Holiday class
        Holiday other = (Holiday) obj;

        //Return true if month and day are same
        return this.month.equals(other.month) && this.day == other.day;
    }
}

HolidayDemo.java:

Code:

public class HolidayDemo {
    public static void main(String[] args) {

        //Create new objects
        Holiday H1 = new Holiday("Independence Day", 4, "July");
        Holiday H2 = new Holiday("Fourth of July", 4, "July");

        //Check if they are the same holidays
        if (H2.SameHolidayDate(H1)) {
            System.out.print(H1.getName() + " and " + H2.getName() + " are ");
            System.out.println("same holidays");
        } else {
            System.out.print(H1.getName() + " and " + H2.getName() + " are ");
            System.out.println("different holidays");
        }

        //Set name, day and month
        H2.setName("Christmas");
        H2.setDay(12);
        H2.setMonth("December");

        //Check if they have same month
        if (H2.isSameMonth(H1)) {
            System.out.print(H1.getName() + " and " + H2.getName() + " fall in ");
            System.out.println("same month");
        } else {
            System.out.print(H1.getName() + " and " + H2.getName() + " fall in ");
            System.out.println("different months");
        }

        //Create new object
        Holiday H3 = new Holiday("New Year's Eve", 31, "December");
        //Check if they have same month
        if (H3.isSameMonth(H2)) {
            System.out.print(H2.getName() + " and " + H3.getName() + " fall in ");
            System.out.println("same month");
        } else {
            System.out.print(H2.getName() + " and " + H3.getName() + " fall in ");
            System.out.println("different months");
        }
    }
}

Output:

Note: In the main method() we have tried to make our output more informational, rather than just returning true or false.If we just need true and false values we can simply print the method calls:

HolidayTest.java:

Code:

public class HolidayDemo {
    public static void main(String[] args) {

        //Create new objects
        Holiday H1 = new Holiday("Independence Day", 4, "July");
        Holiday H2 = new Holiday("Fourth of July", 4, "July");

        //Check if they are the same holidays
        System.out.println(H1.SameHolidayDate(H2));
        //Set name, day and month
        H2.setName("Christmas");
        H2.setDay(12);
        H2.setMonth("December");

        //Check if they have same month
        System.out.println(H1.isSameMonth(H2));

        //Create new object
        Holiday H3 = new Holiday("New Year's Eve", 31, "December");
        //Check if they have same month
        System.out.println(H2.isSameMonth(H3));
    }
}

Output:

Add a comment
Know the answer?
Add Answer to:
The Java class called Holiday is started below. An object of class Holiday represents a holiday...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • public class Fish { private String species; private int size; private boolean hungry; public Fish() {...

    public class Fish { private String species; private int size; private boolean hungry; public Fish() { } public Fish(String species, int size) { this.species = species; this.size = size; } public String getSpecies() { return species; } public int getSize() { return size; } public boolean isHungry() { return hungry; } public void setHungry(boolean hungry) { this.hungry = hungry; } public String toString() { return "A "+(hungry?"hungry":"full")+" "+size+"cm "+species; } }Define a class called Lake that defines the following private...

  • Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will...

    Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will have four instance variables: • An instance variable called “year” which will be of type int • An instance variable called “month” which will be of type int • An instance variable called “day” which will be of type int • An instance variable called “description” which will be of type String The “Appointment” class must also implement the following methods: • A getter...

  • Create a java class for an object called Student which contains the following private attributes: a...

    Create a java class for an object called Student which contains the following private attributes: a given name (String), a surname (family name) (String), a student ID number (an 8 digit number, String or int) which does not begin with 0. There should be a constructor that accepts two name parameters (given and family names) and an overloaded constructor accepting two name parameters and a student number. The constructor taking two parameters should assign a random number of 8 digits....

  • 2. Define a class named “Holiday” that manages one holiday info such as month (integer), day...

    2. Define a class named “Holiday” that manages one holiday info such as month (integer), day (integer) and name (string). For example, 7, 4 and “Independence Day” • “toString” method to return holiday info as a string in the format: holiday name (month/day). For example, “Independence Day (7/4)” • “isLater” method that compares with another Holiday object and return true if the date of the holiday is later (month and day) and false otherwise. • “getMonth” method to return the...

  • Please help me with this code. Thank you Implement the following Java class: Vehicle Class should...

    Please help me with this code. Thank you Implement the following Java class: Vehicle Class should contain next instance variables: Integer numberOfWheels; Double engineCapacity; Boolean isElectric, String manufacturer; Array of integers productionYears; Supply your class with: Default constructor (which sets all variables to their respective default values) Constructor which accepts all the variables All the appropriate getters and setters (you may skip comments for this methods. Also, make sure that for engineCapacity setter method you check first if the vehicle...

  • House Class Specification The House class represents a house. A house has an address (address instance...

    House Class Specification The House class represents a house. A house has an address (address instance variable), a year it was built (built instance variable), and the names of residents of the house (residents instance variable). The declaration of each variable follows. private String address; private int built; private StringBuffer residents; The class methods are: 1. Constructor - Takes a string (representing the address) and an integer (representing the year the house was built) as parameters, and initializes the corresponding...

  • Java Time Class Class declarations are one of the ways of defining new types in Java....

    Java Time Class Class declarations are one of the ways of defining new types in Java. we will create two classes that both represent time values for a 24 hours period. The valid operations are as follows. int getHours(): returns the number of hours int getMinutes(): returns the number of minutes int getSeconds(): returns the number of seconds String toString(): returns a String representation boolean equals(Time other): returns true if and only if other designates an object that has the...

  • Writing 3 Java Classes for Student registration /** * A class which maintains basic information about...

    Writing 3 Java Classes for Student registration /** * A class which maintains basic information about an academic course. */ public class Course {    /** * Attributes. */ private String code; private String title; private String dept; // name of department offering the course private int credits; /** * Constructor. */ public Course(String code, String title, int credits) { // TODO : initialize instance variables, use the static method defined in // Registrar to initialize the dept name variable...

  • In Java, Write a class encapsulating a restaurant,which inherits from Store. A restaurant has the following...

    In Java, Write a class encapsulating a restaurant,which inherits from Store. A restaurant has the following additional attributes: how many people are served every year and the average price per person. code the constructor, accessors, mutators, toString and equals method of the new subclass; also code a method returning the average taxes per year. You also need to include a client class to test your code for both the parent class and the subclass. Code for Store below(Super class aka...

  • Java Homework Help. Can someone please fix my code and have my program compile correctly? Thanks...

    Java Homework Help. Can someone please fix my code and have my program compile correctly? Thanks for the help. Specifications: The class should have an int field named monthNumber that holds the number of the month. For example, January would be 1, February would be 2, and so forth. In addition, provide the following methods. A no- arg constructor that sets the monthNumber field to 1. A constructor that accepts the number of the month as an argument. It should...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT