CS100J       Lab 01. Expressions, variables, declarations, and assignments       Spring 2007 

Name ________________________________ Cornell net id _____________
(e.g. Gries's Cornell email address is djg17@cornell.edu, and his net id is djg17.)

Section time _______________        Section instructor ___________________

This lab deals with expressions and their evaluation in Java. Below are a list of expressions, some followed by questions. Type each expression into DrJava, hit the enter key to have it evaluated, record its value after the expression on this paper, and answer any question to the best of you ability. Do not simply write down what you think is the value of an expression; write down only what DrJava says is its value.

Rather than type an expression character for character, open this handout (from the course webpage) in a browser, copy an expression from the browser page, and paste it into the DrJava Interactions pane. That will save you time and prevent you from making typing mistakes. However, from time to time, it may make sense when in the Interactions pane to hit the uparrow key to obtain a previous expression, edit it, and hit the return key to have the modified expression evaluated.

The last part of this lab assignment concerns variables, declarations, and assignment statements, so you will be typing in declarations and assignments as well.

When you are finished with this assignment , show this sheet to your lab instructor, who will record that you did it. If you do not finish this complete assignment, finish it within the next few days and show this sheet to your lab instructor the next time you see them.

If you finish this assignment early, don't leave. Instead, do one of several things: (1) Find out how to use the course newsgroup —look on the course webpage for a link to information about it. (2) Experiment with DrJava. If there is a topic that you feel you don't fully understand, then type in some expressions that deal with that topic and gain the understanding. (3) Turn to the CD ProgramLive and listen to a lecture on type String, or Character. Also, look in the course text or ProgramLive for the answers.

Don't waste time! If there is something you don't understand, ask your lab instructor or a consultant immediately! For example, you should understand HOW each expression is evaluated, and if the answer doesn't make sense to you, ask someone immediately.

The lab instructors and consultants are in the lab to help. They will look over your shoulder every once in a while and give you advice on what you are doing.

int expressions

 
5 + 2  
5 + 2 * 5 (5 + 2) * 5
4 - 3 - 3 4 - (3 - 3)
-4 - -4 - -4  
6 / 2 6 / 3
6 / 4 Why isn't 6/4 = 1.5?
7 % 2 8 % 3
6 % 3 What is the name of operator %?
Integer.MIN_VALUE Integer.MIN_VALUE - 1
Why does Integer.MIN_VALUE - 1 have such a funny value?  
Integer.MIN_VALUE Integer.MIN_VALUE + 1
Integer.MAX_VALUE Integer.MAX_VALUE - 1
Integer.MAX_VALUE + 1  
   
double expressions  
5.0 + 2.0 1 + 1.99
(5 + 2.1) * 5  
4.0 - 3 - 3 4.0 - (3 - 3)
-4.0 - -4 - -4  
6.0 / 2 6.0 / 4
6.0 % 3 6 % 4
-6.0 % 3 -6.0 % 4
Double.MIN_VALUE Double.MIN_VALUE - 1
Double.MAX_VALUE Double.MAX_VALUE + 1
Double.MAX_VALUE + Double.MAX_VALUE  
   
casting  
(double) 4 (int) 4
(double) 7 / 4 (double) (7 / 4)
Which operator has higher precedence, casting or division?  
(int) 5.3 (double) (int) 5.3
(int) (int) 5.3 (double) (double) 4
(int) 5.3 (int) - 5.3
5 + 7 / 4 (double) 5 + 7 / 4
5 + 7 / (double) 4  
   
boolean expressions  
true true && false
true || false What is the name of operator && ?
false true && true
true || true What is the name of operator || ?
!true What is the name of operator ! ?
!false !!false
true && false && true true || false || true
true || (false && true) true && (true || false)
3 < 5 3 < 5 && 5 < 3
0 <= 4 && 4 < 5  
false && (5 / 0 == 1)    why does this work? (5 / 0 == 1) && false        why doesn't the work?
   
String expressions  
"Truth " + "is " + "best" ("Truth " + "is ") + "best"
"Truth " + ("is " + "best") 56 + "" + 56
"" + 4 / 2 ("" + 4) / 2 —Gives an error message. Why?
"" + (4 / 2) What does + do if at least one operand is String?
4 + 2 + "" 4 + (2 + "")
   
Function calls  
Math.min(25, 4) In the function call on the left, what are the two constants 25 and 4 called?
Math.max(25, 4) Math.min(25, Math.max(27, 4))

Math.abs(25)

Math.abs(- 25)
Math.ceil(25.6) Math.floor(25.6)
Math.ceil(- 25.6) Math.floor(- 25.6)
Math.abs(Math.min(-25, -4))  
   
Variables, declarations, and assignment statements  
int j; (There will be no answer from the declaration to the left)
j Does a newly declared variable have a value?
j= 2; (To the left is your first assignment statement)
j  
j+4  
j= j + 9;  
j (You can see what assigning to j did)
The following shows you that in the interactions pane, you don't have to declare a variable before using it. But in a Java program, you have to.  
k= 5; j + k
w= j + k; w
w; (if you follow an expression with a semicolon, you don't see its value)

When done, show this to your lab instructor. Then, (1) find out how to use the course newsgroup, (2) experiment with DrJava, or (3) read something on the ProgramLive CD.