6 caculate

icon picker

using Boolean values and logic to produce a result

program requirement

Some AI (artificial intelligence) apps are used to help humans complete decision making by using logic. The boolean values true and false can be used in expressions which have logic operations instead of arithmetic operations.
Equipment safety checks will pass overall when all the individual components test safe.
This program should ask the user to enter the safety of two components. The program should use logic operations to produce the overall safety result. The final safety result should be displayed.

learning point

You will remember that there are two boolean values: true, false
There are three basic logic operations (AND, OR, NOT) and are defined like this:
true AND true produces true
true AND false, false AND true, false AND false all produce false
true OR false, false OR true, true OR true all produce true
false OR false produces false
NOT(true) produces false
NOT(false) produces true
in java AND is coded as &&, OR is coded as || and NOT is !

expected output

Does the front light turn on? true
Does the back light turn on? true
Bike safe to ride: true
understanding the output
the user has entered two boolean values, both are true
the program has used logic that uses a rule which says a bike is only safe when both the front light and the backlight operate successfully

introducing the code

1 boolean frontLightOK = false;
2 boolean backLightOK = false;
3 boolean bikeLightsOK = false;
4 System.out.print("Does the front light turn on? ");
5 frontLightOK = Boolean.parseBoolean(keyboard.nextLine());
6 System.out.print("Does the back light turn on? ");
7 backLightOK = Boolean.parseBoolean(keyboard.nextLine());
8 bikeLightsOK = frontLightOK && backLightOK;
9 System.out.println("Bike safe to ride: " + bikeLightsOK);
understanding the code
lines 1-3 the boolean variables are initialised to false, also described as setting them to clear
lines 4-7 the user is prompted to enter two boolean values for the state of the font and back lights
line 8 includes the expression frontLightOK && backLightOK
this expression includes the AND logic operator so the result will be true when both variables are true, otherwise the result is false
the boolean result is then assigned to the variable bikeLightsOK
line 9: displays the value of the boolean variable bikeLightsOK

the program code



Read the code and assume the user enters the value true for both the front and back lights

go ahead and run the program, enter the example values used above, then run the program again and enter your own test values
look at the results displayed by the program, these are called the actual output
check that the expected output is the same as the actual output

try out the other 3 combinations of boolean values for the bike lights: true/false, false/true and false/false
compare the results you see with the learning points
look at the file in the project frame for a full test plan
try changing the logic operators from AND to OR, replace the && operator with ||
run the program again with the same test values for the bike lights
compare the output with the original results and the learning points
change the operators back to AND &&

Move directly on to the make task


Computers and laptops can have several ways to make a connection to a network like the Internet. Many will have both a WiFi interface and also an ethernet (wired) interface. So there will be a network connection available when either or both interfaces are active.


Can you create an Input-Process-Output sequence to calculate the number of flights still left to climb and the average per day to reach the target. The output dialog would look like this.
Is the WiFi interface active?: false
Is the ethernet LAN interface active?: true
Network connetion available: true
Think carefully about how you would do the calculation and what types of data and what variables would be involved. WHat logical operator should you use?
look at the file in the project frame for a full test plan
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
) instead.