4 enter values

icon picker
C enter decimal

enter numbers with decimal fraction to set up brightness and download speed

program requirement

Some user preferences on a smart speaker use number values which include a decimal fraction. For example the brightness preference as a percentage in fraction form. Another important value for an Internet of Things device is the download speed. Often the download speed is given in Megabits per second (Mbps) and typically will involve a decimal number.
This program should ask the owner to enter and store real number values for the brightness level percentage and the expected download speed for the smart speaker.

learning point

Numbers which include a decimal fraction are called real numbers.
In java real numbers are stored in variables with the type double
to read keyboard input and store as a real number the keyboard’s text value has to be converted into a proper double value.
for example typing the keys 1 . 2 3 would be read as “1.23” which is a string
java must convert the String value “1.23” into the integer 1.23 before storing into a double variable

user interface

Here is an an example of a simple dialog where the user is asked to enter preferences using real numbers.
Low brightness level set as decimal fraction
e.g. 0.5 (50%), 0.25 (25%), 0.33 (33%)
Please enter low brightness level: 0.25
The low brightness level is now 0.25

introducing the code

1 double brightness = 0.0;
2 System.out.print("Please enter low brightness level: ");
3 brightness = Double.parseDouble(keyboard.nextLine());
understanding the code
line 1 declares and initialises the double variable which will be used to store the real number entered by the user at the keyboard
line 2 displays a prompt to advise the user to enter a value and describes the value
line 3 reads the text typed typed by the user, converts it into a double and finally stores the number into the variable called brightness.
notice that keyboard.nextLine is nested within the Double.parseDouble() code

developer task

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 brightness value.
Low brightness level set as decimal fraction
e.g. 0.5 (50%), 0.25 (25%), 0.33 (33%)
Please enter low brightness level: 0.25
The low brightness level is now 0.25
Expected download speed in Mbps
Please enter expected download speed: 597.8
Follow the PRIMM stages below to develop your code.

program code

Loading…

PRIMM

predict
Read the code and assume that the user wants to set a brightness to 0.5. Can you anticipate what would appear on the console when the program is run?
only the 0.5 should be entered, not 50%

run
go ahead and run the program, enter a timer value of your own in response to the prompt, check that the program replies to you with your timer value
run the program again and enter a different brightness

investigate
what happens if you do type 50%
you will see an error message NumberFormatException
the unit of measure for a number is never input with the number itself, only the actual digits making up the number
try entering just.25 rather than 0.25
what value should you enter for 100 percent

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 brightness value.
Low brightness level set as decimal fraction
e.g. 0.5 (50%), 0.25 (25%), 0.33 (33%)
Please enter low brightness level: 0.25
The low brightness level is now 0.25
Expected download speed in Mbps
Please enter expected download speed: 597.8

make

Can you create more dialog to set another preference with real numbers?
you can decide how to continue with a different prompt
you will need to create another double variable to store the user’s response
remember to use the code for prompting and reading the user’s response

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.