CSE 131 / 501N (Summer 2014)

Recitation Questions

Do the following exercises using only the material we have studied in Module 1. In particular, you may not make use of the if statement or the ternary operator.

Some of the questions are a bit tricky. The idea is to stretch some on these problems, so that you will find the quiz and exam questions easier to tackle. The quiz and associated exam problems will resemble the questions you see here, so work to master these and you should have no problem on the quiz or exam.

Where there are multiple exercises within a problem, choose one or two and leave the rest for homeowork on your own.

  1. In studio you wrote a program that determined if 3 integers were ordered. Write a program (remember, without using if) that prints true if four integers (n1, n2, n3, n4) are ordered from lowest to highest. You may assume there are no duplicate values.

  2. Suppose that elapsed is an integer representing the number of seconds that have passed since some important event took place in the past 24 hours. Write a program that takes the value of elapsed and expresses the results in hours and seconds, using a format of your own choosing.

  3. Suppose that an int variable named cost maintains the cost of an item in pennies. Using no data type other than int or String, write code that prints out the value of cost in the normal dollars and cents notation.

    For example, if cost has the value 12345, then your program should print $123.45.

  4. We know that in an expression without parentheses, multiplication is performed before addition. Write a Java program that proves this is the case, so that from running your program, you know that multiplication does take precedence over addition.

  5. It turns out that when you use the logical-or operator ||, the second expression is not evaluated if the first one is true. Thus, in the expression (thing1 || thing2), if thing1 is true, then the value of thing2 is not even considered.

  6. An expression tree is shown below.
    1. Write the Java expression for the tree, using parentheses only where necessary.
    2. When the expression is evaluated, what is the resulting value?
    3. What is the type of the resulting value?

  7. For each of the following expressions
    1. 3 * 4 + 7
    2. 3 + 4 * 7
    3. 3.0 + 4 / 7
    4. 1 + 2 + 3 + 1 * 2 * 3
    5. (50 - 5 * 4) / 10 - 7

  8. Determine the result of the following String expressions:
    1. "this " + "string " + "has " + "spaces "
    2. "this" + "string" + "has" + "no" + "spaces"
    3. "the sum of " + 6 + " + " + 10 + " is " + (6 + 10)
    4. "the sum of " + "6 + 10" + " is " + (6 + 10)
    5. 6 + 10 + " equals " + (6 + 10)
    6. "" + 6 + "+" + 10 + " equals " + 6 + 10

  9. Determine the result of the following boolean expressions:
    1. 3 > 7
    2. (3 > 7) || (4 < 9)
    3. 135 == 100 + 35
    4. (true && false) || (true || false)
    5. (3 > 7) || ((10 < 9) == (3 == 8))
    6. false || !(3 > 7)

  10. For each expression below, determine the type of the resulting value. If the expression would cause an error, explain why. Otherwise determine the value of the expression.
    1. 7 / 2
    2. 7.0 / 2
    3. ((7 / 2) == (7.0 / 2))
    4. "x = " + 6
    5. 6 + " = x"
    6. ((6 * 3) / 80) / (35 - 7 * 5)
    7. 1 / 2

  11. For each row in the table below, place an X in the int or double column that is the more appropriate type to represent the specified value.
    Value int? double?
    Area of a circle    
    Number of robots you own    
    Your graduation year    
    Your height in meters    
    Your weight in micrograms    
    Number of facebook friends    
    Average age of your facebook friends    
  12. 1.2.25 on page 42 of the text
  13. 1.2.30 on page 43 of the text