Create a folder named section2
for your work on these
exercises. You can find instructions for doing so
here.
In VS Code, select the File->Open Folder or File->Open menu
option, and use the resulting dialog box to find and open your
section2
folder. The name of the folder should appear in the
Explorer pane on the left-hand side of the VS Code window.
Important: In general, you should not use File->Open File to open a specific file in VS Code. Rather, you should create a folder for the files that belong to a given assignment or section, and you should use File->Open Folder or File->Open to open the folder.
Consider the following Java code fragment, which is supposed to
calculate the area of a triangle with base width b
and height h
.
double b = 3.0; double h = 1.5; double area = 1 / 2 * b * h;
When running this code, we
can see that the area
variable is given the value 0.0
, which is
incorrect. Identify the logic error in this code and suggest a fix.
Recall from lecture that an expression is a combination
of literals and operators that can be evaluated to
obtain a final value. For example, the expression 2 * 3
evaluates to 6
.
Show how the expression 1 + 2 * 3 - 4 / 2
is evaluated,
one operation at a time. 1
Show how the expression (3 + 2) * 2 - 4
is evaluated,
one operation at a time. 2
Recall from lecture that a data type is a classification
of a value in memory. Integers have the int
data type,
and the double
type is often used for floating-point
numbers. Prior to today, we’ve used string literals,
which have the String
type.
Each data type has its own set of operators. In Java,
the operator +
is said to be “overloaded” because it works
with many different types. For example, with two numbers
it can be used for addition. With two strings, +
can be
used for string concatenation.
Fill in the two tables below with the type of the result of evaluating an expression with the two specified operand types. The type of the first operand is given by the row and the type of the second operand is given by the column. 3
+ | int | double | String |
---|---|---|---|
int | int | ||
double | double | ||
String | String |
/ | int | double |
---|---|---|
int | ||
double |
Try to work together with at least one other person to solve the following problems.
Show how the values of a
, b
, c
, and d
change as each assignment
and re-assignment statement is executed. Show the values of the
variables after the execution of each line. 4
int a = 0; int b = 1; int c = 3; int d = b * c + 3; a = 2 * c + b; b = b + 1; d = d % b;
Fill in the following table:
Statement | a | b | c | d |
---|---|---|---|---|
int a = 0; | ||||
int b = 1; | ||||
int c = 3; | ||||
int d = b * c + 3; | ||||
a = 2 * c + b; | ||||
b = b + 1; | ||||
d = d % b; |
Create a table that traces the values of the variables. 5
int e = 5; int f = 10; int g = f; e = e + 1; f = f - 1; g = g + e; g = g / e;
Create a table that traces the values of the variables. 6
int i = 4; int j = 2; int k = 2; i = i + 1; i = i * j * k; j = i / j / k; k = i % (k + 1);
For each expression, calculate the result of evaluating it. Be
sure to remember the rules for the /
operator we reviewed.
Note: you can check your answers by entering the original
expressions into VSCode. 7
(12 / 5) + (8 % 3)
(42 % 5) + (16 / 3.0)
((50 / 9) / 2.0) + (200 / (5.0 / 2))
(int)5.9 + 1.9
(double)(5.9 + 1.9)
(int)2.5 * (double)2.5
(int)2.5 * (int)2.5
(double)((int) 3.14159)
What will happen when you try to compile the following program?
Form a hypothesis about what will happen before you copy and
paste the program into a new Java file. If you get an
error, how can you fix it? Assume that the correct output
is 3.14159
. 8
public class Pi { public static void main(String[] args) { int pi; pi = 3.14159; System.out.println(pi); } }
Copy and paste the following program into a new Java file and alter it so that it computes and outputs the
average of the values in the variables a
, b
, c
, d
,
and e
. Note: The computed average must be precise. 9
public class Average { public static void main(String[] args) { int a = 12; int b = 55; int c = 11; int d = 30; int e = 19; // put your code here System.out.println(average); } }
Create a complete Java program in a class named Birthday
that
declares four variables and assigns appropriate values to them.
Use the following values. 10
Ask others in the class for their name and the proper numbers to store in the variables for their birthday. Then, add one or more statements to produce output in this format using your four variables:
My birthday is 6/15, and Libby's is 1/28.
Each line represents a step in the evaluation.
1 + 2 * 3 - 4 / 2 // initial 1 + 6 - 4 / 2 // just did 2 * 3 1 + 6 - 2 // just did 4 / 2 7 - 2 // just did 1 + 6 5 // just did 7 - 2
Each line represents a step in the evaluation.
(3 + 2) * 2 - 4 // initial 5 * 2 - 4 // just did 3 + 2 10 - 4 // just did 5 * 2 6 // just did 10 - 4
Here are the completed tables:
+ | int | double | String |
---|---|---|---|
int | int | double | String |
double | double | double | String |
String | String | String | String |
/ | int | double |
---|---|---|
int | int | double |
double | double | double |
Here is the completed table:
Statement | a | b | c | d |
---|---|---|---|---|
int a = 0; | 0 | none | none | none |
int b = 1; | 0 | 1 | none | none |
int c = 3; | 0 | 1 | 3 | none |
int d = b * c + 3; | 0 | 1 | 3 | 6 |
a = 2 * c + b; | 7 | 1 | 3 | 6 |
b = b + 1; | 7 | 2 | 3 | 6 |
d = d % b; | 7 | 2 | 3 | 0 |
Here is the completed table:
Statement | e | f | g |
---|---|---|---|
int e = 5; | 5 | none | none |
int f = 10; | 5 | 10 | none |
int g = f; | 5 | 10 | 10 |
e = e + 1; | 6 | 10 | 10 |
f = f - 1; | 6 | 9 | 10 |
g = g + e; | 6 | 9 | 16 |
g = g / e; | 6 | 9 | 2 |
Here is the completed table:
Statement | i | j | k |
---|---|---|---|
int i = 4; | 4 | none | none |
int j = 2; | 4 | 2 | none |
int k = 2; | 4 | 2 | 2 |
i = i + 1; | 5 | 2 | 2 |
i = i * j * k; | 20 | 2 | 2 |
j = i / j / k; | 20 | 5 | 2 |
k = i % (k + 1); | 20 | 5 | 2 |
a: 4, b: 7.333333333333333, c: 82.5, d: 6.9, e: 7.8 or 7.800000000000001, f: 5.0, g: 4, h: 3.0 ↩
The compiler presents us with the error possible loss of
precision, required: int, found: double
, which means that
we’re trying to assign a more precise type (double
) to a
variable with a less precise type (int
). The int
type
is less precise in the sense that it cannot store digits
to the right of the decimal point. The way to fix this is
to change the type of the pi
variable to double
. ↩
Last updated on June 26, 2025.