While
and for loops, Scanner
The goal for this lab is to gain experience using different
kinds of loops for repeating the same command or series of commands multiple
times. We will also see how to
solicit input from the user via the terminal window using the Scanner class. Part of the lab involves creating a class
that gets input from the user in the terminal window and performs an action
based on that input. The other part
of the lab involves implementing a class to perform those actions with the
user's input.
Go menu select Connect to Server.... Type afp://fileserver1 or choose it from the
list of selections and select the Personal volume
when prompted.numberOfBinaryDigits that takes a single parameter
of type int (don't forget to give it a name) and also returns an
integer. It will calculate
the number of binary digits required to store the specified number. In the body of the method, i.e.
between { } add the following
lines (with your variable name in place of _____):
int numberOfDigits = 1;
//keep dividing by 2 and adding 1 to numberOfDigits until result <
1
while (_____ / 2 >= 1)
{
______ = ______ / 2;
numberOfDigits++;
}
Add
a final line that returns the contents of the numberOfDigits variable. Compile and fix any syntax errors, but
don't test it yet.
import
java.util.Scanner;). Also add a main method to the
class. Recall that the main
method is always public and static, returns void and takes a single
parameter called args that is of the String array type (denoted
String[]). Don't forget to
add { and } to denote the beginning and end of the method's body.
new Scanner(System.in)). Add a line after that which prints
the message "Please enter a positive integer: " to the terminal
window using the print() rather than println() method. In the next line, call the nextInt() method for input and assign the result to
a variable of type int. Don't
forget to give your variable a meaningful name (int ____ = input.nextInt()). numberOfBinaryDigits()method in the CSMath
class with your variable as a parameter (CSMath.numberOfBinaryDigits(____)) and prints the
integer the user entered and the integer that the method returns to the
terminal window in a phrase like "The number of binary digits need
for ___ is ____." Use
println() for this message and remember you can use + to concatenate
strings.
import java.util.logging.*; as the first line in
your CSMathApplication class (even before the class
interface/declaration). Then
use the method Logger.global.info() instead of System.out.println() (it works the same
way, but just send the result to a different place. The advantage of using Logger is
that you can leave commands in your final version and users of your class
won't see those messages that are helpful to you the developer. To turn logging off add the
command Logger.global.setLevel(Level.OFF) at the very beginning
of your main. Try adding a
call to the info() method for each of the variables that contain a
user-entered value right after input.nextInt() assigns them a value
to verify they are correctly set to what the user enters. See Java class libraries for
additional logging information if interested.
numberOfBinaryDigits(). Then add a line that
calls the static method displayFibonnaciNumbers() with variable that
contains the user's input as a parameter (CSMath.displayFibonnaciNumbers(___);). The method will return void
so you don't need to assign it to anything. ThatŐs what void means: nothing, emptiness, nada.
displayFibonnaciNumbers() that returns void and
takes a single integer parameter.
Use a for loop and the specified parameter to compute and display
each Fibonacci number on a separate line in the terminal window. The first and second Fibonacci
numbers are both 1. The third
Fibonacci number is the sum of the first and second ones, and the nth
Fibonacci number is the sum of the (n-1)st and (n-2)nd
ones. Hence, you need to
store the last two numbers computed to get the next one. Don't forget to update the value
of the last two numbers inside the for loop. You may need some logic to check for a parameter value
less than 1 and possibly to handle the case when only the first Fibonacci
should be printed. Work
through it on paper. CSMath. displayFibonnaciNumbers (7); should display
1
1
2
3
5
8
13
displayIntegerFactors() in the CSMath class
(it will return void).
displayIntegerFactors()
method to your CSMath class that returns void and takes a single integer
parameter. You should print
each factor of the specified parameter (assumed to be a positive integer)
on a separate line. For
example CSMath.displayIntegerFactors(8);
should display
2
2
2
Start by creating and initializing an integer variable to 2 (called factor for example). If the number is divisible by factor (2) you should print factor (2), and otherwise you should add 1 to factor and repeat until a factor is found that divides the number. Recall that the % operator gives the remainder of division. For example 3%2 is equal to 1, and 4%2 is equal to 0. Once you find a factor that divides the number and print the factor, call the method again (yes you can call a method from itself!) with the number/factor as the parameter (new number to factor). This is an example of recursion. You will need to add a check somewhere to make sure this process doesn't go on forever. In infinite loop is a very common mistake when working with while loops. Consider it another rite of passage. If this happens DON'T PANIC. To have BlueJ halt execution of method go to the project window. In the lower left corner you should see a whit and red striped animated bar (like a barbershop pole or candy cane). Control-click (Windows right-click) on that bar and select "Reset Machine" or hold down the shift and open apple keys and type R. See, no reason to panic.
numberOfBinaryDigits(),
2 for displayFibonnaciNumbers(),
3 for displayIntegerFactors(). Use the appropriate Scanner class
method to read this value (nextInt()
or next() for
Strings), ask for and read a second input which is what you were
previously asking for the method they chose.