Results 1 to 7 of 7
  1. #1
    v1ru5 is offline Member
    Join Date
    Sep 2012
    Posts
    20
    Rep Power
    0

    Default Help needed for Diving Program

    Hey guys,

    I need some help on this assignment Question 4/5:

    http://www.site.uottawa.ca/~mseha092...2_Fall2012.pdf

    So basically this is what I have done:

    Java Code:
    // This is a template to be used for all single-class ITI 1120 programs.
    
    /**
     * Replace this comment with a description of your program.
     */
    
    import java.util.Scanner;
    
    class A2Q5 // Replace 'Template' with your own algorithm name.
    {
      public static void main (String args[ ])
      {
        // DECLARE VARIABLES/DATA DICTIONARY
        Scanner keyboard = new Scanner (System.in);
        int tMax = 0;
        String diveAgain = "y";
        
        while (true)
        {
          if (!diveAgain.equals("y"))
          {
            break;
          }
        // READ IN GIVENS use our ITI1120 special class for keyboard input    
        System.out.print ("Please enter the maximum time in seconds to make a dive: ");
        tMax = keyboard.nextInt();
        dive (tMax);
        System.out.println ();
        System.out.println ();
        System.out.print ("Do you wish to make another dive attempt?(y/n): ");
        diveAgain = keyboard.nextLine();
        
        // BODY OF ALGORITHM - Call to the method to solve problem
        // PRINT OUT RESULTS AND MODIFIEDS
        }
      }
      
      // Problem Solving Method
      // Description: Short description of what the
      // method does, 
      // Parameters (GIVENS): 
      //    A description of variables in the parameter list
      public static void dive(int tMax)
      {  
        // DECLARE VARIABLES/DATA DICTIONARY
        // INTERMEDIATES8
        
        double probability = 0;
        double distance = 0;
        
        double randomNum = 0;
        // RESULT
        char[] moves = new char[tMax];
        
        // BODY OF ALGORITHM
        for (int i = 0; i<(moves.length-1); i++)
        {
        randomNum = 0 + Math.random();
        System.out.println (randomNum);
        probability = 1 - (distance/10);
        System.out.println (probability);
        if (randomNum < probability)
        {
          moves[i] = ('F');
          distance+=2;
        }
        else
        {
          moves[i] = ('B');
          distance--;
        }
        }
        if (distance >= 10)
        {
          moves[moves.length-1] = ('D');
          System.out.print ("The man jumped with the moves ");
          for (int i = 0; i <= moves.length-1; i++)
          {
            System.out.print (moves[i]);
          }
          System.out.print (" in " + (moves.length-1) + " seconds");
        }
        else
        {
          System.out.print ("The man lost his nerve after the moves ");
          for (int i = 0; i <= moves.length-1; i++)
          {
            System.out.print (moves[i]);
          }
        }
             
      }
      
    } // Don't remove this brace bracket!
    For some reason, the person never jumps. Basically since each step forward is two steps and each step backward is one step, after the difference between the two the guy needs to have 10 steps forward in order to jump off but that never happens. If anyone knows what I am doing wrong, any help would be appreciated.

    Thanks!

  2. #2
    arieltal is offline Member
    Join Date
    Sep 2012
    Posts
    34
    Rep Power
    0

    Default Re: Help needed for Diving Program

    when i ran your code, the man did jump on some occasions, i guess it's just the random numbers,

  3. #3
    v1ru5 is offline Member
    Join Date
    Sep 2012
    Posts
    20
    Rep Power
    0

    Default Re: Help needed for Diving Program

    Quote Originally Posted by arieltal View Post
    when i ran your code, the man did jump on some occasions, i guess it's just the random numbers,
    Still not getting the man to jump. I think I know what the problem is though. I need to make the for loop exit when distance equals 10 because even when distance equals 10 it still continues and so the man moves backwards. So my question was, how can I make a for loop exit:

    Java Code:
    if (randomNum < probability)
        {
          moves[i] = ('F');
          distance+=2;
          if (distance == 10)
          {
             What would I put here to exit for loop?
          }  
        }
        else
        {
          moves[i] = ('B');
          distance--;
        }

  4. #4
    v1ru5 is offline Member
    Join Date
    Sep 2012
    Posts
    20
    Rep Power
    0

    Default Re: Help needed for Diving Program

    Nvm, I added a break and it works. But now I am getting an output that looks like this:

    The man jumped with the moves FFFFBFBF(16 blank spaces)D in 24 seconds.

    How am I supposed to get rid of the blank spaces in the array since the arrays size is the size of the maximum time.

  5. #5
    arieltal is offline Member
    Join Date
    Sep 2012
    Posts
    34
    Rep Power
    0

    Default Re: Help needed for Diving Program

    you can add 'D' at the moment he jumps and ignore the rest of the array

  6. #6
    v1ru5 is offline Member
    Join Date
    Sep 2012
    Posts
    20
    Rep Power
    0

    Default Re: Help needed for Diving Program

    okay, but how do I ignore the rest of the array?

  7. #7
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    393
    Rep Power
    4

    Default Re: Help needed for Diving Program

    You can use "break" to stop a for-loop (or a while-loop) from running.

Similar Threads

  1. diving distance program
    By maximumammo in forum New To Java
    Replies: 4
    Last Post: 04-05-2012, 09:09 PM
  2. HELP NEEDED with J2ME program
    By just_in_deed in forum CLDC and MIDP
    Replies: 11
    Last Post: 03-06-2012, 09:53 PM
  3. Diving into Java Web Services from .NET world
    By mhalabi in forum New To Java
    Replies: 0
    Last Post: 02-12-2012, 08:59 PM
  4. need some advice before diving into struts 2
    By kulangotski in forum Web Frameworks
    Replies: 8
    Last Post: 04-26-2011, 05:52 AM
  5. Calender Program Help Needed!!
    By DavidEvans in forum New To Java
    Replies: 1
    Last Post: 03-17-2010, 07:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •