Hello. I have this question:

"Amend the subclass of Holiday called Premier with an overridden equals method. Test this method in an orchestrating
class with objects of Premier that contain the same data values and with objects that
contain different data values."

I have searched high and low for a good explanation of overriding and how to achieve it but it refuses to click for me. Could someone please (using my code) explain how I go about achieving the answer to the question. Or point me in the right direction.

So far I have as follows:

Holiday.java

package Holiday;

//<---------------------------------------------------------------------------------------------------------------------------------------------------------
public class Holiday {

    static String startDate;
    static String endDate;
    static String place;
    static String price;

    static String hotelGrade;
    static String hotelName;
    static String resortName;
//<--------------------------------------------------------------------------------------------------------------------------------------------------------- 
public Holiday(String startDate2, String endDate2, String place2, String price2){



    startDate = startDate2;
    endDate = endDate2;
    place = place2;
    price = price2;

    }

//<---------------------------------------------------------------------------------------------------------------------------------------------------------
public static void setStartDate(String sDate){

    startDate = sDate;

    }

public static String getStartDate(){

    return startDate;

    }

public static void setEndDate(String eDate){

    endDate = eDate;

}

public static String getEndDate(){

    return endDate;

}

public static void setPlace(String pla){

    place = pla;

}

public static String getPlace(){

    return place;

}


public static void setPrice(String pri){

    price = pri;

}

public static String getPrice(){

    return price;

}


//<---------------------------------------------------------------------------------------------------------------------------------------------------------
public static void main(String[] args){

//  HolidayOrch.setHol();                                         // UNCOMMENT FOR QUESTION 2
//  
//  
//  System.out.println("Holiday One:\n");
//  System.out.println("The Start Date: " + getStartDate());
//  System.out.println("The End Date: " + getEndDate());
//  System.out.println("The Location: " + getPlace());
//  System.out.println("The Price: " + getPrice()+"\n" + "\n");
//  
//  
//  HolidayOrch.setHol2();
//  
//  System.out.println("Holiday Two:\n");
//  System.out.println("The Start Date: " + getStartDate());
//  System.out.println("The End Date: " + getEndDate());
//  System.out.println("The Location: " + getPlace());
//  System.out.println("The Price: " + getPrice());

    HolidayOrch.setPremierAndHol();

    System.out.println("Holiday & Premier Information:\n");
    System.out.println("The Start Date: " + getStartDate());
    System.out.println("The End Date: " + getEndDate());
    System.out.println("The Location: " + getPlace());
    System.out.println("The Price: " + getPrice());
    System.out.println("The Hotel Grade: " + Premier.getHotelGrade());
    System.out.println("The Hotel Name: " + Premier.getHotelName());
    System.out.println("The Resort Name: " + Premier.getResortName());
}
//<---------------------------------------------------------------------------------------------------------------------------------------------------------


}

Premier.java:

package Holiday;

public class Premier extends Holiday{

//<--------------------------------------------------------------------------------------------------------------------------------
    public Premier(String hotelGrade2, String hotelName2, String resortName2, String startDate2, String endDate2, String place2, String price2) {

        super(startDate2, endDate2, place2, price2);
        hotelGrade = hotelGrade2;
        hotelName = hotelName2;
        resortName = resortName2;   

    }


    public static void setHotelGrade(String hotelGrade2){
        hotelGrade = hotelGrade2;   
    }

    public static String getHotelGrade(){
        return hotelGrade;
    }

    public static void setHotelName(String hotelName2){
        hotelName = hotelName2;
    }

    public static String getHotelName(){
        return hotelName;
    }

    public static void setResortName(String resortName2){
        resortName = resortName2;
    }

    public static String getResortName(){
        return resortName;
    }
//<--------------------------------------------------------------------------------------------------------------------------------  
}

HolidayOrch.java

package Holiday;

public class HolidayOrch {

//<--------------------------------------------------------------------------------------------------------------------------------
public static void main(String[] args){

        //setHol();//UN COMMENT FOR QUESTION 2
        //setHol2();//UN COMMENT FOR QUESTION 2
        setPremierAndHol();
    }
//<--------------------------------------------------------------------------------------------------------------------------------
public static void setHol() {

        Holiday hol = new Holiday(null, null, null, null);

        Holiday.setStartDate("1/1/1990");
        Holiday.setEndDate("2/2/1992");
        Holiday.setPlace("England");
        Holiday.setPrice("£500");
    }
//<--------------------------------------------------------------------------------------------------------------------------------  
public static void setHol2() {

        Holiday hol2 = new Holiday(null, null, null, null);

        Holiday.setStartDate("5/5/1995");
        Holiday.setEndDate("6/6/1996");
        Holiday.setPlace("Scotland");
        Holiday.setPrice("£200");
    }   
//<--------------------------------------------------------------------------------------------------------------------------------          
public static void setPremierAndHol(){

        Premier prem1 = new Premier(null, null, null, null, null, null, null);

        Holiday.setStartDate("10/10/2000");
        Holiday.setEndDate("11/11/2100");
        Holiday.setPlace("Germany");
        Holiday.setPrice("£900");
        Premier.setHotelGrade("****");
        Premier.setHotelName("Java Hotel");
        Premier.setResortName("C++ Garden");
    }
//<--------------------------------------------------------------------------------------------------------------------------------   
}

Many thanks for your help in this matter.

P.S I also have one other minor issue with another project, should I make a new topic or is two topics in two minutes against etiquete?

Recommended Answers

All 11 Replies

P.S I also have one other minor issue with another project, should I make a new topic or is two topics in two minutes against etiquete?

I don't know Java but please start a new thread for a new question. :)

This tutorial is about as simple as it gets:
http://www.tutorialspoint.com/java/java_overriding.htm

In your code you have made everything static - that means you can only have one value for the dates, price etc and those values are shared between all the holidays. That's obviously wrong. Every instance of Holiday should have its own dates. price etc. The only thing that should be static is the main method.

ps: You should be greatly honoured to get a post from Queen cscgal - it's incredibly rare for her to speak to one of us ordinary people!

Thank you, I am reading through them now, hopefully it'll clear some things up!

When I delete all the static references but the static void main it gives me this error on all my orchestrating methods:
"Cannot make a static reference to a non-static method"

But I removed all static?

When you have lines like
System.out.println("The Start Date: " + getStartDate());
getStartDate is an instance method (ie not a static method) so you need an instance of the class for the method call, If your code was in an instance method then the current instance would be used, but main is a static method that (by definition) doesn't have a current instance. So you have to provide an instance explicitly, eg

Holiday hol = new Holiday(null, null, null, null);
hol.setStartDate("1/1/1990");  // not Holiday, setStartDate is not a static method
System.out.println("The Start Date: " + hol.getStartDate());
commented: thanks +0

public void setHol() {

        Holiday hol = new Holiday(null, null, null, null);

        hol.setStartDate("1/1/1990");
        hol.setEndDate("2/2/1992");
        hol.setPlace("England");
        hol.setPrice("£500");
    }

Editing my code accordingly but now in my main method I get the exact same error for my:

setHol();

Going to have a play around with it now.

I've read all the articles linked, and in the examples provided it makes sense. But when applied to my problem:

"Amend the subclass of Holiday called Premier with an overridden equals method. Test this method in an orchestrating
class with objects of Premier that contain the same data values and with objects that
contain different data values.
"

It's back to square one again, could you give me a brief overview of what they're asking if you wouldn't mind?

Thank you for your help so far.

I get the exact same error

Please post the full text of the error message

"Cannot make a static reference to a non-static method setHol(); from the type HolidayOrch"

Any more insight would be great, thanks!

what is the line that is giving that error? please post it here.
if you are trying to use an instance of the class to call a static method from within the class it will not work you would have to call it using the class reference if the method is static, only non-static methods are called using an initiated instance of the class

"Cannot make a static reference to a non-static method setHol(); from the type HolidayOrch"

This is exactly the same type of problem as you had before, and you solve it in exactly the same way. Please re-read my previous post

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.