Lab2 - Java Expressions and Control Structures
Introduction
Now that you have reviewed expressions and control structures, it is a good idea to practice using them. In lecture we looked at (1) while loops, (2) for loops and (3) do loops in Java. We also looked at if statements and switch statements. Furthermore, we looked at Java's Scanner class to allow simple input from the console and enter data at the command-line. In this lab your TA will give you a short programming problem for you to solve using Java during the lab session. Once you have completed the problem, you should do the following:
- Show your TA that your program compiles and executes correctly.
- Answer a short question about how you solved the problem.
- Submit your Java program to the CourseWeb’s Digital Dropbox.
Background
A logarithm is a very useful mathematical function. Using a given base, b, we define Y = logbX to be an exponent such that bY = X. If we are dealing exclusively with integer values, we typically round the log up or down, using the ceiling or floor function. Consider rounding down, using the floor function. This will give us the exponent Y such that bY <= X, but bY+1 > X .
For example, consider b = 10 (i.e. we are considering base 10 logarithms):
floor (log10(123456)) = 5, because 105 = 100000 <= 123456 and 106 = 1000000 > 123456.
As another example, consider b = 2 (i.e. we are considering base 2 logarithms):
floor (log2(64)) = 6, because 26 = 64 <= 64 and 27 = 128 > 64
One way of calculating the floor of the logbX is by repeated integer division. Divide by the base until the quotient is less than the base. The number of completed divisions is equal to the floor of the logbX.
For example, consider the cases above, and one additional case:
123456 / 10 = 12345
12345 / 10 = 1234
1234 / 10 = 123
123 / 10 = 12
12 / 10 = 1 < 10 => 5 divisions;
64 / 2 = 32
32 / 2 = 16
16 / 2 = 8
8 / 2 = 4
4 / 2 = 2
2 / 2 = 1 < 2 => 6 divisions;
4567 / 10 = 456
456 / 10 = 45
45 / 10 = 4 < 10 => 3 divisions.
Program, Grading and Submission
You are to write two versions of a Java program that does the following:
Asks the user to enter an integer base b > 1 (if the number is <= 1 you should quit the program)
Asks the user to enter a positive integer X. Numbers <= 0 should be rejected and the user must re-enter. To implement entering data, use a Scanner for the first version and from the command-line for the second version.
Calculates the floor of logbX in the manner indicated above and outputs the result
The whole process repeats again.
Demonstrate that your program works correctly to your TA by running it with various inputs, showing that it handles all of the cases indicated.
Answer the question asked by your TA.
Notes and Hints
You will need to use nested loops to solve this problem. However, the exact combination of loops is up to you, as long as your logic is correct. However, the combination of loops must be different in the two versions of the solution. If you need help as you are working on the program, ask your TA.
|