Home / Expert Answers / Computer Science / must-be-in-java-objectives-practice-do-while-loop-data-type-validation-and-random-class-pr-pa657

(Solved): Must be in java Objectives Practice do-while loop, data type validation, and Random class. Pr ...



Must be in java

Objectives
Practice do-while loop, data type validation, and Random class.
Problem
Write a program that simulates a person tr

- Add steps to the distance variable
- Display a message indicating the number of the steps that the person moved up
- else
-

What is the height of the mountain: fddfdf
What is the height of the mountain: \( -345 \)
What is the height of the mountain:

Objectives Practice do-while loop, data type validation, and Random class. Problem Write a program that simulates a person trying to climb up a mountain. The person starts on the ground, at height 0 . Each iteration, the person climbing up the mountain either moves up a random number of steps between 20 and 30 . Or the person slips down a random number of steps between 1 and 5 . There is a \( 50 \% \) chance on each iteration that the person either climbs up or slips down therefore a random number 0 or 1 must be generated to decide if the person will climb up or slips down. Refer to the sample output. Requirements - Must provide all the method - Must follow the requirements - You can come up with your own solution but make sure that there are at lease 3-4 methods including the main method - Your output will not match my output since we are using the random class and numbers are being generated randomly. - Refer to the rubric regarding the comments, indentation, proper naming, and other requirements. Methods Public static int input (Scanner kb): this method asks the user for the height of the mountain. As long as the user is not entering a valid height, your program must ask the user again. Must use a while and a do while loop in this method. This method must check for two things: integer value and also it must be a positive integer. Refer to the video for the sample code. The mountain height that user enters must be an integer greater than zero. (data type validation and range validation are being done) public static int climb (int height, Random rand): This method gets the height of the mountain and a Random object as its parameters. Do-while must be used Declare variables distance and slip before the do-while loop and set them to 0 Do\{ - A random number must be generated at each iteration. There is a \( 50 \% \) chance on each iteration that the person climbs up steps or slips down steps. - A random number 0 or 1 must be generated: rand.nextInt(2) - If the generated random number is a one then - Generate a random number between 20 and 30 so that the person can climb up that many steps. Name this variable steps If (height-distance \( < \) steps) steps \( = \) height \( - \) distance - Add steps to the distance variable - Display a message indicating the number of the steps that the person moved up - else - generate a random number between 1 and 5 indicating the number of the steps the person will slip down. 0 If \( ( \) distance \( !=0 \) ) \{ Decrement the distance variable by the number of the steps the person slipped down Display a message indicating the number of the steps the person slipped. \}\( / / \) end if \}//end of do-while - The do-while loop continues until the person gets to the top of the mountain, meaning that the distance variable will be the same as the height of the mountain Public static void execute(Random rand, Scanner kb): This method uses a while loop so that the program can run many times. boolean repeat \( = \) true; While(repeat) \{ //call the method input, pass the proper parameters, remember that the method input returns an integer. Make sure to store the return in a variable since you need to pass it to the method climb. // call the method climb, pass the proper parameters, this method returns the number of the slips, make sure to store it in a variable since you need to display it. \( / / \) display the number of the slips // ask the user if they want to run the program again \( / / \) if the user says "no" then repeat \( = \) false; \} public static void main (String [] args): this method creates objects of Scanner and Random and then calls the method execute. Public static void description0: displays a description of the program. What is the height of the mountain: fddfdf What is the height of the mountain: \( -345 \) What is the height of the mountain: 345 Sorry you slipped down 1 step (s) You climbed up 23 steps You climbed up 28 steps Sorry you slipped down 5 step (s) Sorry you slipped down 5 step (s) You climbed up 28 steps Sorry you slipped down 2 step (s) You climbed up 26 steps You climbed up 26 steps You climbed up 26 steps You climbed up 23 steps You climbed up 22 steps You climbed up 27 steps Sorry you slipped down 5 step (s) You climbed up 24 steps You climbed up 23 steps Sorry you slipped down 2 step (s) You climbed up 23 steps You climbed up 21 steps Sorry you slipped down 5 step (s) Sorry you slipped down 2 step (s) You climbed up 28 steps You climbed up 23 steps While climbing up the mountain with the height 345, you sliped 26 times another run: yes What is the height of the mountain: eff What is the height of the mountain: 55 You climbed up 24 steps You climbed up 26 steps Sorry you slipped down 2 step (s) You climbed up 7 steps While climbing up the mountain with the height 55, you sliped 2 times another run: yes What is the height of the mountain: 40 Sorry you slipped down 2 step (s) Sorry you slipped down 1 step (s) You climbed up 28 steps Sorry you slipped down 4 step (s) You climbed up 16 steps While climbing up the mountain with the height 40 , you sliped 4 times another run: no Good bye


We have an Answer from Expert

View Expert Answer

Expert Answer


Answer Step 1/2 Do-while loop a, data type validation and Random class: main ( ) method: This method creates objects of Scanner and Random and then ca
We have an Answer from Expert

Buy This Answer $5

Place Order

We Provide Services Across The Globe