#Equals method
1 messages · Page 1 of 1 (latest)
While you are waiting for getting help, here are some tips to improve your experience:
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.
and what's ur question?
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 _______
}
Detected code, here are some useful tools:
my question is, how do i make it equal?
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
what's unclear to u?
the method is supposed to return true iff u think this and that are the same
i tried to test it in some way but then i remembered i can't write objects for an abstract class
so u compare what u want to compare and then return true or false. very straightforward
that's what i chose for C
that's my options for if
thats my options for return
and what's unclear to u?
i'm very not sure if thats right because i've no way to test it
just type it out in ur ide and test it
its compiled but i can't test it because C is an abstract class
i also have a class B that extends C
i know. but that one has its own equals method
and u want to test the original
from C
so i create class D with the code
public boolean foo(int n){
return n==_cVal;
}
?
did i understand?
it doesn't matter what u do in the method. u can just not do anything
foo is irrelevant to all of this
i think i'm lost lol
what's unclear to u
the explanation
i'm still not sure whats the code i should write to make the method equals
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
the question is a bit awkward since objects of type C can't exist
well, they can. any subclass is a type C as well
thats what i'm saying
haha
but then in a subclass you will rewrite the equals method anyway if you add new fields
yes i tried to test my options but as i said i couldn't make a tester
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
i explained to u how u can do it
im not sure what was unclear
well, technically you can subclass C and not add anything that might affect the equals check. so the answer is correct
heck, u could even just remove the abstract from it and delete the foo method. then u can test it as well
i'm not sure if thats allowed lol
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
thanks anyways for the help