0

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);
}
power5000
  • 25
  • 1
  • 9
  • You're comparing a constructor to movement code. I'm not clear on what part is over complicated. I will say your movement code isn't ideal because you could easily bind the specific movement to a specific key and avoid all the checking which is what the comments in "their" code seems to be implying they're planning on. – ChiefTwoPencils Apr 04 '15 at 22:23
  • Okay that makes since. So how would you improve the code though? that is how the lesson showed us to write hte movement code. – power5000 Apr 04 '15 at 22:27
  • Well then don't worry about that part. Can you clarify what you believe is more complicated about their's. That's a constructor, so if you're going to compare them it seems like you'd have to compare the same things, for example, constructors, methods, variables, etc. The main thing I see is they have references to the images and you don't. Their's would be better because you'll have to fetch the file every time you set the image; isn't that right? IOW, they `setImage(image)` and you `setImage(string)`. – ChiefTwoPencils Apr 04 '15 at 22:31
  • Well Their code uses a middle variable, they have the argument `keyName` and then create a new variable called `key` and then set `key = keyName` but why not just set `keyName = "key"` where "key" is the key you would use such as w. – power5000 Apr 04 '15 at 22:37

1 Answers1

0

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.

ChiefTwoPencils
  • 13,548
  • 8
  • 49
  • 75
  • Okay, I udnerstand what you mean I was trying to compare apples to oranges. at first glance it appeared the code was doing the same thing but they weren't. which is why I was confused thank you for the clarification. – power5000 Apr 04 '15 at 22:53
  • It was a small question and wasn't exactly related so I removed it. I will add it to the top portion... – power5000 Apr 05 '15 at 00:27
  • Okay thank you, it is very confusing becuase they only have a small part of the code displayed in a picture in a .PDF so you can not look at the entirety of it.... I am starting to dislike this class more and more... Thank you again for your assistance, you have been a huge help in my pursuit to learn Java! – power5000 Apr 05 '15 at 00:37
  • If you're second guessing your decisions against the books don't. Their snippets are likely from the end result and so they know more that you do now and you may not need it. Just know that when the time comes when you do you simply refactor (change to reflect the new requirements) the code to work properly. If I've managed to answer better now feel free to re-accept it :) @power5000 – ChiefTwoPencils Apr 05 '15 at 00:40
  • @power5000, I would say though that snippet is a hint staring you in the face. So, while you may not need it now or know why you would in general, they thought they needed it and they likely have a good reason. – ChiefTwoPencils Apr 05 '15 at 00:44