In computer science we typically can represent three main types of expressions: numeric, logical and system-related operations. In this lecture, we will introduce the second type of computations; logical expressions and how they can be implemented using Java.
Logical expressions
We can start by defining logical operations and how they can be expressed in Java.
Branching
In order to represent many complex logical concepts, one may often need to use the concept of branching, alternative flows in the execution of a program. In this section, we study how branching can be defined and represented in activity diagrams.
If Statements
The most straightforward syntactic clause to implement conditional execution in Java is the if statement. This short lecture presents the syntax and usage of if statements in Java.
Exercise
Implement the game Rock, Paper, Scissors using if-else statements. In this game, you are going to:
- Ask the user to pick one of the three: Rock, Paper, Scissors
- Randomly pick a hand for the computer
- After reading user’s input, check if the user wins based on the following rules: 1) Paper beats Rock, 2) Scissors beats Paper, 3) Rock beats Scissors.
- If both pick the same hand, the game will end in a draw.
Please see Gradescope to submit your full solution implementing the game with if-else statements.
Solution
The game essentially consists of three different conditions. The first condition is when the computer and the user picks the same hand, thus there would be no winners. In the second case, the user wins by picking one of the three situations where they are in advantage. In the third case the user loses, again by picking on of the three remaining scenarios. The representation of each hand can be implemented using integers (1 for Rock, 2 for Paper and 3 for Scissorrs). We can implement the computer’s pick by using the random number generation method of the Math class.
Switch/Case Statements
In cases when different branches that the program may follow during the execution depend on the value of a particular variable, it may be more convenient to use the switch/case statements. Here we briefly discuss how to use switch clauses in Java.
Exercise
Take this chance to practice the switch-case statements now to implement the Rock, Paper, Scissors game using the switch-case statements instead.
Solution
In this exercise, we represent each selection claus using a switch-case statement. For each of the user preferences, we can create a nested switch-case clause that operates over the possible computer preferences. See the implementation below for a quick reference to an example application.
The Ternary Operator
A final alternative to simply write single-line conditional selections will be the ternary operator, which will be very convenient in implementing many double-branched selective executions. The usage is described in the lecture below.
After completing the module, don’t forget to complete the self-assessment quiz to strengthen your theoretical understanding of the newly discussed concepts.