#Equals method

1 messages · Page 1 of 1 (latest)

kindred jackalBOT
#

<@&987246399047479336> please have a look, thanks.

kindred jackalBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

dusty folio
#

and what's ur question?

deep shore
#

Hey!!

public abstract class C
{
    protected int _cVal;
    
    public C()
    {
        _cVal = 2;
    }
    public C(int cVal)
    {
        _cVal = cVal;
    }
    public abstract boolean foo (int n);
}

/////////////////

public class B extends C
{
    // instance variables - replace the example below with your own
    protected int _bVal;
    
    public B(int cVal, int bVal)
    {
        _bVal = bVal;
    }
    public boolean foo(int n){
        return n==_cVal;
    }
      
}

Implement the equals method in the classes.
Objects of type C must be compared according to the _cVal attribute,
and objects of type B should be compared according to the attributes _cVal and _bVal.

Write the equals method that should be in class C:
public boolean equals (Object obj)
{
if ______
return false;
return _______
}

Write the equals method that should be in class B:
public boolean equals (C obj)
{
if _______
return false;
return _______
}

kindred jackalBOT
deep shore
#

sorry i accidently deleted my post after i tried to edit it

#

i have a uni task and that's the only question i got

#

i have several options

dusty folio
#

what's unclear to u?

#

the method is supposed to return true iff u think this and that are the same

deep shore
#

i tried to test it in some way but then i remembered i can't write objects for an abstract class

dusty folio
#

so u compare what u want to compare and then return true or false. very straightforward

deep shore
#

that's my options for if

#

thats my options for return

dusty folio
#

and what's unclear to u?

deep shore
#

i'm very not sure if thats right because i've no way to test it

dusty folio
#

just type it out in ur ide and test it

deep shore
#

its compiled but i can't test it because C is an abstract class

dusty folio
#

then extend it

#

implement the foo method trivially

#

then u can test it

deep shore
#

like

#

to test the equals of foo?

dusty folio
#

to test the equals of C

#

extend C with a non abstract class D

deep shore
#

i also have a class B that extends C

dusty folio
#

i know. but that one has its own equals method

#

and u want to test the original

#

from C

deep shore
#

so i create class D with the code
public boolean foo(int n){
return n==_cVal;
}
?

#

did i understand?

dusty folio
#

it doesn't matter what u do in the method. u can just not do anything

#

foo is irrelevant to all of this

deep shore
#

i think i'm lost lol

dusty folio
#

what's unclear to u

deep shore
#

the explanation

#

i'm still not sure whats the code i should write to make the method equals

dusty folio
#

because u don't understand it and are just guessing?

#

or because u think its right but want to test it but can't bc abstract?

#

or what

wild atlas
#

the question is a bit awkward since objects of type C can't exist

dusty folio
#

well, they can. any subclass is a type C as well

deep shore
#

haha

wild atlas
#

but then in a subclass you will rewrite the equals method anyway if you add new fields

dusty folio
#

and unless they override equals as well, it's relevant

#

not necessarily

deep shore
dusty folio
#

it can make sense. and whether it does or not is irrelevant. it's the task at hand and it's as hard to solve it. and simple to test it

dusty folio
#

im not sure what was unclear

wild atlas
#

well, technically you can subclass C and not add anything that might affect the equals check. so the answer is correct

dusty folio
#

heck, u could even just remove the abstract from it and delete the foo method. then u can test it as well

deep shore
dusty folio
#

that's not the point

#

u want to test it. so test it

#

open ur ide, type it out and play with it

#

until u understand what the correct answer is and can then select it

#

if u don't know the correct answer yet, u essentially failed the test and should step back and play around more until u understand it

#

so type it out and see what works and what not. inspect what's going until ur sure

deep shore
#

thanks anyways for the help