Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Beginning Java and the fly likes Need Help please Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need Help please" Watch "Need Help please" New topic

Need Help please

MK Barman

Joined: Dec 14, 2004
Posts: 3
can anyone tell me what the mistakes in this class?
Thanks in advance!

// 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");

Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

// 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 ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

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?

[Jess in Action][AskingGoodQuestions]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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!


Java API Documentation
The Java Tutorial
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
another hint: pass by ?

I agree. Here's the link:
subject: Need Help please
It's not a secret anymore!