Objectives

Students will be able to:

  • understand that computer programs can follow different paths.
  • understand logical operators such as >, <, ==, !=
  • understand the different symbols for drawing a flowchart.

Solution

int money = //assign a value to the variable;

if(money>20){

getHaircut();

buyHat();

cinema();

}else if(money>10){

buyPizza();

}else{

readBook();

}

goToBed();

Note: goToBed() is called no matter which path is taken through the program...

Selection

 

Scenario

A Smart ATM recommends how to spend the amount of money that is withdrawn from an account.

Depending on the amount, the computer program will follow a different path.

Click the button to see the starter code for the simulator.

Task

Open the worksheet and complete each task.

Note, the starter code does not include exception handling and the program will crash with invalid input.

As a computer scientist, you should be able to predict this and include appropriate exception handling in your code.

Submission

Submit your worksheet and .java file as instructed.

Comparison Operators Review

To help computer programs compare values, relational operators are used. Here is a table summarising those commonly used in Java:

operatordescription
>greater than
<less than
>=greater than or equal to
<=less than or equal to
==the same as
!=not the same as

Helper Video

Tags

selectionsequence if else compile relational operator syntax flowchartalgorithm