Results 1 to 6 of 6
  1. #1
    tonmachine is offline Member
    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    Default overloading an equals method

    My assignment for class is to create a base class and a subclass, and each one has to have an equals method. Both equals methods are meant to iterate through the indices of an array, but the base class method is for integers and the derived class method is for strings. The equals method in the derived class is supposed to overload the equals method in the base class, but not override it. Right now, the equals method I have in my base class is:

    public void equals(int[] equalsList) {
    boolean found = false;
    for (int i = 0; i < numbers.length; i++) {
    equalsList[i] = numbers[i];
    }
    for (int i = 0; i < equalsList.length; i++) {
    for (int j = 1; j > i && j < equalsList.length; j++) {
    if(Integer.toString(equalsList[i]).equalsIgnoreCase(Integer.toString(equalsList[j]))) {
    found = true;
    break;
    }
    }
    if (found) {
    System.out.println("You entered a duplicate number. Please restart and only enter each number once.");
    System.exit(0);
    }
    }
    }

    When I try to pass an array as a parameter in the derived class (ex. super.equals(priorities);, where priorities is an array containing a list of integers), it doesn't work and I still get duplicate numbers. I was able to make it work when I had no parameters at all for the base class equals method, but I need it to have a parameter so it can be overloaded by the equals method of the derived class. Does anyone have any suggestions?

    Thanks!!

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: overloading an equals method

    First, it doesn't look like you are even calling your equals method. Second, an equals method must return true or false as an indicator of equality. Your method signature says it doesn't expect a return value.

    I might be more helpful if I saw all the code. And please put your code between [code][/code] tags.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: overloading an equals method

    tonmachine, you were told about the code tags here, but then you didn't have the courtesy to either continue or end the discussion on that thread.

    If you're not going to read and reply to responses, why bother to post on a forum?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    tonmachine is offline Member
    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    Default Re: overloading an equals method

    That is all the code I have for the first equals method. Then in the derived class I have an array of integers named priorities that is supposed to take numbers entered by the user and then store them in the array. I'm trying to use the equals method from the base class to check that array of integers for duplicate numbers and then display a message if the user entered the same number twice. But when I write super.equals(priorities) in the derived class, it doesn't work. I know how to write boolean equals methods comparing two objects, but not indices within an array...I think that's why I'm stuck.

    And Darryl, I didn't reply because I figured out the answer. I'm not going to continue a conversation on a forum when I have my problem fixed.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: overloading an equals method

    Quote Originally Posted by tonmachine View Post
    And Darryl, I didn't reply because I figured out the answer. I'm not going to continue a conversation on a forum when I have my problem fixed.
    Which is exactly why you should reply. It provides closure for folks who may be thinking about this and haven't responded yet. It also provides help for folks who search the forum. If you figure out the answer you need to post it so others may benefit. And it's always courteous to finish up by thanking the person or people who helped you.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: overloading an equals method

    Quote Originally Posted by tonmachine View Post
    I'm not going to continue a conversation on a forum when I have my problem fixed.
    Recommended reading. Wouldn't hurt to go through the rest of the page too.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. What is use of method overloading and overriding
    By srinivasmallabathula in forum New To Java
    Replies: 3
    Last Post: 04-06-2011, 07:07 AM
  2. Method Overloading - Doubt
    By vidya lakshman in forum New To Java
    Replies: 4
    Last Post: 02-01-2011, 06:47 AM
  3. Method Overloading Verification
    By lala in forum New To Java
    Replies: 22
    Last Post: 11-30-2010, 05:32 AM
  4. method overloading and arrays.
    By glopez09 in forum New To Java
    Replies: 5
    Last Post: 12-08-2009, 03:02 PM
  5. Method/Operator Overloading
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-01-2007, 08:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •