JMU
Lab: Gaining Experience with Arithmetic Operators


Instructions: Answer as many of the following questions as you can during the lab period. If you are unable to complete the assignment during the lab period it is strongly recommended that you complete it on your own.

1. One Set of Symbols: Given the following operators (in decreasing order of precedence):
TIMES Multiplication
DIVIDED_BY Division
DIVIDED_BY_WITHOUT_REMAINDER Integer Division
REMAINDER_AFTER_DIVISION Remainder
PLUS Addition
MINUS Subtraction
ASSIGNED Assignment

what is the value of x after each of the following statements is evaluated:

  1. x ASSIGNED 5


  2. x ASSIGNED 5 PLUS 2


  3. x ASSIGNED 5 DIVIDED_BY 2


  4. x ASSIGNED 5 DIVIDED_BY_WITHOUT_REMAINDER 2


  5. x ASSIGNED 5 REMAINDER_AFTER_DIVISION 2


  6. x ASSIGNED 5 MINUS 2 TIMES 2


2. A Second Set of Symbols: Given the following operators (in decreasing order of precedence):
* Multiplication
/ Division
DIV Integer Division
MOD Remainder
+ Addition
- Subtraction
<- Assignment

what is the value of x after each of the following statements is evaluated:

  1. x <- 16


  2. x <- 16 + 4


  3. x <- 16 / 4


  4. x <- 16 DIV 4


  5. x <- 16 MOD 4


  6. x <- 16 - 4 * 4


3. Unary and Binary Operators: Given the following unary operators (in decreasing order of precedence):
+ Positive
- Negative
++ Increment (i.e., increase the operand by 1)
-- Decrement (i.e., decrease the operand by 1)

and the following binary operators (also in decreasing order of precedence, all of which are of lower precedence than the unary operators):

* Multiplication
/ Division (integer or real depending on operands)
% Remainder
+ Addition
- Subtraction
= Assignment

what is the value of x after each of the following statements is evaluated:

  1. x = 5 - 2 * 2


  2. x = 3 + 8 * 3 - 5 * 2


  3. x = 3 + 8 * 3 - 5 % 2


  4. x = - 16 + 4 * 2


  5. x = 16 % ++ 4


  6. x = 16 % 4 + 1


  7. Make each of the statements in this section clearer by adding parentheses to them.


4. Expressions Involving Division (in Java): Given the operators used in Java, what will each of the following expressions evaluate to?
  1. 7 / 3


  2. 7 / 2


  3. 7. / 2.


  4. 7. / 2


  5. 7 / 2.


5. Programming Patterns: This part of the lab will give you some experience with programming patterns involving arithemtic operators. You should carefully test your answers that involve code to ensure that they are correct.
  1. Suppose today is a Monday. What day of the week will it be in 53 days?


  2. Letting the days of the week be denoted by the integers 0 through 6 (starting with Sunday), what integer corresponds to Monday?


  3. Continuing with the system from the previous questions, write an expression for determining the integer that corresponds to the day of the week in 53 days.


  4. Letting the int variable today denote the integer corresponding to today and letting the int variable days denote the number of days in the future we are interested in, and the int constant DAYS_PER_WEEK denote the numer of days in a week, genealize your answer to the previous question so that it will work for any day of the week and number of days.


  5. Write an expression that evaluates to the two left-most digits of the number 5792. (Note: You must not use subtraction and your answer must use the number 5792.) (Example of Use: Suppose you are given a four digit number. You might want to determine the century.)


  6. Write an expression that evaluates to the right-most digit of the number 978. (Note: You must not use subtraction and your answer must use the number 978.)


  7. Suppose you weighed 135lbs,10oz before Thanksgiving and gained 11oz. What do you weigh now?


  8. Given three non-negative int variables named pounds, ounces, and changeInOunces that contain your current weight in pounds and ounces and your change of weight in ounces, write two Java statements that can be used to calculate your weight in pounds and ounces after the change.


  9. Test your answer to the previous question with the data from the question about Thanksgiving.
Note: This part of the lab allows you to "go further" on this material. It is neither required nor for extra credit. It will, however, help you gain a deeper understanding of the material.
6. Postfix Notation: Given the following binary operators (all of which have the same precedence):
* Multiplication
/ Division (integer or real depending on operands)
% Remainder
+ Addition
- Subtraction
= Assignment

evaluate each of the following expressions written using postfix notation (assuming left-to-right associativity):

  1. 5 2 - 2 *


  2. 3 8 + 3 * 5 - 2 *


  3. 3 8 + 3 * 5 - 2 %


  4. Re-write each of the expressions in this section using infix notation. Put parentheses around each subexpression.


Copyright 2013