School of Engineering and Applied Science
Department of Computer Science
CSci 53 -- Introduction to Software Development
http://www.seas.gwu.edu/~csci53/fall04
Prof. Michael B. Feldman
mfeldman@gwu.edu

Lab Exercise #3
for lab meeting Tuesday, Sept. 21, 2004

Objectives: This exercise will help you explore integer and float arithmetic, and operator precedence. It is based on Exercises 2.6 and 2.7 (p. 120-121) in the textbook.

Part I. Integer and float arithmetic.

Given these variable declarations:

int iResult, num1 = 25, num2 = 40, num3 = 17, num4 = 5;
double fResult, val1 = 17.0, val2 = 12.78;

For each of these assignment statements, what will the result be in iResult or fResult? First compute the results manually and fill in the "predicted results" column of the table. Then, use the program programs53/Arithmetic.java as a way to test your predictions and fill in the "actual results" column. Use the fourth column for any comments you may have. Make sure you understand why each actual result is what it is!
 
Assignment Statement Predicted Result Actual Result Explanatory Comments 
iResult = num1 / num4;      
fResult = num1 / num4;      
iResult = num3 / num4;      
fResult = num3 / num4;      
fResult = val1 / num4;      
fResult = val1 / val2;      
iResult = num1 / num2;      
fResult = (double) num1 / num2;      
fResult = num1 / (double) num2;      
fResult = (double) (num1 / num2);      
iResult = (int) (val1 / num4);      
fResult = (int) (val1 / num4);      
fResult = (int) ((double) num1 / num2);      
iResult = num3 % num4;      
iResult = num 2 % num3;      
iResult = num3 % num2;      
iResult = num2 % num4;      

 

Part II: Operator precedence.

Now do the same with these assignment statements, given the declarations:

int iResult, a = 10, b = 8, c = 6, d = 5, e =2;

and the program programs53/Precedence.java.
Assignment Statement Predicted Result Actual Result Explanatory Comments
iResult = a - b - c - d;      
iResult = a - b + c - d;      
iResult = a + b / c / d;      
iResult = a + b / c * d;      
iResult = a / b * c * d;      
iResult = a % b / c * d;      
iResult = a % b % c % d;      
iResult = a - (b - c) - d;      
iResult = (a - (b - c)) - d;      
iResult = a - ((b - c) - d);      
iResult = a % (b % c) * d * e;      
iResult = a + (b - c) * d - e;      
iResult = (a + b) * c + d * e;      
iResult = (a + b) * (c / d) % e;      

(end of lab)