5 update

icon picker
Update a value

update a variable with a new value

program requirement

One purpose of asking for user input is to update a variable with the latest value. For example personal devices will have some kind of security code or password to prevent unauthorised access. However new devices may have a standard security code which should be set to a new value by the owner.
This program should ask the user to review default security codes and update them.

learning point

You will remember that a program variable stores a value.
The variable can be assigned another value.
The new value replaces the original value since a variable can only store one value at a time
Once the original value is replaced it is no longer available
Displaying the current value a variable is storing is an important way of following what a program is doing when the code is updating or changing the value stored.

user interface design

Here is an an example of a dialog where the user is asked to update an existing value
The temporary passcode is 123456
Please enter a new pass code: 937758
You're new passcode is 937758
Your temporary password is ZYXW0987
Enter a new secure password: a!9R_pDN
You're new password is a!9R_pDN
understanding the input-output dialog
the text displayed by the computer is in black the user’s response in red
the user is shown the original value stored by the program and then types a response after reading a prompt which is a message the program outputs to request input
a good prompt will give the user a helpful description of what to enter
of course this is not a very secure program since the passcode is displayed in clear text!

introducing the code

1 int passCode = 0;
2 passCode = 123456;
3 System.out.println("The temporary passcode is " + passCode);
4 System.out.print("Please enter a new pass code: ");
5 passCode = Integer.parseInt(keyboard.nextLine());
6 System.out.println("You're new passcode is " + passCode);
understanding the code
line 1: the passCode variable is declared and initialised to 0, 0 is the usual initial value for an integer
setting any variable to a default initial value ensures that it does have a value
line 2: the standard passCode is set to a dummy value that the user must change as soon as possible, the user is shown the dummy value on line 3
lines 4, 5: the user is prompted for a new secret passCode and it is entered and stored
line 6: the user is shown the passCode for confirmation
notice that passCode has stored 3 different values, each time the new value replaces the previous value

developer task

Carry out the PRIMM activities below the program code block

program code

Loading…

PRIMM


predict
Read the code and assume that the user's new password is 557129. Can you anticipate what would appear on the console when the program is run?

run
go ahead and run the program, enter your own passcode

investigate
what happens if you just press enter without typing a number?
you should get an error message
:
NumberFormatException this is because you entered a text value (an empty String) which cannot be converted into a number
preventing this kind of run-time error is for another course!

modify
Here is the full dialog that the program should create with the user.
Read, copy and re-use the code you already have for entering the passcode.
The temporary passcode is 123456
Please enter a new pass code: 937758
You're new passcode is 937758
Your temporary password is ZYXW0987
Enter a new secure password: a!9R_pDN
You're new password is a!9R_pDN

make

there is no special make task for this update task but feel free to create your own scenario with a requirement to update a stored value

Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.