Skip to main content

Section 3.1 Worked Example: Writing Expression Statements

Subgoals for Writing Expression Statements.

  1. Determine expression that will yield variable
  2. Determine data type and name of variable and data type of expression
  3. Determine arithmetic equation with operators
  4. Determine expression components
  5. Operators and operands must be compatible

Subsection 3.1.1

Given the following problem, write a Java code snippet that will store the result of the expression in the variable isAffordable:
We want to determine if the cost of a particular restaurant is considered affordable. We have calculated the approximate total for what we want to order, which comes to the amount $126.50. However, we need to remember to add on an additional 20% for the tip. If the total cost of the bill is no greater than $150, then the restaurant is considered affordable.>

Subsection 3.1.2 SG1: Determine expression that will yield variable

The expression is the right-hand-side (RHS) of an assignment statement, and for this problem we want the expression to determine whether or not the restaurant cost is affordable. To determine this we will need to mathematically calculate the total cost of the bill with the tip and then determine if it is no greater than the limit provided ($150).

Subsection 3.1.3 SG2: Determine data type and name of variable and data type of expression

We are given the name of the variable, isAffordable. In this example, the result of the problem is to determine whether or not a particular restaurant is affordable or not – which is a yes or no (or true or false) answer. Therefore, the data type of isAffordable must be boolean.
boolean isAffordable = /* expression goes here */;

Subsection 3.1.4 SG3: Determine arithmetic equation with operators

To calculate the total cost of the restaurant bill, we need to add the estimated cost to the tip amount. To calculate the tip amount, we need to multiply the estimated cost by 20%. There are a couple of different ways to calculate this:
126.50 + 126.50 * 0.2 // adds the cost to 20% of the cost, which is the tip
Or...
126.50 * 1.2 // calculates the cost plus 20%
Note that there is usually more than one way to calculate something. You should choose the way which you understand. Generally doing fewer operations is more efficient, but understanding is more important than efficiency when learning.
Also notice that there are no $ or % symbols in the expressions. These are mathematical symbols that help the human reader understand the numbers, but Java does not need them. (In fact, it will cause errors if you include them in Java expressions. % has its own meaning in Java - as a modulus operator!)
So now we have the part of the RHS of the assignment expression that calculates the total bill:
boolean isAffordable = 126.50 * 1.2 /* rest of expression */;

Subsection 3.1.5 SG4: Determine expression components

We are now to the second part of the problem - determining if the total cost of the restaurant bill is affordable. The problem states that if the total cost of the bill is no greater than $150, then the restaurant is considered affordable.
We need to do a relational comparison of the total bill to the value $150:
boolean isAffordable = (126.50 * 1.2) < 150;
Again note that there is no $ in the expression. We have added ( ) around the calculation for the restaurant bill cost to make it clear that it is a calculation whose result is to be compared. While the ( ) are not needed for precedence (Java would automatically do the * before the <) it helps to remind us that we were calculating something to be compared. If we had chosen the other mathematical expression for calculation, the ( ) are more helpful in clarifying the value to be calculated and compared:
boolean isAffordable = (126.50 + 126.50 * 0.2) < 150;

Subsection 3.1.6 SG5: Operators and operands must be compatible

Our final check is to make sure that the operators and operands are compatible. In our original solution we have two operators, * and <. For the multiplication, both operands are doubles so the result would be a double. For the < operator, the left operand (cost calculation) is a double and the right operand is an integer (150 is a whole number). We can compare a double to an integer using a relational operator and the result will be a boolean value. The result of the < comparison is a boolean, which can be assigned to a boolean variable (isAffordable). So all operations are legal.
Answer.
boolean isAffordable = (126.50 * 1.2) < 150;

Practice Pages.

You have attempted of activities on this page.