University of Washington, AP/CS A

Lab 2: Expressions

Except where otherwise noted, the contents of this document are Copyright 2013 Stuart Reges and Marty Stepp.

lab document created by Marty Stepp, Stuart Reges and Whitaker Brand

Today's lab

Goals for this lab:

Expressions

Recall that Java has expressions to represent math and other computations. Expressions may use operators, which are evaluated according to rules of precedence. Every expression produces a value of a given type.

Type Description Expression Example Result
int integers (up to 231 - 1) 3 + 4 * 5 23
double real numbers (up to 10308) 3.0 / 2.0 + 4.1 5.6
String text characters "hi" + (1 + 1) + "u" "hi2u"

Exercise : Expressions (2.1)

Write the results of each of the following expressions. If you're stuck, ask a TA or neighbor.

12 / 5
2
12.0 / 5
2.4
12 / 5 + 8 / 4
4
3 * 4 + 15 / 2
19
42 % 5 + 3 % 16***
5
***Hint: Suppose you have 3 apples, and you distribute them among 16 people so that everyone gets the same number of (whole) apples. How many (whole) apples does each person get? How many apples do you have left over?

Exercise : More expressions (2.1)

Write the results of each of the following expressions.

Some of these expressions include String concatenation. When the result of the expression evaluates to a String, put "quotation marks" around the result:

"x" + 2
"x2"
// Add the other quote to get the correct answer
2 + 6 + "cse 142"
"8cse 142"
"cse 142" + 2 + 6
"cse 14226"
1 + 9 / 2 * 2.0
9.0
46 / 3 / 2.0 / 3 * 4/5
2.0

Exercise : Expressions practice-it

For each expression in the left-hand column, indicate its value in the right-hand column. Be sure to list a constant of appropriate type (e.g., 7.0 rather than 7 for a double, Strings in quotes).

4 * (2 + 4) - 3 * 5
9
54 % 10 + 8 * 3 % 9
10
3 * 2 + 4 + "+" + 2 + 3 * 4
"10+212"
2.3 * 3 + 19 / 5 / 2 + 6.0 / 5
9.1
108 / 20 * 3 / 4 / 2.0 + 1.0 / 2
2.0

Exercise : Expressions

For each expression in the left-hand column, indicate its value in the right-hand column. Be sure to list a constant of appropriate type (e.g., 7.0 rather than 7 for a double, Strings in quotes).

If you've forgotten how to tackle expressions problems, check out lab 2 for a recap!

12/5 + 8/4
4
2.5 * 2 + 17/4
9.0
41 % 15 % 7 + 17 % 3
6
21/2 + "7 % 3" + 17 % 4
"107 % 31"
46/3/2.0/3 * 4/5
2.0

Exercise : Expressions practice-it

For each expression in the left-hand column, indicate its value in the right-hand column. Be sure to list a constant of appropriate type (e.g., 7.0 rather than 7 for a double, Strings in quotes).

1 + 9 / 2 * 2.0
9.0
5.0 / (3125 % 2) + 2 * (5 / 3)
7.0
6 % 17 + 9 % 3 + 22 / 4 / 2.0
8.5
"[" + 2 + 4 * 2.0 + "]" + 3
"[28.0]3"
!(3 < 2) && (4.3 > 3 || 3 < 2)
true

Exercise : Expressions practice-it

For each expression in the left-hand column, indicate its value in the right-hand column. Be sure to list a constant of appropriate type (e.g., 7.0 rather than 7 for a double, Strings in quotes).

3 + 3 * 8 - 2
25
109 % 100 / 2 + 3 * 3 / 2.0
8.5
1 - 3 / 6 * 2.0 + 14 / 5
3.0
1 + "x" + 11 / 10 + " is" + 10 / 2
"1x1 is5"
10 % 8 * 10 % 8 * 10 % 8
0

Exercise : Boolean Expressions

Write the result of each expression as either true or false, given the following variables.

int x = 12;
int y = 7;
int z = 28;
String s = "mid term";
x < 14
true
!(x % 2 < 1)
false
x < y || x < z
true
z / x < x / y * x
true
s.length() == y
false
s.toUpperCase().equals("MID TERM")
true
!s.equals("mid term") || x * y != z
true
s.substring(z / x).length() > y
false