Goals for this problem set:
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  Example  Result 

int 
integers (up to 2^{31}  1)  3 + 4 * 5 
23 
double 
real numbers (up to 10^{308})  3.0 / 2.0 + 4.1 
5.6 
String 
text characters  "hi" + (1 + 1) + "u" 
"hi2u" 
Write the results of each of the following expressions. If you're stuck, ask a TA or neighbor.
12 / 5 + 8 / 4

4 

3 * 4 + 15 / 2

19 

(1 + 2 * 3 + (1 + 2) * 3)

16 

42 % 5 + 16 % 3

3 

2.5 * 2 + 17 / 4

9.0 

4.5 / 3 / 2 + 1

1.75 
Write the results of each of the following expressions.
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 

50 / 9 / 2.0 + 200 / 10 / (5.0 / 2)

10.5 
continued on the next slide...
In this exercise, you'll use the Interactions Pane to quickly discover the result of some expressions that would be difficult to evaluate by hand. Copy/paste each expression below into the Interactions Pane to evaluate it, then input the answer into this slide.
123 * 456  789

55299 

3.14 + 1.59 * 2.65

7.3535 

2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2

1024 

2 + 2 + "xyz" + 3 + 3

"4xyz33" 
(For the last expression, the Interactions Pane doesn't put ""
quotes around Strings when displaying results, so you must add those yourself if needed. For example, if the Interactions Pane gives you a result of abc123
, it should be written here as "abc123"
.)
Recall that you can use a variable to store the results of an expression in memory and use them later in the program.
type name; // declare name = value or expression; // assign a value ... type name = value or expression; // declareandinitialize together
Examples:
double iphonePrice; iPhonePrice = 299.95; int siblings = 3; System.out.println("I have " + siblings + " brothers/sisters.");
Which of the following choices is the correct syntax for declaring a real
number variable named grade
and initializing its value
to 4.0
?
Suppose you have a variable named grade
, set
to 1.6
:
double grade = 1.6; // uhoh
Suppose later in the program's code, we want to change the value
of grade
to 4.0
. Which is the correct syntax to
do this?
Suppose you have a variable named balance
, set
to 463.23
:
double balance = 463.23
Suppose later in the program's code, we want to add 5 to the account balance. Which is a correct statement to do this?
a
,
b
, and c
What are the values of a
, b
, and c
after the following statements? Write your answers in the boxes on the
right.
int a = 5; int b = 10; int c = b; a = a + 1; // a? 6 b = b  1; // b? 9 c = c + a; // c? 16
1 2 3 4 5 6 7 8 9 10 11 12 13 
public class Oops { public static void main(String[] args) { int x; System.out.println("x is" x); int x = 15.2; // set x to 15.2 System.out.println("x is now + x"); int y; // set y to 1 more than x y = int x + 1; System.out.println("x and y are " + x + and + y); } } 
answer on next slide...
+
between "x is"
and x
x
before assigning it a
value
15.2
into a variable of
type int
"
mark should be between now
and +
int
here
y
should be same type as x
y
to be 1 more
than x
(should not write the word int
here)
and
should be in quotes with surrounding spaces
public class Oops { public static void main(String[] args) { double x = 0.0; System.out.println("x is" + x); x = 15.2; // set x to 15.2 System.out.println("x is now " + x); double y; // set y to 1 more than x y = x + 1; System.out.println("x and y are " + x + " and " + y); } }
i
,
j
, and k
What are the values of i
, j
, and k
after the following statements?
int i = 2; int j = 3; int k = 4; int x = i + j + k; i = x  i  j; // i? 4 j = x  j  k; // j? 2 k = x  i  k; // k? 1
Suppose you have a real number variable x
. Write a Java
expression that computes a variable named y
storing the
following value:
y = 12.3x^{4}  9.1x^{3} + 19.3x^{2}  4.6x + 34.2
(We haven't learned a way to do exponents yet, but you can simulate them using several multiplications.)
Use the example program on the next slide to test your code.
Copy/paste this program into jGRASP to test your solution.
// expected output: // y is 7043.7 public class EquationY { public static void main(String[] args) { double x = 5; double y = put your expression for y here ; System.out.println("y is " + y); } }
(answer on next slide)
double y = 12.3*x*x*x*x  9.1*x*x*x + 19.3*x*x  4.6*x + 34.2;
If you want an added challenge, try to come up with a way to compute the
above value while using the *
operator no more than 4 times.
(click Next → for answer)
double y = (((12.3 * x  9.1) * x + 19.3) * x  4.6) * x + 34.2;
Bday
that
declares four variables and assigns appropriate values to them.
My birthday is 9/19, and Suzy's is 6/14.