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

Thread: Help me with my code for a project? Not sure how to fix the errors.

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Help me with my code for a project? Not sure how to fix the errors.

    Hi guys, so I have a project due next week, and I can't figure out what I'm doing wrong/how to make it run.

    The assignment:

    For this assignment, your job is to create a version of a carnival game in which mechanical dogs race along a track. The racing game is called DogTrack. DogTrack is also the name of the java source file you must submit to complete the assignment. The DogTrack class should be written so that the driver class below, DogDriver, works as intended.

    A dog track has N discrete positions, 0 through N-1. The value of N is determined interactively in the application driver (below) once your program begins running.

    There are three dogs, named Fido, Spot, and Rover. Each starts at position 0. A spinner selects (returns) a random value from among the values 1-2-3-4-5; these values are equally likely. Each dog advances by a separate spin of the spinner. In general a dog advances 1 space when the spinner spins a 1, 2 spaces when the spinner spins a 2, and so forth. The game is over when (at least) one of the dog crosses the finish line, that is, when at least one dog reaches a board value that is greater than or equal to (N-1).

    A special condition: if a dog lands exactly at position number N/3 or position number 2N/3, where N is the size of the track, then that dog returns to its position before the move was made, and then moves back one more square, if possible. Because there are no negative positions, a dog at 0 that advances to N/3 on the first move does NOT go to (negative) position -1; after the move it should simply remain at position 0. See sample run below for an example.

    At each round, the track should be drawn, using the symbol o, and the letters F, S, and R (for Fido, Spot, and Rover).

    You should draw the state of the race as three "lines", one for each dog, using an 'o' to represent unoccupied positions, and F,R, and S to represent dog positions. These 3 line groups should be separated by a row of dashes, as shown in the sample run below. If the track size is 10, then this drawing

    ooooooSooo

    means that the dog Spot is at location 6 on the track, with earlier positions 0-5 unoccupied (dog tracks always begin with position 0).


    Here is the driver class for the racing game:

    import java.util.Scanner;   
    public class DogDriver{       
      public static void main(String[] args){      
        System.out.println("Enter an int > 3: size of the track");     
        Scanner s = new Scanner(System.in);      
        int trackSize = s.nextInt();      
        System.out.println("track Size: " + trackSize);      
        DogTrack d = new DogTrack(trackSize);      
        d.playGame();    
      } 
    }

    Here is a sample run:

    > run DogDriver
    Enter an int > 3: size of the track
    8
     
    track Size: 8   // so positions are 0,1,...,7; position N/3 = 8/3 = 2 is a go-back square, as is position (2*8)/3 = 5.
     
    Fooooooo // all dogs start a position 0
    Sooooooo
    Rooooooo
    --------
    ooooFooo // at position 4
    oooSoooo // at position 3
    Rooooooo // Rover spins 2, but 8/3 = 2, so Rover goes back to 0, but NOT to position -1
    --------
    oooFoooo // Fido, at position 4, spins a 1, to location 5 = (2*8)/3, so: back to original position 4, then back 1, to pos 3
    ooooSooo
    ooooRooo
    --------
    ooooFooo
    oooooooS // S & R have reached position 7 = tracksize - 1, terminating the game
    oooooooR
    --------
    Spot Wins!
    Rover Wins!


    A requirement: your DogTrack class must include and MAKE ESSENTIAL USE OF the following methods, in addition to the DogTrack constructor and principal method playGame(). These are:
    * spin - generates an integer value from 1 to 5 at random
    * move - advances the pieces (alternative: instead of a move method write 3 separate methods, moveRover, moveSpot, moveFido)
    * isOver - checks if race termination condition has been met
    * showTrack - draws the three tracks using 'o' and a dog letter to indicate dog position. Should insert a row of dashes after the three tracks are displayed.
    * showWinners - reports the winning dog or dogs when the race ends (see example above.)



    Suggestions on how to proceed (only suggestions though):

    * idea #1: get your code running but ignore the N/3 + 2N/3 stuff. You can add that in later. Also, don't worry at first about drawing the board correctly. Just print out the values of the dog positions.

    * idea#2: Before you write any of the required methods explicitly, write the playGame method using the named methods above as if they already existed. Be sure to think about and account for any parameters each method might have.

    Paste your DogTrack class code in the box below:

    NOTE: you may NOT include any import statements in the class you submit. Use the Math class random method to generate your random numbers. Do NOT import the java.util library. Do NOT implement your solution using arrays.


    Here is my code:
    public class DogTrack{
     
      int s = 0;
      int r = 0;
      int f = 0;
     
     
        public void playGame(){
        while (isOver()==false){
          d.spin();
          d.moveRover();
          d.spin();
          d.moveSpot();
          d.spin();
          d.moveFido();
          d.showTrack();
        }
     
        if (isOver()==true){
          d.showTrack();
          d.showWinners();
        }
      }
     
       public DogTrack(int size){
       trackSize = size;
        }
     
      public void moveRover(){
        r = r + n;
      }
     
      public void moveSpot(){
        s = s + n;
      }
     
      public void moveFido(){
        f = f + n;
      }
     
      public void spin (int n){
        n = ((int)5*Math.random()+1);
      }
     
      public boolean isOver(){
        if
          (r >= (TrackSize - 1) || s >= (trackSize - 1) || f >= (trackSize -1)) 
          return true;
        else
          return false;
      }
     
      public String showTrack(){
        String rover = "";
        for (int x = 0; x < trackSize; x++){
          if
            (r >= x)
            rover = rover + "R";
          else 
            rover = rover + "o";
        }
     
     
        String spot = "";
        for (int x = 0; x < trackSize; x++){
          if
            (s >= x)
            spot = spot + "S";
          else
            spot = spot + "o";
        }
     
          String fido = "";
        for (int x = 0; x < trackSize; x++){
          if
            (f >= x)
            fido = fido + "F";
          else
            fido = fido + "o";
        }
     
        return rover;
        return spot;
        return fido;
      }
     
      public String showWinners(){
        if (r>=(trackSize-1)) return "Rover Wins!"; 
        if (s>=(trackSize-1)) return "Spot Wins!"; 
        if (f>=(trackSize-1)) return "Fido Wins!"; 
      } 
     
    }

    Thanks in advance!


  2. #2
    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: Help me with my code for a project? Not sure how to fix the errors.

    Welcome to the Forum! Thanks for taking the time to learn to post code correctly, and if you haven't already, please read this topic to see other useful info for newcomers.

    What's your question? What do you need help with? Ask specific questions, describe specifically what you don't understand how to do or where you are having problems. Post any errors you want help fixing.

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help me with my code for a project? Not sure how to fix the errors.

    Thanks for replying GregBrannon.

    My code is generating quite a few "cannot find symbol" errors, specifically the variable d in lines 10 - 16 and the variable trackSize wherever it appears.

  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: Help me with my code for a project? Not sure how to fix the errors.

    Remember that variables must be recognized by the compiler as having been previously declared, either in the existing scope or in an enveloping one. "cannot find symbol" means the compiler doesn't know what that variable (method, etc.) name refers to, because it can't find an in-scope declaration for it, like:

    int d = 0; // includes initialization

    I'm not sure what 'd' is either, though I'm pretty sure it's not an int. Declare 'd' so that the compiler knows what it is.

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    tyrion (June 12th, 2014)

  6. #5
    Junior Member
    Join Date
    Jun 2014
    Posts
    22
    My Mood
    Starving
    Thanks
    1
    Thanked 8 Times in 6 Posts

    Default Re: Help me with my code for a project? Not sure how to fix the errors.

    A few things I noticed to get rid of some of the errors:

    In your class DogTrack, you have a method called "playGame()". In that method you call other methods inside the DogTrack class.

    If you are calling a method is inside of DogTrack and the method belongs to DogTrack (ie. spin()), you can remove the "d." when calling method. "d.spin()" would be used if you are in another class other than DogTrack, and want to reference a DogTrack object. You might be able to think of it like a DogTrack is trying to say, "go tell myself to spin" , and you cant go do that because you are yourself, you will not find yourself anywhere else. So you remove the "d." so if it is simply spin() you are now telling yourself to spin, and you can spin because you are DogTrack, and you have a method called spin().

    That may sound confusing, but if it does, you may want to read up on what classes and objects are.

    Example:
    d.spin()

    should be
    spin()

    actually should be
    spin(5)
    or another number other than 5 (an int), because...
      public void spin (int n){     //LOOK:  the (int n) means this is going to require an int, if you meant to return (or produce) an int instead, it should have been declared: public int spin().
        n = ((int)5*Math.random()+1);
      }


    Hopefully you can take something from that!
    Last edited by koder632417; June 11th, 2014 at 09:16 AM.

  7. The Following User Says Thank You to koder632417 For This Useful Post:

    tyrion (June 12th, 2014)

  8. #6
    Junior Member
    Join Date
    Jun 2014
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help me with my code for a project? Not sure how to fix the errors.

    That does help get rid of a bunch of errors, thanks! But now I have a question about the variable trackSize, and the object declaration in the Driver class. I'm getting the "cannot find symbol" error for this statement, but I don't see anything wrong with it.

    Do I have to declare it again in the dogTrack class? If so, how should I go about doing that?

    Thanks!

  9. #7
    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: Help me with my code for a project? Not sure how to fix the errors.

    Post your updated code AND the error message in its entirety.

    Java is case sensitive, and I see trackSize with variable capitalizations. Be consistent, OR if they are in fact meant to be different variables, then give them more distinct names.

    Where is trackSize declared? If a variable name is used in the DogTrack class, then the variable must be declared within DogTrack's scope.

  10. #8
    Junior Member
    Join Date
    Jun 2014
    Posts
    22
    My Mood
    Starving
    Thanks
    1
    Thanked 8 Times in 6 Posts

    Default Re: Help me with my code for a project? Not sure how to fix the errors.

    Your dogTrack class does not know of the main classes trackSize variable, meaning it is out of scope as Greg had said.
    So, yes you have to declare the variable inside the dogTrack class.

    The trackSize variable you refer to here is completely different from the main classes trackSize:
       public DogTrack(int size){
       trackSize = size;
        }
    And it looks like DotTrack doesn't declare its own trackSize..

    If so, how should I go about doing that?
    You have done it already for s,r, and f.
      int s = 0;
      int r = 0;
      int f = 0;
    So you should be able to declare trackSize just the same!
    Last edited by koder632417; June 12th, 2014 at 09:39 AM.

  11. #9
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Help me with my code for a project? Not sure how to fix the errors.

    return rover;
    return spot;
    return fido;
    What do you think is going to happen when this executes? I can tell you one thing - probably not
    what you expect. As soon as control reaches here - the value returned is the first one it reaches
    (in this case "rover". The other two return statements will never execute.

    It might be a better idea to give the other two a specified condition on when they return; e.g:

    if (condition == true)
          return spot:

    Also, in your constructor if I was reading it without the assignment description I would not know what
    "f, r, and s" were. Try to give your instances better names e.g "instFido". Just a suggestion for readability

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  12. #10
    Junior Member
    Join Date
    Jun 2014
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help me with my code for a project? Not sure how to fix the errors.

    I should also mention that I can't edit anything in the DogDriver class, as that was given to me and not coded by me. The only thing I can edit is the DogTrack class.

    Here is my updated code:

    public class DogTrack{
     
      int s = 0;
      int r = 0;
      int f = 0;
     
        public void playGame(){
        while (isOver()==false){
          spin();
          moveRover();
          spin();
          moveSpot();
          spin();
          moveFido();
          showTrack();
        }
     
        if (isOver()==true){
          showTrack();
          showWinners();
        }
      }
     
       public int spin(){
        return ((1+ (int)Math.random()*5));
      }  
     
      public int moveRover(){
        r = r + spin();
      }
     
      public int moveSpot(){
        s = s + spin();
      }
     
      public int moveFido(){
        f = f + spin();
      }
     
      public boolean isOver(){
        if
          (r >= (d - 1) || s >= (d - 1) || f >= (d -1)) 
          return true;
        else
          return false;
      }
     
      public String showTrack(){
        String rover = "";
        for (int x = 0; x < d; x++){
          if
            (r >= x)
            rover = rover + "R";
          else 
            rover = rover + "o";
        }
     
     
        String spot = "";
        for (int x = 0; x < d; x++){
          if
            (s >= x)
            spot = spot + "S";
          else
            spot = spot + "o";
        }
     
          String fido = "";
        for (int x = 0; x < d; x++){
          if
            (f >= x)
            fido = fido + "F";
          else
            fido = fido + "o";
        }
     
        System.out.println(rover);
        System.out.println(spot);
        System.out.println(fido);
      }
     
      public String showWinners(){
        if (r>=(d-1)) return "Rover Wins!"; 
        if (s>=(d-1)) return "Spot Wins!"; 
        if (f>=(d-1)) return "Fido Wins!"; 
      } 
    }

    Thanks for all the help and sorry if I'm being stupid/not seeing something obvious that I can do to fix this code. I'm a complete beginner.

  13. #11
    Junior Member
    Join Date
    Jun 2014
    Posts
    22
    My Mood
    Starving
    Thanks
    1
    Thanked 8 Times in 6 Posts

    Default Re: Help me with my code for a project? Not sure how to fix the errors.

    Thanks for all the help and sorry if I'm being stupid/not seeing something obvious that I can do to fix this code. I'm a complete beginner.
    We are all still learning. No worries!

    So is it SOLVED, or were you still having issues?

  14. #12
    Junior Member
    Join Date
    Jun 2014
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help me with my code for a project? Not sure how to fix the errors.

    Oh sorry, I forgot to say that yeah, I'm still having issues, but they're all "cannot find variable" errors. I'm wondering if there is a way to connect the Driver class to the DogTrack class?

  15. #13
    Junior Member
    Join Date
    Jun 2014
    Posts
    22
    My Mood
    Starving
    Thanks
    1
    Thanked 8 Times in 6 Posts

    Default Re: Help me with my code for a project? Not sure how to fix the errors.

    They are already connected!

    Look in the DogDriver class and you will see it creates a DogTrack and calls on its constructor and method playgame():

        DogTrack d = new DogTrack(trackSize);      
        d.playGame();

    but that means that there are a few methods that are never used in DogTrack. Are you sure you can't edit that?



    If you are getting a cannot find variable error, it is probably because you have not DECLARED it yet. Where are you getting that error?

Similar Threads

  1. Errors I don't know how to fix java beginner I need help ASAP
    By coding2 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 12th, 2014, 04:40 PM
  2. Replies: 3
    Last Post: April 27th, 2013, 07:19 AM
  3. Getting Exception Errors, But Not Sure How To Fix Code
    By noel222 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 3rd, 2012, 12:09 PM
  4. Can't fix my own errors
    By mrroberts2u in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 14th, 2011, 09:20 AM
  5. Replies: 3
    Last Post: February 23rd, 2011, 01:27 AM