• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

RHE question - equals() PL HELP

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear friends,
You are given the class file for a class called Sssh. However, you do not have the source code or any information about the internals of the Sssh class. You do know that the class has a protected int variable called i. What does the following application print out?
1. class Mine extends Sssh {
2. int z;
int i;
3. public static void main(String[] args) {
4. Mine m1 = new Mine(); m1.i = 5; m1.z = 12;
5. Mine m2 = new Mine(); m2.i = 5; m2.z = 12;
6. if (m1.equals(m2))
7. System.out.println("YES");
8. else
9. System.out.println("NO");
10. }
11.
12. public boolean equals(Object ob) {
System.out.println(z); // Line 1
13. if (z != ((Mine)ob).z)
14. return false; // line 2
15. return super.equals(ob); //line 3
16. }
17. }
Ans: IT IS IMPOSSIBLE TO KNOW.
problem:
1. why is the answer impossible to know.
2. Line1 access which instance of z , ie, m1.z or m2.z . I tried giving a print statement and found that it accessed m1.z(Here the values are same. I gave diff values) . Why is it is accessing m1.z value?
3. Line 2 where is this return going to (I mean it is returning to which line?)
4. Line 3 -- can there be 2 return statements.
Hi ranchers, I had made a similar posting sometime back,but couldn't understand. Please help me out.
Thanks in advance
Padmini

[This message has been edited by padmini Babu (edited June 23, 2001).]
 
padmini Babu
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Scott, Cindy, Manfred, Jane are you all there ? please help me.
Padmini
 
padmini Babu
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by padmini Babu:
Hello Scott, Cindy, Manfred, Jane are you all there ? please help me.
I am giving the exam on 2nd . Please bear with me if I am too impatient.
Thanks
Padmini


 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. class Mine extends Sssh {
2. int z;
int i;
3. public static void main(String[] args) {
4. Mine m1 = new Mine(); m1.i = 5; m1.z = 12;
5. Mine m2 = new Mine(); m2.i = 5; m2.z = 12;
6. if (m1.equals(m2))
7. System.out.println("YES");
8. else
9. System.out.println("NO");
10. }
11.
12. public boolean equals(Object ob) {
System.out.println(z); // Line 1
13. if (z != ((Mine)ob).z)
14. return false; // line 2
15. return super.equals(ob); //line 3
16. }
17. }
Ans: IT IS IMPOSSIBLE TO KNOW.
problem:
1. why is the answer impossible to know.
ans. we don't know about super class equals method.
2. Line1 access which instance of z , ie, m1.z or m2.z . I tried giving a print statement and found that it accessed m1.z(Here the values are same. I gave diff values) . Why is it is accessing m1.z value?
ans. you are calling equals() metod on instance m1,so 'this'
points to m1. Try with m2.equals(m1).
3. Line 2 where is this return going to (I mean it is returning to which line?)
ans. return type is boolean here,so if you give
boolean b=m1.equals(m2);//b will get the return value
4. Line 3 -- can there be 2 return statements.
ans. it can contain any number of return statements,but take
care that all statements are reachable
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic