Search code examples
javamethodscompilationcompiler-errorsjoptionpane

JOptionPane error-- Tortoise and Hair race


I am new to my Intro to Java course and am struggling with a program. I have posted about this program before but this is a different question. My program is supposed to model a race between a hare and a tortoise. I think I have everything I need but I am having trouble with my JOptionPane phrase. I think my phrase is Ok, but I am experiencing problems with the while portion of my statement. Here is the error message: cannot find symbol - Variable OK. I use Blue J to write and compile my program. Is there any reason why its not working? I thought the variable OK was the thing that the program user chooses to start the program. Am I mistaken? Can anyone help with this problem? Is there anything else you see in my program that needs fixing? Thanks

import java.util.Random;
import javax.swing.JOptionPane;

class Race
{
    int [] race = new int[70];
    int tortoise;
    int hare;
    Random randomGenerator = new Random();
    public boolean again = true;
    public void StartRace()
    {
        tortoise = 1;
        hare = 1;
        System.out.println("AND THEY'RE OFF!!!!");
        while (tortoise < race.length && hare < race.length)
        {
            moveHare();
            moveTortoise();
            DisplayCurrentLocation();
            String request;
        } 
        if 
            (tortoise > hare)
        {
            System.out.println("\n TORTOISE WINS!!");
        }
        else if
            (hare > tortoise)
        {   
            System.out.println("\n HARE WINS!!!");
        }
        else if
            (hare == tortoise)
        {
            System.out.println("TIE!!!");
        }
    }

    public void moveTortoise()
    {
        int n = randomGenerator.nextInt(10) + 1;
        //fast plod
        if ( n > 0 && n< 6)
            tortoise += 3;
        //slip
        else if (n > 10 && n< 11)
            tortoise -= 6;
        //slow plod
        else if (n > 6 && n< 9)
            ++tortoise;
            // protect from going past start
        if (tortoise < 1)
            tortoise = 1;
       // to make sure game ends
        else if (tortoise > 70)
            tortoise = 70;
    }// end tortoise

    public void moveHare()
    {
        int m = randomGenerator.nextInt(10) + 1;
        //big hop
        if (m > 0 && m<3)
            hare += 9;
        //big slip
        else if (m < 6)
            hare -= 12;
        // small hop
        else if (m > 3 && m< 5)
            ++hare;
        // )small slip
        else if (m < 9)
            hare -= 2;
        else if (m < 11)
            hare += 0;
        //ensure hare doesn't go past start
        if (hare < 1)
            hare = 1;
        // ensure hare doesnt go past end
        else if (hare > 70)
            hare = 70;
    } // end movehare

    public void DisplayCurrentLocation()
    {
        //this is the location of each on the array
        for (int count = 1; count <= 70; count++)
            // when at same location
            if (count ==tortoise && count ==hare)
            { 
                System.out.println("OUCH");
            }
            else if (count == hare)
            { 
                System.out.println("H");
            }
            else if (count == tortoise)
            {
                System.out.println("T");
            }
           else
               System.out.println();

    }

    public static void main ( String[] args)
    {
        Race Application = new Race();
        int startRaceRequest;
        while(startRaceRequest != JOptionPane.OK_OPTION)
     {
            JOptionPane.showConfirmDialog(null, "Select OK To Begin the Race!:");
     }
        do
     {
        Application.StartRace();

     } while(startRaceRequest != JOptionPane.OK_OPTION);
    }
   }

Solution

  • To your question with the JOptionPane: I suggest you to use

    JOptionPane.showConfirmDialog(null, "Message");
    

    instead of

    JOptionPane.showMessageDialog(null, "Message");
    

    because it allows you to do this:

        int startRaceRequest;
        while(startRaceRequest != JOptionPane.OK_OPTION)
            JOptionPane.showConfirmDialog(null, "Hit OK to start the race!");
    

    I don't really understand what you mean by "Ok" later on in your code, but if you refer to the confirmed dialog, try using JOptionPane.OK_OPTION wich represents an int.

    Also some help with your code: At the beginning you initialize the int[] race with the length of 70. Whenever you check if one of the racers is at or above 70, you do it like this

    if(tortoise < 70 && hare < 70){}
    

    wich is called hard-coding and you should try to avoid that as much as possible, to keep your code as dynamic as possible. If you do this

    if(tortoise < race.length && hare < race.length)
    

    you don't have to rewrite half your code just because you changed the length of the race.

    Another thing to the methods MoveTortoise and MoveHare. Conventionally the should be named moveTortoise and moveHare because method ins Java begin with lower case (that goes for all your methods!). Within those two methods inside your if conditions you write way too much code. for example this:

    if (m == 1 || m == 2)
            hare += 9;
        else if (m == 6)
            hare -= 12;
        else if (m == 3 && m == 5) //there is something wrong here, m cant be 5 and 3 ;)
            ++hare;
        else if (m == 7 || m == 8)
            hare -= 2;
        else if (m == 9 || m == 10)
            hare += 0;
    

    could be cut to this:

    if(m > 0 && m < 3){ // if a number is > 0 and < 3 -> number is 1 or 2
    
        } else if(m < 6){ // if a number is > 0 and > 3 -> if number is < 6 -> number could be 3, 4 or 5
    
        } else if(m == 6){ 
    
        } else if(m < 9){ // -> 7 or 8
    
        } else if(m < 11){ // 9 or 10
    
        }
    

    In Addition, you use a random number generator and I think you are aiming for a number between 1 to 10, however this

    int m = randomGenerator.nextInt(11);
    

    will return a number from 0 to 10. Instead, try this:

    int m = randomGenerator.nextInt(10) + 1;
    

    wich will return a number from 0 to 9, adding one will result in a number between 1 and 10.

    Hope this helps. Please remember to give feedback and mark a solution if your question was answered.