Basic Java Lab 5

profilebpinomoore
Lab5.pdf

ISM3230 In-class Lab Module 5 – Loop Logic Spring 2021

FOCUS TOPICS

• Looping logic

TASK

You are working in a large organization, in an IT department. Your employer has asked you to build a

password generator to provide long randomized passwords for the company’s more secure systems. As

an initial prototype, you have been asked to show the functionality with short generated strings, to

show a proof-of-concept. For this prototype, your password-generation software should do the

following:

• Produce a 5-character string consisting of randomized uppercase and lowercase characters.

o Each character should have a 50% probability of being uppercase, and a 50% probability of

being lowercase which can be achieved by generating a random Boolean value of true or

false.

• Prompt the user to re-type the generated password until they produce a correct copy of the

password.

USEFUL CODE FOR THIS TASK

• The Random class will generate random numbers.

o Declare a variable of the Random class in the same way you declare a Scanner variable:

▪ Random ranVariable = new Random(); (and include an import for

java.util.Random)

o Get a random integer using the nextInt(int upperLimit) method.

▪ The nextInt method generates an integer in the range from 0 up to (but not

including) the upperLimit value

o Get a random boolean value using the nextBoolean() method.

▪ The nextBoolean method returns a true or false value with a probability of 50%

each.

• Characters are associated with integer values in the Java character encoding

o These characters are equivalent to the ASCII character encoding for the familiar Latin

alphabet

▪ Upper-case characters ‘A’ to ‘Z’ are represented by integers in the range 65-90,

where ‘A’ is 65 and ‘Z’ is 90

▪ Lower-case characters ‘a’ to ‘z’ are represented by integers in the range 97-122,

where ‘a’ is 97 and ‘z’ is 122

o See http://www.asciitable.com/ (look at the Dec and Chr columns) for examples

o Use typecasting to convert an integer value to its char equivalent:

▪ int myInt = 66; // We want this to become the character ‘B’

▪ char myChar = (char) myInt; // Converts the integer 66 into the char value ‘B’

• Building a String from nothing, by concatenating characters

o You will want to build the generated password string by concatenating 5 characters

together.

o If you start by declaring a simple String variable for the password, with no value assigned,

you will get an error in NetBeans for trying to add characters to a non-existent String value

o Instead, assign an empty String value using two "" double-quote characters with nothing

between them. This creates a String value that contains no characters, and NetBeans will

allow you to concatenate characters to it.

o See the last few slides of the 3c_stringVariablesAndValues for an example.

INSTRUCTIONS

1. Create four integer class-level constants for the following values:

o MIN_UPPER = 65 // min int value for an uppercase character; 65 is ‘A’

o MIN_LOWER = 97 // min int value for a lowercase character; 97 is ‘a’

o ALPHABET_LENGTH = 26 // there are 26 characters in the alphabet

o PASSWORD_LENGTH = 5 // desired length of passwords

2. First, you need to create one character and test whether you are getting the correct range of

values and the correct character conversion:

o Use the Random nextBoolean() method to determine whether the character will be

upper or lower case. If the value is true, then that is an upper case character,

otherwise it is a lower case character.

o Use the Random nextInt(int upperLimit) method using the

ALPHABET_LENGTH as the upper limit. This will give you a random number that will tell

you how far into the alphabet your to-be-generated character is

o Add MIN_UPPER or MIN_LOWER to your generated integer value, depending on

whether you need an upper or lower case character. This will give you the integer value

that represents your character.

o Use typecasting to convert the generated integer to a char type.

Checkpoint 1:

Print the following values to the screen:

1. The Boolean random variable, which should print true or false

2. The result of the nextInt random integer method, which should print a number

from 0 to ALPHABET_LENGTH – 1

3. The ASCII code of the character which is the result of the previously generated

random number from checkpoint step 2 plus either the MIN_UPPER or

MIN_LOWER, depending on the value of the random Boolean from checkpoint

step 1.

4. The converted character that has been obtained by casting the above integer

from checkpoint step 3 into a character type value.

Run the code multiple times (many times) to check that you are getting the

characters in the expected range. if you see empty squares or strange looking

symbols, your math may be incorrect; check your random generating code carefully.

Since the boolean is true, we

add 65 to 15 to make an upper P

Since the boolean is false, we

add 97 to 2 to make an lower c

Examples of incorrect math resulting in the characters being out of range:

3. Create a String variable to hold the password, and assign an empty String “” value to it.

4. In order to generate a password of PASSWORD_LENGTH, you will want to repeat the character-

generation for each character

o Before you start writing this code, think about what type of loop would work best for

this, and what the loop-control structure should look like.

o Inside the loop, concatenate the generated character to the password variable

5. When you have generated all PASSWORD_LENGTH characters, print the password

Sample output at this stage:

Note that this is randomly generated – your output will be different!

Unprintable character, appears blank

Character out of desired range

6. Next, you want to repeat the logic associated with the user typing in the password until they

enter a password that matches.

o Before you starting to write this code, think about what type of loop would work best

for this, and what the loop-control structure should look like.

o HINT: Use a boolean variable to control your loop. This variable will represent whether

or not the user has successfully entered the password. For example: set the value to

false to start with (i.e., the user has not been successful yet). When you detect a

matching input string, change the value to true. You can then use this variable as the

condition to remain in the loop (false) or exit the loop (true).

7. For each user attempt at typing the generated password, do the following:

o Prompt the user to enter the generated password

o Read the input using the Scanner nextLine() method, and store it in an appropriately-

typed variable

o Compare the input string to the generated password

▪ If they match, exit the loop

▪ If they do not match, remain in the loop, and return to the “prompt” step

▪ HINT: to compare string values, use a String method firstString.equals(String

secondString) which returns a Boolean result (true when they match, false

otherwise).

8. When the user has successfully entered the password, print the success message.

SAMPLE OUTPUT

*** Note: your password values will be randomized, and will vary from the sample output ***