Nino: Ch 6 (or Chapter 4 in new edition) problems

Java
These are problems out of the Nino text (text no longer used but plenty of examples to affirm understanding of logic).
If you are looking at the old archive of the course and are using the Nino book, you should bring your text the day we look at these.
Look at pages 165-167.

If you are using the new text (Lewis and Loftus), you should still try these problems, but also Ch.5 of your text, #5.1-5.6, 5.26, 5.28, 5.30-5.32, 5.34.


  1. Given that x = 4, y = 6, and z = 8, evaluate each of the following boolean expressions.

  2. Given that i and j are int variables. Simplify each of the following: helpful

  3. For which of the values of int variable i is the expression true? Compute the negation of this expression, and check against the negation of your original answer. help

  4. Given that x, y, and z are int variables write expressions to accomplish the following:

      a. Determine if x is greater than y
      b. Determine if x is greater than both y and x
      c. Determine if x is greater than either y or z
      d. Determine if x is equal to the product of y and z
      e. Determine if x ends in 0: i.e. if x is divisible by 10
      f. Determine if x is even
      g. Determine if x is between y and z, inclusive, where y is known to be less than z

  5. See page 143 - care with using operators == and != with floating points. Issue: floating points are approximations. In mathematics, we can actually prove that .999999... = 1.0
    Here, I will show you. Let x = 0.999...
      10x = 9.999...
         x = 0.999... subtract and you get
      9x = 9
         x = 1
    Things like this can be nasty when working with computers.
    You may want to look at the class
    java.lang.Math for things like how we round numbers. Note that the two round methods return an int and a long. How would you get it to return, say two decimal places after rounding? Note that we have seen formatting but could you do it mathematically?

  6. Suppose that x and max are int variables and done a boolean variable.

      a. Write a statement that will increment x by 1 only if done is false
      b. Write a statement that will increment x by 1 only if x is less than max

  7. Given that x, y, and z are int variables write statements to accomplish the following:

      a. Assign to z the larger of x and y
      b. Assign to x the absolute value of x
      c. Assign to z the largest of x, y and z

  8. Suppose that value, max, and min are int variables. The following code fragment should set value to min if it is smaller than min, and min to max if it is larger than max. It is not correctly written. Add braces to make the code correct.
      if (value >= min) if (value > max) value = max; else value = min;

  9. Assume that x is an int variable. For each of the following code fragments, state which initial values of x will result in x being incremented by 4.
    a.
      if (x ==1) x = x + 1; else if (x == 2) x = x + 2; else if (x ==3) x = x + 3; else x = x + 4;
    b.
      if (x ==1) x = x + 1; if (x == 2) x = x + 2; if (x ==3) x = x + 3; if (x ==4) x = x + 4;

  10. Consider the class Student specified in Ch. 4. Assume that fees are assessed as follows:

      0-3 credit hours:                      $500
      4-6 credit hours:                      $750
      7-9 credit hours:                      $1000
      10 or more credit hours:           $1250

    Based on this, write a definition of the method fees.

    solutions for the above problems
    Solutions for Lewis and Loftus #5.1 (MinOfThree), 5.2-5.6, 5.26, 5.28, 5.30-5.32, 5.34.