Question: meat.getXFoodLoc and meat.getYFoodLoc are declared the same way in ano
ID: 639304 • Letter: Q
Question
Question: meat.getXFoodLoc and meat.getYFoodLoc are declared the same way in another file but meat.getYFoodLoc gives me this error: method getYFoodLoc in class Food1 cannot be applied to given types;
System.out.println(meat.getYFoodLoc() + ")"); // <<<THIS LINE WONT COMPILE??
^
required: int
found: no arguments
reason: actual and formal argument
but the other function call (meat.getXFoodLoc) will run. How do I fix this?
import java.util.Random;
public class BearClassDriver3 {
public static void main(String[] args){
Random rand = new Random(System.currentTimeMillis()); // Seeds Random # with current time
int fx;
int fy;
int famount;
Bear_pt4 HungryBear = new Bear_pt4 (40,0,0,1);
while(HungryBear.energy>0)
{
fx= rand.nextInt(8);
fy= rand.nextInt(8);
famount= rand.nextInt(8);
Food1 meat = new Food1 ();
meat.setXFoodLoc(fx);
meat.setYFoodLoc(fy);
meat.setFoodAmount(famount);
System.out.print("Food Location("+ meat.getXFoodLoc() + ",");
System.out.println(meat.getYFoodLoc() + ")"); // <<<THIS LINE WONT COMPILE??
System.out.println("Energy is :" + HungryBear.energy);
int bearAction;
bearAction = rand.nextInt(3);
if(bearAction==0)
{
HungryBear.rotateRight();
}
else if(bearAction==1)
{
HungryBear.rotateLeft();
}
else if(bearAction==2)
{
HungryBear.step();
}
System.out.println("Bear is at: ("+ HungryBear.x+ ","+ HungryBear.y+ ")");
}
}
}
Explanation / Answer
Please provide the getYFoodLoc method body in the question.
As per the error the definition requires an int as parameter, may be, you copied the set method and forgot to remove the argument in the get method.
edit:
As I've explained the get method doesnt need a parameter.
change it to:
public int getYFoodLoc () { return y; }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.