Search code examples
variablesabstractiongreenfoot

Abstraction with variables


So I'm taking a highschool online Java class and well my teacher doesn't help... so we are learning about abstraction and I had already done this with my "alien" class that moves, he will face one way going forward and another going backward by switching two images... However when they showed the code in an example it seemed overcomplicated and I was wondering if I am just missing something.

My Code

private String avatarRight = "Alien.png";
private String avatarLeft = "Alien1.png";
/**
  * Act - do whatever the Alien wants to do. This method is called   whenever
  * the 'Act' or 'Run' button gets pressed in the environment.
  */
public void act() 
 {
     movement(avatarRight, avatarLeft);
     gatherPart();
 }

(Superclass containing movement method)

/**
* Sets up the movement keys and facing for the Object
*/
public void movement(String avatarRight,String avatarLeft)
{
     if (atWorldEdge() == false)
      {
        if (Greenfoot.isKeyDown("w"))
         {
            setLocation(getX(), getY()-2);
         }
        if (Greenfoot.isKeyDown("d"))
         {
            setImage(avatarRight);
            setLocation(getX()+2, getY());
         }
        if (Greenfoot.isKeyDown("s"))
         {
            setLocation(getX(), getY()+2);
         }
        if (Greenfoot.isKeyDown("a"))
         {
            setImage(avatarLeft);
            setLocation(getX()-2, getY());
         }
      }
     else
      {
      }
}

Their Code

{
    private GreenfootImage image1;
    private GreenfootImage image2;
    private boolean isKeyDown;
    private String key;
    private String sound;
    /**
    * Create a Duke and initialize his two images. Link Duke to a specific keyboard
    * key and sound.
    */
   public Duke(String keyName, String soundFile)
   {
        key = keyName;
        sound = soundFile
        image1 = new GreenfootImage("Duke.png")
        image3 = new GreenfootImage("duke2.png")
        setImage(image1);
    }

}

Where I just say avatarRight = "this image"

they say key = keyname

key = "key"

edit: So the way the set it up and I set mine up initially was

private int rotation;
public Capsule(int rot)
{
    rotation = rot
    setRotation(rotation);
}

but the one below works perfectly fine, as far as I can tell. Is there any reason why I would do the above code rather than the one below

public Capsule(int rot)
{
    setRotation(rot);
}

Solution

  • OK, based on the commentary I'm inclined to say you're not comparing the same things.

    Where I just say avatarRight = "this image" they say key = keyname key = "key"

    That doesn't seem to be exactly accurate. Where you say

    private String avatarRight = "Alien.png"; and private String avatarLeft = "Alien1.png";

    they have the png hard coded in the constructor as "Duke.png" and "duke2.png", which by the way contains an error because as far as I can see there's no image3.

    So the keyName doesn't seem to directly map as you say it does. Perhaps you should investigate the code further to see how they use the key or provide equal code for both examples so we can further see the differences.

    By looking at it perhaps there's a map somewhere and the key would be used to access the specific alien or other type of game object.


    To address your edit.

    Is there any reason why I would do the above code rather than the one below

    It's not possible to tell by that code if the reason has any value; it doesn't appear to by what you've shown. I can tell you that the reason I would do that is because I need that value elsewhere but not now. That could be for any number of reasons. You have to look at all the code available to you and see if they ever use that variable anywhere else without passing it in. Then you have found the reason or the lack there of.