• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need Help please

 
MK Barman
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
can anyone tell me what the mistakes in this class?
Thanks in advance!

// NOTES:
// 1. There are several mistakes that cause this class does not behave
// properly, including silly mistakes, (what some have found to be) subtle mistakes,
// and mistake(s) of omission.
// The goal of this exercise is to find and correct
// as many of those mistakes as possible.
// 2. The "main" method is just a quick-and-dirty "unit test" of
// the class. While the "main" method may or may not be a *thorough* test
// of the functionality of this class, it is a *valid* test,
// so there is no need to fix the "main" method.
// 3. One clue to the intent of this class is to try to determine
// what *should* print out at the end of the "main" method.

/*
* This class is designed to be a key into hash tables so that
* elements can be "looked up" if the two specified strings match.
*/

package code_examples;

import java.util.HashMap;


public class BadKey {
String key1;
String key2;

public BadKey(String key1, String key2) {
key1 = this.key1;//use this.key1
key2 = this.key2;//use this.key2
}

public boolean equals(BadKey other) {
return (other.key1 == this.key1) &&
(other.key2 == this.key2);
}


public static void main(String[] args) {
HashMap map = new HashMap();
BadKey badKey1 = new BadKey("test1", "test2");

map.put(badKey1, "value");

BadKey badKey2 = new BadKey("test1", "test2");

System.out.println(map.get(badKey2));
}
}
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

// The goal of this exercise is to find and correct
// as many of those mistakes as possible

Well, as I'm not the person being tested - wouldn't I be subverting the goal of this exercise by telling you?

Asking for answers to assignment questions is not in the spirit of these forums, so though someone may well give you the answer you need, but its not likely.
[ January 18, 2005: Message edited by: Paul Sturrock ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi MK,

Welcome to JavaRanch!

This isn't even close to what I'd call an advanced question; I would call this one a beginner question. Therefore, I'm going to move this to the Java in General (Beginner) forum for further discussion.

But let me warn people not to simply hand over the answers: just give hints.

I'll start: how does the assignment operator "=" work in Java?
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This looks strangely like a homework assignment. This means you are supposed to learn something from it. If we just do the assignment for you, then how are you supposed to learn?

Anyway, enough of my soapbox. I would suggest that you compile and run the program. You probably need to know what the program *should* do and then you can compare it to what the program actually does. It also will help if pretend that you are the computer and "execute" the program by hand. By this I mean that you should make a table with the variables. Look at each line of code (in the order the computer will execute it) and update the value of each variable in your table as they change.

If there are any specific issues that you run into as you do this, let us know. We will be more than happy to clarify any specific questions you have.

Keep Coding!

Layne
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
another hint: pass by ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic