Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: exercise 229

  1. #1
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile exercise 229

    Hi, so this exercise has two separate sections of editable code. The assignment is to fix the two errors. Do I need to create a getter method to get rid of the error? Here are the full instructions:

    The following code segment overrides the processActors method to implement BlushingActor, but it contains two errors, one of which causes a compile-time error and the other of which will sometimes cause a runtime exception. Study the code and correct the errors. Test that your corrected definition gives rise to Actors that behave in the manner described above.

    Here is my attempt at fixing the first error in section 1:

    public class BlushingActor extends Critter
    {
      public void processActors( ArrayList<Actor> actors )
      {
        Color c = getColor();
        int red = c.getRed();
        int green = c.getGreen();
        int blue = c.getBlue();
        if ( actors.size() > 0 )
          red = 255;
          green = 235;
          blue = 110;
        else
          red = Math.min( 0, red - 10);
        setColor( red, c.getGreen(), c.getBlue() );
      }
    }

    And here is section 2:

    Actor alice = new Bug(),
          blusher = new BlushingActor();
     
    blusher.setColor( Color.BLACK );
     
    (new Rock()).putSelfInGrid( grid, new Location( 3, 5 ) );
    (new Rock()).putSelfInGrid( grid, new Location( 0, 10 ) );
    (new Rock()).putSelfInGrid( grid, new Location( 7, 16 ) );
    (new Rock()).putSelfInGrid( grid, new Location( 15, 19 ) );
    (new Rock()).putSelfInGrid( grid, new Location( 19, 2 ) );
     
    alice.putSelfInGrid( grid, new Location( 5, 3 ) );
    blusher.putSelfInGrid( grid, new Location( 7, 7 ) );

    I get this error when I try to run it:

    "error 'else' without if"

    That's different from the original error, but am I just messing this code up further?

    Oh, and for section 1, I think I can give you the original code:

    public class BlushingActor extends Critter
    {
      public void processActors( ArrayList<Actor> actors )
      {
        Color c = getColor();
        int red = c.getRed();
        if ( actors.size() > 0 )
          red = 255;
        else
          red = Math.min( 0, red - 10);
        setColor( red, c.getGreen(), c.getBlue() );
      }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: exercise 229

    Always use curly brackets {} with if statements and for loops. An if statement without the curly brackets only does the very next statement that follows the if statement.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: exercise 229

    It's still not working. Here's what I've edited it to:

    public class BlushingActor extends Critter
    {
      public void processActors( ArrayList<Actor> actors )
      {
        Color c = getColor();
        int red = c.getRed();
        int blue = c.getBlue();
        int green = c.getGreen();
        if ( actors.size() > 0 ){
          red = 255;
          blue = 235;
          green = 235;
        }
        else
          red = Math.min( 0, red - 10);
        setColor( red, c.getGreen(), c.getBlue() );
      }
    }&#65279;


    --- Update ---

    I think the brace should be AFTER the else statement, but I've had to take a short time off. Any quick review tutorials for me to read that might help?

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: exercise 229

    KW's answer still applies, but in a more general sense: Enclose ALL clauses inside curly brackets, "{}".

  5. #5
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: exercise 229

    Obviously, that includes else statements. Alright, so here's what I have now:

    public class BlushingActor extends Critter
    {
      public void processActors( ArrayList<Actor> actors )
      {
        Color c = getColor();
        int red = c.getRed();
        int blue = c.getBlue();
        int green = c.getGreen();
        if ( actors.size() > 0 ){
          red = 255;
          blue = 235;
          green = 235;
        else
          red = Math.min( 0, red);
          blue = Math.min(0, blue);
          green = Math.min(0, green);
        }
        setColor( c.getRed(red), c.getGreen(gren), c.getBlue(blue) );
        }
    }

    That's at least for the top box, which is what really matters I think.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: exercise 229

    That construct is certainly giving you errors. if and else are separate clauses. A proper else statement is not included inside the if clause.

  7. #7
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: exercise 229

    Alright, so I'll take the else statement out. Here's what I have now:

    public class BlushingActor extends Critter
    {
      public void processActors( ArrayList<Actor> actors )
      {
        Color c = getColor();
        int red = c.getRed();
        int blue = c.getBlue();
        int green = c.getGreen();
        if ( actors.size() > 0 ){
          red = 255;
          blue = 235;
          green = 235;
        }
        else
          red = Math.min( 0, red);
          blue = Math.min(0, blue);
          green = Math.min(0, green);
        setColor( c.getRed(red), c.getGreen(green), c.getBlue(blue) );
        }
    }

    But here's the thing. I still get this error:

    error: method getRed in class cannot be applied to the given types
     
    setColor (c.getRed(red), c.getGreen(green), c.getBlue(blue));


    --- Update ---

    And I get the same thing if I take the parameters out of the part described in the error message, from the getters

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: exercise 229

    Why are you just guessing what to do? You're better than that. The construct is:
    if ( condition )
    {
        // the body of the if clause goes here
    }
    else
    {
        // the body of the else clause goes here
    }
    The error is telling you that you're not calling the getRed() method correctly. The parameter you're supplying are not those expected by the method. Review the method's signature, the API if necessary, and adjust your code to comply with the method's requirements.

    This is not new stuff to you.

Similar Threads

  1. accessor and modifier method exercise; exercise 100
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 30th, 2013, 10:18 PM
  2. Exercise 95 (I KNOW, I'm at an earlier exercise)
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 24th, 2013, 08:42 PM