• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getting to a 300 answer to compile

 
donald rieck
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm getting a:

java:25: cannot find symbol
symbol : method addMe(int)
location: class MyChild
return addMe(mychi.x + mychi.y + mychi.z);

this is the piece of code starting at line 24:

public int addMe(MyChild myChi) {
return addMe(myChi.x + myChi.y + myChi.z);
}

Here is the full code:


class MyParent {
int x,y;
MyParent(int x, int y) {
this.x = x;
this.y = y;
}
public int addMe(int x, int y) {
return this.x + x + y + this.y;
}
public int addMe(MyParent myPar) {
return addMe(myPar.x , myPar.y);
}
}

class MyChild extends MyParent {
int z;
MyChild (int x, int y, int z) {
super(x,y);
this.z = z;
}
public int addMe( int x, int y, int z){
return this.x + x + this.y + y + this.z + z ;
}
public int addMe(MyChild myChi) {
return addMe(myChi.x + myChi.y + myChi.z);
}
public int addMe(int x, int y) {
return this.x + x + this.y + y;
}
}

public class MySomeOne {
public static void main(String [] args) {
MyChild myChi = new MyChild(10,20,30);
MyParent myPar = new MyParent(10,20);

int x = myChi.addMe(10,20,30);
int y = myChi.addMe(myChi);
int z = myPar.addMe(myPar);
System.out.println(x + y + z);
}
}

That is the only compilation error, for now. But I'm trying to figure out how this mock exam question is giving a aswer of 300. How does the call of the addMe(myChi) arrive at 120?
 
donald rieck
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, here it is with the code tags:


 
Henry Wong
author
Marshal
Pie
Posts: 21190
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You are trying to call an addMe() method that takes a single int. This method does not exist.

Do you mean this?

 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java:25: cannot find symbol
symbol : method addMe(int)
location: class MyChild
return addMe(mychi.x + mychi.y + mychi.z);

you do not have a method signature addMe(int)
perhaps
return addMe(mychi.x + mychi.y + mychi.z);
should be
return addMe(mychi.x,mychi.y,mychi.z);
= addMe(int,int,int)

300 is correct
int x = myChi.addMe(10,20,30);// =(10+10)+(20+20)+(30+30) = 120
int y = myChi.addMe(myChi);// =(10+10)+(20+20)+(30+30) = 120
int z = myPar.addMe(myPar);// =(10+10)+(20+20) = 60
System.out.println(x + y + z);// =(120+120+60) = 300
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, I'd like to point out that you can edit your own messages. This is handy for adding CODE tags and such. To edit your message, just click on the pencil-and-paper icon at the top and far right of your message in any thread.

Now to answer your question. Let's look closely at the error message:

This tells you that the compiler is trying to find a method named "addMe" in MyChild. "But wait, MyChild has an addMe method," you say. Look closer. The compiler wants a version of addMe that takes a single int as a parameter. The name and parameter list form what is called the signature of a method. They all have to match exactly in order for a method to be called. (At the moment, I'm ignoring issues that arise in inheritence hierarchies.)

When I look at your addMe methods, I see the following signuatures:

Notice that I have removed some of the extraneous stuff from the actual code. This helps us concentrate the signatures only and ignore the unnecessary details (such as the access modifier and return type). None of the above methods take exactly one int as a parameter. This is why your compiler complains to you about it.

So this should explain what the problem is. Can you now see how to fix it? I'm sure there are more than one way to do this, so I'll leave it to you to figure something out. Of course, if you are still stuck, let us know and we'll help some more.

Keep Coding!

Layne
 
donald rieck
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
doh!

this should not have been:
public int addMe(MyChild myChi) {
return addMe(myChi.x + myChi.y + myChi.z);
}

But :

public int addMe(MyChild myChi) {
return addMe(myChi.x,myChi.y,myChi.z);
}

Hence, now the complier can find a method with the right signature.
 
donald rieck
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Back to my orginal problem: how does 300 become the answer?
 
donald rieck
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me see if I can get this right. A method can return any method that it can see(scope)? Because, in this case the addMe(MyChild myChi) method is returning a method with a signature of int addMe(int,int,int)?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a method can return only what it's signature says it can return
all yout addMe() methods return an int.

public int addMe(MyChild myChi) {
return addMe(myChi.x,myChi.y,myChi.z);
}

returns the int value it receives from addMe(myChi.x,myChi.y,myChi.z)

see my earlier post for the answer to the 300
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic