Starting from:
$30

$24

Lab 04 ​while​loops Solved

Lab Objectives: ​while​loops





    a. Create a new project Lab04a. Write a program that takes an integer, ​n​, from the user and calculates the largest power of ​2 less than n​. Display the result on the screen. For example, if the user entered n = 64 the computer should give the result as 32, because
the largest power of 2 less than 64 is 25​​= 32.
You should ​validate​the user input as shown in the sample runs.

Note:​You are not allowed to use Math class functions to solve this problem.


    b. Create a new project Lab04b. Write a guessing game between the user (player) and the computer. The program should generate a random integer between 1 and 100. The user should be prompted to guess the number, or enter 0 to quit the game. Display a message to the player according to the following:

        ◦ If the player’s guess is within 20 (but more than 10) of the correct number, display “You are getting warmer.”

        ◦ If the player’s guess is within 10 or less of the correct number, display “You are hot!”

        ◦ If the player’s guess is more than 20 away from the correct number, display “You are cold…”

The game should keep inputting guesses until either the player quits or guesses correctly. You should validate the player’s guess, and prompt for new value when invalid input is entered. When the game finishes display an appropriate message to the player. See sample runs below.

Note: You can use Math.random() method which generates a random double value between 0 and 1. You will have to do some calculations to convert the number to an int between 1 and 100.

Sample runs:




    c. Create a new project Lab04c.

        1. Write a program that inputs a sequence of integers from the user until the user enters an input which is not an integer and does the followings:

            ▪ Display all adjacent duplicates. For example, if the input is 3 3 5 5 5 4 7 7 4 6 2 2 3 3 3 3 8 0.5, the program should display “Duplicates: 3 5 7 2 3”.

            ▪ Display the maximum and minimum of the inputs.

            ▪ Display the sum and average of the inputs.

            ▪ Display the count of the inputs and the number of even inputs.
Note:​​Do not store all values read from the user; simply process each one as it is read!
Note:​You are not allowed to use Math class methods to solve this problem.

Sample runs:






    2. In the same folder as your .class file, create a text file, "testdata.txt", containing some test data, e.g. the values 3 3 5 5 5 4 7 7 4 6 2 2 3 3 3 3 8 X, space separated or one value per line.

Open a command prompt and navigate to the folder containing your class file. From the command prompt, run your program by typing the command "java Lab04c < testdata.txt", which should cause your program to read its input from the specified file, rather than the keyboard. It will still output its results to the command prompt. ​Note: you can have your program read data from one file and send the output to another file --as you did in the previous lab assignments-- by


typing "java Lab04c < testdata.txt > results.txt" Try it, then open "results.txt" to see what happened!

Download and save realdata.txt text file, then run your program reading input from the given file ​realdata.txt​.


Sample runs:




Note: ​The last sample runs were taken on Ubuntu. Command window on Windows or Mac will look slightly different.



























4

More products