2

I'm struggling to suss something out with Java. How do I use the method giveCode() to return the integer value for an object. This is code that was pre written by our lecturer, and we have to use it to return an integer value for an object, it's to be used when creating hash values for a hash table.

HashCode.java

// Interface for HashCode. Has only one method declaration
public interface HashCode {

    // For an input object, computes and returns its HashCode as an integer
        public int giveCode(Object o);

}

StringHashCode.java (I created this class as we are supposed to implement the HashCode class into this class)

public abstract class StringHashCode implements HashCode{


} 

I've tried things like this in my main, just to simply return the code value, so I can see how it works, but nothing seems to be working:

System.out.println(new StringHashCode.giveCode(example_object));

Or

System.out.println(new StringHashCode.giveCode(example_object.hashCode()));

Or

System.out.print(HashCode.giveCode(example_object));

I'm quite new to Java, but I have a lot of experience with other OOP languages, but I can't seem to grasp the issue with this one, I feel like it's probably something stupid, but I can't suss it out.

3
  • 1
    What does not working mean? What are your expectations and what are the actual results? Commented Oct 15, 2013 at 23:16
  • You can't create instances of abstract class. You need a concrete class. Your examples won't even compile. I suggest you write some code which will compile first. Commented Oct 15, 2013 at 23:16
  • You're missing two very important characters called parenthesis: new StringHashCode() Commented Oct 15, 2013 at 23:17

3 Answers 3

1

Since your lecturer gave you that interface you have to implement it. And that could be a class. It can be another interface which you might not need, Abstract class is not useful for your solution so use simple class and implement that interface.

class HashCodeImpl implements HashCode {
    @Overwide
    public int getCode(Object obj) {
        return obj.hashCode();
    }
}

and in main method this class hould be used as

class Main {
    public static void main(String[] args){
        HashCodeImpl impl = new HashCodeImpl();
        System.out.println(impl.getCode(new String()));
    }
}

Now in your question I don't know how you are not getting any output because none of your system out will compile.

 System.out.println(new StringHashCode.giveCode(example_object));

giveCode method of this class is not static so this won't compile

 System.out.println(new StringHashCode.giveCode(example_object.hashCode()));

same reason giveCode is not static so it won't compile and you have to pass in a object and class suppose to return has code. In this case you are passing hash code into the method.

 System.out.print(HashCode.giveCode(example_object));

Compilation error since HashCode is an interface like a prototype which you have to implement to create concrete class .

Sign up to request clarification or add additional context in comments.

4 Comments

Haha, yeh I gathered. The issue I've been having also is that when I make the class a non abstract class it tells me that the class needs to be abstract or Overridden. But even when I add your override it tells me the same thing and gives me an error for the Override.
@ConnerStephenMcCabe For an override, the return type and parameter types must be exactly the same (except for covariance which doesn't apply here). Did you try to override with something declared exactly as public int giveCode(Object o)? P.S. Looks like the answerer misspelled it getCode, so if you cut-and-pasted it, it will be wrong.
@ajb Derp, thanks for the heads up, that solved the issue, haha! Should have thoroughly examined the code before assuming that it didn't work. Cheers.
@user2880879 The upper half of your answer is correct but your comments to why the three different lines with System.out.println doesn't work aren't correct.
0
  1. You're trying to instantiate the class without using ()s. You'd want new StringHashCode().
  2. You're trying to instantiate an abstract class, which is impossible.
  3. You haven't implemented the giveCode method anywhere, so naturally you can't call it.

Comments

0
  1. Make the class non-abstract
  2. Write the method implementation
  3. You're missing parenthesis

Fix these problems and it will work better for you.

 public class StringHashCode implements HashCode{

    public int giveCode(Object o) {
        return o.hashCode(); // Replace this with your own implementation
    }
} 


System.out.println(new StringHashCode().giveCode(example_object));

3 Comments

The giveCode section isn't a method as far as I'm aware, it's simply a variable that is declared with an integer value of an object. Also. returning 42 would be of no use, as the code that is returned is supposed to be unique.
@ConnerStephenMcCabe The giveCode section is a method in the HashCode interface in the OP's question. And it's definitely not a variable. Of course there's no use in returning 42, that was intended as a // TODO: Replace with your own implementation but I changed that now to use an implementation which makes much more sense.
@ConnerStephenMcCabe I'm sorry, just realized now that you are the OP. What makes you think that the giveCode-"thingy" isn't a method? When it's declared that way in an interface, it specifies that all instances of the interface must contain that method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.