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

Thread: Beginners Java Help !

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Beginners Java Help !

    Im having some trouble with my code:


    It is supposed to display a sequence of letters based on whether a man jumps off a cliff or not. The equation used for the jump is listed as fStep . Heres the code:

    import java.util.* ;
     
    class manDiving { 
     
     public static void main(String args[]) 
     {
     
    Scanner input = new Scanner (System.in); 
     
    int tMax = 0 ; //Total time for the jump  
     
    System.out.println("Man Jumping off a cliff !");
    System.out.println("__________________________") ;
    System.out.println("                           ");
    System.out.println("Please enter the maximum time in seconds"); 
    tMax = input.nextInt(); 
    calcDive(tMax);  
     }
     
    public static void  calcDive(int tMax) {
     
     char[] moves = new char[tMax] ; 
     
     double cliffDistance = 0; //The man starts at 0.This is the distance he covered.
     double fStep ; // The probability of making the forward step.
     double random ; //Random Number generated to determine the sequence.
     int mLength = moves.length ; 
     
     
     for(int counter = 0 ; counter < mLength ; counter++ ){
     
      random = 0.0 + (int)(Math.random()*((1-0)+1));; 
      fStep = 1 - (cliffDistance/10) ;  
     
      if ( random < fStep ){ 
      moves[counter] = 'F';
      cliffDistance+=2 ; // Adds two feet to the distance.  
     
      }
      else if ( random > fStep)
      { 
         moves[counter] = 'B'; 
         cliffDistance-- ; 
     
      } 
     
     
     }
     
         if ( cliffDistance == 10){ 
         moves[moves.length-1] = 'D' ; 
         System.out.print("The Man jumped after this succession of moves:");
         for (int counter = 0 ;counter <mLength - 1 ; counter ++ ) {
           System.out.print (moves[counter]) ; 
     
         }
        }
         else{ 
           System.out.print("The man lost his nerve after this succession of moves:"); 
           for(int counter = 0 ; counter<mLength ; counter++) { 
             System.out.print(moves[counter]) ; 
     
           } 
     
         }
    }
    }


    Im having multiple errors here and I want to be able to display the sequence here properly . The letter D , which indicates whether he dives or not doesnt display. What displays is a bunch of blanks.

    Could anyone help please!

    Much appreciated !


    Heres the question itself too , for good measure :

    Assume a man is preparing to dive, as shown in the following picture. The man needs to
    move 10 feet forwards to go over the edge.
    The man takes one step every second, and each step moves him two feet. He may,
    however, not always move forward. Because he is nervous, he may step backwards,
    particularly as he gets near the edge. The probability that the man will take a step forward
    is given by :
    P(forward) = 1- (x/10)
    where x is the man’s distance (in feet) from his starting point
    Note that, when x is 0 (man at his starting point), the man is certain to move forwards, but
    that, when x is 8 (the man is on the brink of going over the edge), the probability of him
    moving forwards is just 0.2. Note also that, if the man does not go forwards, he goes
    backwards. If the man does not go over the edge within tMax secs, he doesnt go at al .


  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: Beginners Java Help !

    What exactly are the multiple errors you're seeing?
    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
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginners Java Help !

    There are spaces in between the letters F and B , where there shouldnt be and The final letter D( where the man is supposed to dive ) doesnt show up .
    Heres an example of what I mean by the spaces :

    Please enter the maximum time in seconds :
    [DrJava Input Box]
    The man lost his nerve after this succession of moves:FBB FBBFBB

    There shouldn't be any spaces there and when the man does jump , there should be a 'D' as the final letter in the sequence.

  4. #4
    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: Beginners Java Help !

    Are you expecting the space before the semicolon to translate to a space being printed out? It doesn't work that way. If you want a space printed out, you have to print it out yourself. Java code itself doesn't really care about whitespace like that.
    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!

  5. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginners Java Help !

    No no , im not expecting any spaces at all. An example of the code running smoothly is :

    Please enter the maximum time in seconds to make a dive: 15

    The man jumped with the moves FBFFFBFBFFD in 11 seconds
    Do you wish to make another dive attempt?(y/n): y

    Please enter the maximum time in seconds to make a dive: 10
    The man lost his nerve after the moves FFBFBFFBFB

    Do you wish to make another dive attempt?(y/n): n
    Program terminated!

Similar Threads

  1. Replies: 1
    Last Post: October 20th, 2012, 12:21 PM
  2. where is the java tutorial for beginners
    By javaque in forum Java Theory & Questions
    Replies: 2
    Last Post: September 14th, 2011, 03:30 AM
  3. Beginners Eclipse Tutorial. How to run first java application on Eclipse?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 13
    Last Post: June 24th, 2011, 12:26 AM
  4. Java Binary Tree (Beginners)?
    By IAmHere in forum Algorithms & Recursion
    Replies: 6
    Last Post: June 19th, 2011, 10:28 AM