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 4 of 4

Thread: Class method followWallsRight- formatting and compile errors

  1. #1
    Junior Member
    Join Date
    Oct 2018
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Class method followWallsRight- formatting and compile errors

    I am new to Java. I have this code to process the following requests. I have a compile error and I am confused on how to correctly format the code. Please review the code and give me some help.

    An Athlete needs to be instantiated at (1, 1) facing north with an infinite number of beepers. A class method followWallsRight needs to be written that contains the code to escape the maze. ================================================== ============
    import edu.fcps.karel2.Display;
    import edu.fcps.karel2.Robot;
     
    public class Lab10
    {
       public static void followWallsRight(Athlete king);
       public static void main(String[] args)
       {
         Display.openWorld("maps/maze1.map");
          Display.setSize(10, 10);
          Display.setSpeed(10);
       }
     
      Athlete king = new Athlete(1, 1, Display.NORTH, Display.INFINITY);
      {
          while(king.frontIsClear())
             king.move();
          if(!king.rightIsClear())
             king.move();
       }
    }

    Error message:

    Lab10.java:6: error: missing method body, or declare abstract
    public static void followWallsRight(Athlete king);

  2. #2
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Class method followWallsRight- formatting and compile errors

    You have declared a method signature with no body and you can't do that (in this situation at least). So the compiler complains.

    Instead of doing this:
    public static void followWallsRight(Athlete king);

    You should do something like this:

    public static void followWallsRight(Athlete king) {
    // put the code here to do 
    // something.
    }
    And your while loops, for, loops and if statements should always have braces even for single statements. Even though you're allowed to do it without you will save yourself many headaches in the future if you stick to that rule.

    Regards,
    Jim

  3. The Following User Says Thank You to jim829 For This Useful Post:

    rmcoder (October 6th, 2018)

  4. #3
    Junior Member
    Join Date
    Oct 2018
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Class method followWallsRight- formatting and compile errors

    Hi Jim, thanks for the advice. I have taken your input to update the method body and was able to compile the code without error. However, when I run, the robot doesn’t even show up on the map. Can you review my code below to teach me what I still missed in the code? Thanks so much.

     
    import edu.fcps.karel2.Display;
    import edu.fcps.karel2.Robot;
     
    public class Lab10
    {
       public static void followWallsRight(Athlete king)
       {
          while(king.frontIsClear())
             king.move();
          if(!king.rightIsClear())
             king.move();
     
       }
       public static void main(String[] args)
       {
         Display.openWorld("maps/maze1.map");
          Display.setSize(10, 10);
          Display.setSpeed(10);
       }
     
      Athlete king = new Athlete(1, 1, Display.NORTH, Display.INFINITY);
      {
      followWallsRight(king);
      }
    }

  5. #4
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Class method followWallsRight- formatting and compile errors

    You need to put the Athlete king statement and the followWallsRight(king) statement inside of the main method. Also your last followWallsRight(king) statement does not need the braces. I was talking about the while loop and if statement. And as I also said, it is not required, just good programming practice.

    Other than that, I can't help much since you're using some 3rd party classes I don't have and am unfamiliar with.

    Regards,
    Jim

Similar Threads

  1. Help with formatting and creating a user defined class.
    By mike61096 in forum Object Oriented Programming
    Replies: 1
    Last Post: October 1st, 2014, 01:37 PM
  2. Compile Errors
    By acl2011 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 12th, 2012, 11:19 AM
  3. Couple compile errors for dice game
    By smithmar in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 8th, 2012, 12:16 PM
  4. Remaining compile errors: no suitable method found for & cannot find symbol
    By ChuckLep in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 12th, 2011, 02:33 PM
  5. Replies: 2
    Last Post: January 7th, 2011, 08:10 PM