Lesson 1.3 · Unit 1 · CED topic 1.3
Expressions and output
A program is only useful if it can show you something and calculate something. This lesson covers both: the statements that display output, and the expressions that compute values from numbers and variables. Expressions are the core of every program in the course, and the exam tests them constantly, so the details matter: what division really does with whole numbers, what the remainder operator is for, and the order in which operators run.
Learning objectives.
- 1.3.A. Develop code to generate output and determine the result that would be displayed.
- 1.3.B. Develop code to utilize string literals and determine the result of using string literals.
- 1.3.C. Develop code for arithmetic expressions and determine the result of these expressions.
Essential knowledge.
- 1.3.A.1. System.out.print and System.out.println display information on the computer display. System.out.println moves the cursor to a new line after the information has been displayed, while System.out.print does not.
- 1.3.B.1. A literal is the code representation of a fixed value.
- 1.3.B.2. A string literal is a sequence of characters enclosed in double quotes.
- 1.3.B.3. Escape sequences are special sequences of characters that can be included in a string. They start with a \ and have a special meaning in Java. Escape sequences used in this course include double quote \", backslash \\, and newline \n.
- 1.3.C.1. Arithmetic expressions, which consist of numeric values, variables, and operators, include expressions of type int and double.
- 1.3.C.2. The arithmetic operators consist of addition +, subtraction -, multiplication *, division /, and remainder %. An arithmetic operation that uses two int values will evaluate to an int value. An arithmetic operation that uses at least one double value will evaluate to a double value. X EXCLUSION STATEMENT—Expressions that result in special double values (e.g., infinities and NaN) are outside the scope of the AP Computer Science A course and exam.
- 1.3.C.3. When dividing numeric values that are both int values, the result is only the integer portion of the quotient. When dividing numeric values that use at least one double value, the result is the quotient.
- 1.3.C.4. The remainder operator % is used to compute the remainder when one number a is divided by another number b. X EXCLUSION STATEMENT—The use of values less than 0 for a and the use of values less than or equal to 0 for b is outside the scope of the AP Computer Science A course and exam.
- 1.3.C.5. Operators can be used to construct compound expressions. At compile time, numeric values are associated with operators according to operator precedence to determine how they are grouped. Parentheses can be used to modify operator precedence. Multiplication, division, and remainder have precedence over addition and subtraction. Operators with the same precedence are evaluated from left to right.
- 1.3.C.6. An attempt to divide an integer by the integer zero will result in an ArithmeticException. X EXCLUSION STATEMENT—The use of dividing by zero when one numeric value is a double is outside the scope of the AP Computer Science A course and exam.
Displaying output
System.out.print and System.out.println both display what you give them. The only difference is the cursor: println moves it to a new line afterward, and print does not.
System.out.print("Hello");
System.out.print("World");
System.out.println("!");
System.out.println("Next line");
HelloWorld! Next line
A literal is the code representation of a fixed value. A string literal is a sequence of characters enclosed in double quotes. To put certain characters inside a string literal you use an escape sequence, which starts with a backslash and has a special meaning in Java. Three are required in this course.
| Escape sequence | Meaning | Example | Displays |
|---|---|---|---|
\" | a double quote | "She said \"hi\"" | She said "hi" |
\\ | one backslash | "C:\\temp" | C:\temp |
\n | a new line | "one\ntwo" | one then two on the next line |
System.out.println("She said \"hi\" and left.\nGoodbye");
She said "hi" and left. Goodbye
Arithmetic expressions
An arithmetic expression is made of numeric values, variables, and operators. The five arithmetic operators are addition +, subtraction -, multiplication *, division /, and remainder %. The type of the result depends on the types of the operands.
- An operation on two
intvalues evaluates to anint. - An operation with at least one
doublevalue evaluates to adouble.
### Division
When both operands are int, / produces only the integer part of the quotient: the digits after the decimal point are discarded, not rounded. When at least one operand is a double, the result is the full quotient.
| Expression | Value | Why |
|---|---|---|
7 / 2 | 3 | both int: integer part of 3.5 |
7.0 / 2 | 3.5 | one double, so a double quotient |
7 / 2.0 | 3.5 | same |
1 / 4 | 0 | the integer part of 0.25 |
1.0 / 4 | 0.25 |
Dividing an int by the integer 0 throws an ArithmeticException while the program runs.
### The remainder operator
a % b is the remainder when a is divided by b. On the exam, a is zero or positive and b is positive. 17 % 5 is 2 because 5 goes into 17 three times (15) with 2 left over. Four uses show up again and again.
| Task | Expression | Example |
|---|---|---|
| Is a number even? | n % 2 == 0 | 10 % 2 is 0 |
| Last digit of a number | n % 10 | 4073 % 10 is 3 |
| Remove the last digit | n / 10 | 4073 / 10 is 407 |
| Wrap around a range | (start + steps) % size | (5 + 4) % 7 is 2 |
### Precedence
Operators are grouped before the expression is evaluated. Multiplication, division, and remainder have precedence over addition and subtraction. Operators with the same precedence evaluate from left to right. Parentheses change the order, and they are the cheapest insurance against mistakes.
System.out.println(2 + 3 * 4); System.out.println((2 + 3) * 4); System.out.println(20 - 4 - 3); System.out.println(20 / 4 * 2); System.out.println(20 / (4 * 2));
14 20 13 10 2
The last two are the classic trap: 20 / 4 * 2 goes left to right, so it is 5 * 2. The fifth expression's parentheses force 4 * 2 first, so it is 20 / 8, which is 2 in integer division.
Trace the values and say what is printed.
int minutes = 135; int hours = minutes / 60; int leftover = minutes % 60; double avg = minutes / 60; double exact = minutes / 60.0; System.out.println(hours + " h " + leftover + " min"); System.out.println(avg); System.out.println(exact);
| Statement | Value stored | Reasoning |
|---|---|---|
hours = minutes / 60 | 2 | 135 / 60 is integer division |
leftover = minutes % 60 | 15 | 135 minus 120 |
avg = minutes / 60 | 2.0 | the division happens first, on two int values, giving 2; then the int is stored in a double |
exact = minutes / 60.0 | 2.25 | one double operand, so a real quotient |
2 h 15 min 2.0 2.25
The line avg = minutes / 60 is the most common wrong answer on the exam. The type of the variable on the left does not change the type of the operation on the right.
Print the tens digit and the ones digit of 74, and the number with its digits reversed.
int n = 74; int ones = n % 10; int tens = n / 10; int reversed = ones * 10 + tens; System.out.println(tens + " " + ones); System.out.println(reversed);
7 4 47
74 % 10 is the remainder, 4, so it is the ones digit. 74 / 10 throws away the 4 and keeps 7. The reversed number is rebuilt with place values: 4 tens and 7 ones.
Evaluate 3 + 6 / 4 * 2 - 5 % 3, showing the order of evaluation.
System.out.println(3 + 6 / 4 * 2 - 5 % 3);
3
Step by step, the operators /, *, and % run first, left to right: 6 / 4 is 1, then 1 * 2 is 2, and 5 % 3 is 2. What remains is 3 + 2 - 2, evaluated left to right: 5 - 2, which is 3.
Expect questions that ask for the value of an expression with mixed int and double operands, and questions where the answer depends on integer division or the remainder. The usual wrong answers come from applying the type of the variable instead of the type of the operands, rounding where Java truncates, or ignoring precedence. Write out the order of evaluation when a line has more than two operators. The exam stays with non-negative values for % and never divides by zero in a double expression.
- What is printed?
System.out.println(9 / 2); System.out.println(9 % 2); System.out.println(9 / 2.0);
Show answer
4, then1, then4.5. The first quotient is integer division (4.5 truncated to 4), the remainder of 9 divided by 2 is 1, and the third has adoubleoperand, so the quotient is exact. - What is the value of
10 - 4 * 2 + 6 / 3?System.out.println(10 - 4 * 2 + 6 / 3);
Show answer
4. Multiplication and division first:4 * 2is 8 and6 / 3is 2. Then10 - 8 + 2left to right is 4. - A class has 25 students and tables seat 4. Write expressions for the number of full tables and the number of students left over.
int students = 25; int fullTables = students / 4; int leftOver = students % 4; System.out.println(fullTables + " " + leftOver);
Show answer
Full tables is
students / 4, which is 6; students left over isstudents % 4, which is 1. Integer division and remainder are a matched pair:(students / 4) * 4 + students % 4always equalsstudents. - What is printed?
double result = 5 / 2; System.out.println(result);
Show answer
2.0. The right side is evaluated first as integer division, giving 2, and thatintis then widened to adoublefor storage. - Write the statement that displays exactly this output on two lines:
Name: "Ada"and thenDone.System.out.println("Name: \"Ada\"\nDone");Show answer
The escape sequence
\"puts a double quote inside the string and\nstarts a new line, so the singleprintlnabove produces both lines. - What does this display?
System.out.print("A"); System.out.println("B"); System.out.print("C");Show answer
ABon the first line andCon the second. Theprintlnends the first line, and the lastprintleaves the cursor right after theC.