Jump to content

Comparing two String variables, each from a different object...

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
ZipOnTrousers

ZipOnTrousers

    Learning Programmer

  • Validating
  • PipPipPip
  • 94 posts
Hi, my code:

    public void argue(ProgrammerDuck otherDuck) {
        //Creates a new duck and asks what its favourite language is.
        System.out.println("What is your favourite language, other duck?");
        otherDuck.proclaim();
        if (this.getFavProgLang().equals(otherDuck.getFavProgLang()) {
            System.out.println("I agree!");
        }
        else {
            for (int i = 0; i < 54; i++) {
                this.quack();
            }
      

In theory, this should create a new object "otherDuck" and then the real point of the method is to compare whether the "otherDuck"s variable favProgLang is the same as this.ProgrammerDucks favProgLang. I was wondering if anyone could suggest why the syntax I'm using doesn't work and if there's another method I could use that functions as I think this should.

#2
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
I understand, that there's Duck in the method argument and this method belongs in the Duck object itself, but there's nothing in this code, that should make a new duck object.

I have no idea, what does method proclaim() do, but this code should work fine, when you are comparing two ducks' FavProgLang().

Could you explain a little more, what do you mean, by creating new object?

#3
ZipOnTrousers

ZipOnTrousers

    Learning Programmer

  • Validating
  • PipPipPip
  • 94 posts
Ok:

This method is in a class called ProgrammerDuck which is a subclass of Duck. The class initializes one ProgrammerDuck and then this method's parameter creates ANOTHER ProgrammerDuck called otherDuck. All of the ProgrammerDuck's variables (including FavProgLang) is set in the constructor.

All the other methods such as proclaim are basically just methods to print things to the screen and they all work, so you can pretty much ignore them. Its really just the comparing part that I'm having problems with.

Does this clear anything up?

N.B. The values for the constructor in the parameter will be set in the main method, we use the main method solely to set up the methods of the class with test data in my current programming class.

#4
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts

Quote

The class initializes one ProgrammerDuck and then this method's parameter creates ANOTHER ProgrammerDuck called otherDuck.
This method does not contain the code to create a new ProgrammerDuck.

Well, your comment says:
//Creates a new duck...

There's nothing there, that creates a new duck..
I can see that otherDuck comes from an argument, and isn't made inside this method.

Quote

this method's parameter creates ANOTHER ProgrammerDuck called otherDuck.

Whenever new instance of object is made, it's called by "new" keyword. Can i see the part, where otherDuck actually is made(in your main/test class)?

Comparison seems to be right. I believe the error is somewhere else... you should print out both duck's "favLanguages" and see yourself, if they match.

#5
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
wait.... does you program even compile?

This seems to be missing parenthese from the end:

if (   
        this.getFavProgLang()
        .equals(    otherDuck.getFavProgLang()    ) 

   // WHERE DOES IF-CLAUSE END??
{

correct:
if (this.getFavProgLang().equals(otherDuck.getFavProgLang())){


#6
ZipOnTrousers

ZipOnTrousers

    Learning Programmer

  • Validating
  • PipPipPip
  • 94 posts
I was missing a bracket. Theres half an hour wasted.

Cheers, -Zip