Wednesday, October 26, 2011

Challenge 023

Constructors are awesome!!!

Constructors give you the option to alter the bug within certain parameters. You can click on the frid to construct or manipulate and actor (bug).

info.gridworld.actor.Bug(java.awt.Color) is a constructor. When clicking on this constructor inside the world, you are able to change the parameters of the bug. For example, you are able to change the color of the bug. You are also able to alter the bug by clicking it and getting a list of constructors. From here, you can move the bug to a specific coordinate location on the grid, you can alter the color, turn the bug, etc.

Since you are able to click on each individual box in the grid, it leads me to assume that there are as many constructors as there are squares in the grid?

13 comments:

  1. Constructors are indeed awesome, I'll definitely give you that.

    I'll throw you a resource here:

    tilde.aurora-schools.org

    On the Fledgling Developer show, I believe it's Episode 5, "World of the Grid"; you may find that episode particularly riveting.

    Research, then revise when able. Thank you.

    ReplyDelete
  2. I had watched the full video and I dont recall there being abything about constructors, which is fine with me, I learned other useful things. However, after reading online:

    Constructors are used to create an instance of a class. This is going to allow them to change the parameters of the bug, in this case the colors.

    In the bug class, there are two constructors one constructor is a no args constructor, allowing the bug class to be created. The no args constructor generates the parameters at random. The bug is defaulted to be red. The second constructor allows us to establish parameters that allow us to alter. For an example, we are allowed to change the color of the bug.

    ReplyDelete
  3. Hi Ryan, very good here, much closer. I have two items I'd like to consider:

    1. "The no args constructor generates the parameters at random." - How so?

    2.) Let's work up an example, and then analyze:

    Two statements, each constructing a Bug object:

    Bug bug1 = new Bug():
    Bug bug2 = new Bug(Color.GREEN);

    Which of the following statements (if any) will actually work after creating these Bugs:

    bug1.setColor(Color.BLUE);
    bug2.setColor(Color.YELLOW);

    ReplyDelete
  4. the no args constructor is set to the default because of the definition of a no args constructor.

    Bug2 would work and bug1 would not. This is because the bug1 is a no args meaning that it will default to red and not let you establish a blue or yellow bug.

    ReplyDelete
  5. Test your theory as stated in BlueJ and let me know what you find. Thank you.

    ReplyDelete
  6. When these bugs are created, neither of the statements show any bugs

    ReplyDelete
  7. You need to find a method for bug objects that gives you information on them, and then send that info to the System.out.println() method.

    ReplyDelete
  8. I answer the original question to the challenge above and i have no idea what you are asking me to do in the follow up questions

    ReplyDelete
  9. Fair question. In your investigation, with the following:

    bug1.setColor(Color.BLUE);
    bug2.setColor(Color.YELLOW);

    You commented, "Bug2 would work and bug1 would not. This is because the bug1 is a no args meaning that it will default to red and not let you establish a blue or yellow bug."

    I want you to discover that both of these statements run just fine, regardless of which constructor was used to create bug1 versus bug2.

    All bug objects have a setColor() method, so regardless of the constructor used, we can set the color of any Bug object.

    We can test this by using the following two statements, after creating the bugs and setting their color:

    System.out.println(bug1.toString());
    System.out.println(bug2.toString());

    These statements will show information about the bugs, including color.

    Clearer now?

    ReplyDelete
  10. still cant get any bugs to show up, im just going to move on

    ReplyDelete
  11. My advise would be to hang in, and check this out:

    You won't get bugs to show up in this context, since no grid and no world objects were created to contain them, so nothing will display in a GUI.

    The toString() methods give you a text-based way of "seeing" them, as it shows you information about their existence, their state of being.

    Run the main method from the following and indicate what information you see regarding the two Bug objects created:

    import info.gridworld.actor.Bug;
    import java.awt.Color;

    public class BugText {
    public static void main(String[] args) {
    Bug bug1 = new Bug();
    Bug bug2 = new Bug(Color.BLUE);

    System.out.println(bug1.toString());
    System.out.println(bug2.toString());
    }
    }

    ReplyDelete
  12. when I use the public class BugText, I am given the location which comes out to be null meaning no specific value was given. It gives the direction which is 0, and the RGB color code which comes out to give you a red bug and a blue bug.

    ReplyDelete
  13. Yes, the null is a result of the Bug not being in a Grid, so there is no location, just null.

    Most excellent, way to hang in there. As I see it, you were put through the ringer on this one, double XPs are in order:

    60 XPs for the completion of this challenge.

    ReplyDelete