This site requires JavaScript, please enable it in your browser!
Greenfoot back
parthjuthani
parthjuthani wrote ...

2018/9/29

fatcat scenario - issue of being unable to inherit the value of a public variable in subclass MyCat, inside the walkLeft and walkRight method call parenthesis

parthjuthani parthjuthani

2018/9/29

#
in the following code i am not able to inherit into the subclass MyCat, the value of the variable 'distance' from the superclass Cat Cat superclass: /** * Walk a bit to the left. 'distance' determines how far to walk. Use small numbers (1 to 10). */ public int walkLeft(int distance) { walk(distance, -10, "cat-walk.png", "cat-walk-2.png"); return distance; } /** * Walk a bit to the right. 'distance' determines how far to walk. Use small numbers (1 to 10). */ public int walkRight(int distance) { walk(distance, 10, "cat-walk-right.png", "cat-walk-right-2.png"); return distance; } /** * Internal walk method. Walk a given distance into a given direction, using given images. */ public void walk(int distance, int direction, String img1, String img2) { for (int i=0; i<distance; i++) { setImage(img1); wait(4); setLocation(getX() + direction, getY()); setImage(img2); wait(4); setLocation(getX() + direction, getY()); } setImage("cat.png"); } MyCat subclass: public class MyCat extends Cat { public boolean tired = false; public boolean hungry = false; public boolean bored = true; private boolean isAlone = true; public int numberOfCats = 0; /** * Act - do whatever the MyCat wants to do. */ public void act() { walkLeft(int distance); walkRight(int distance); // Repeared activity walkRight(int distance); // alone(); if(isAlone != true) { shoutHooray(); dance(); eat(); //Repeated Activity shoutHooray(); // walkLeft(int distance); walkRight(int distance); tired = true; getObjectsInRange(10, MyCat.class); removeTouching(MyCat.class); isAlone = true; } }
parthjuthani parthjuthani

2018/9/29

#
ignore the misspelling of Repeated as Repeared
danpost danpost

2018/9/29

#
'inherit' is not the proper word for this. You pass values to methods via the method's parameters. For example to pass a distance of 10 to the walkLeft method, you would use:
walkLeft(10);
The method (
public int walk(int distance)
) will assign the argument (10) to the parameter variable (distance) for that execution of the method.
parthjuthani parthjuthani

2018/9/30

#
So, in the super class MyCat, there should be 10 instead of 'int distance'?
danpost danpost

2018/10/1

#
parthjuthani wrote...
So, in the super class MyCat, there should be 10 instead of 'int distance'?
There should be something that has a specific value. It could be a literal constant (like '10' is) or it could be a variable (with a pre-assigned value) or it could be a method call that returns a value of the appropriate type (int, in your case) or it could be an expression that evaluates to a value of that type. At any rate, you supply the distance that you want it to move.
You need to login to post a reply.