CSE 142 Summer 2001

Homework #3

Due: At the beginning of class, Monday, July 9, 2001

 

Purpose

The purpose of this assignment is to gain some practice with conditional expressions, the if statement, and simple input and output.

Reading

You should look at the lecture slides on simple I/O and conditionals (from Monday, July 2).  Reading sections 7.1-7.3 of the textbook (Riley) is likely also likely to be helpful..

Instructions

Your solution to these problems should be written out on paper and handed in.  No online turnin is available.  You do not have to run your solutions on a computer, but you are encouraged to do so to check your answers.

  1. (Riley, p. 248 #1).  What is the value of emuCounter following the execution of each of the following instructions, assuming it has a value of 3 just prior to this execution.

    1. if (emuCounter <= 3) {
        emuCounter = 5;
      } else {
        emuCounter = 17;
      }
    2. if (emuCounter > 3) {
        emuCounter = 83;
      } else {
        emuCounter = 94;
      }
      
            
  2. (Riley #2) How do the answers to question 1 change if emuCounter has a value of 4 just before the if executes?

  3. (Riley #3 in part) Assume that each of the following is used as a boolean expression in a Java program.  Which of these expressions evaluate to true, which evaluate to false, and which contain syntax errors?

    1. 7 == 7
    2. 6 < 8 && 'a' < 'b'
    3. true != false || 7 > 7.5
    4. 0 < 3 < 7
    5. "z" == 'z'
  4. Write a sequence of Java statements that asks the user to enter the temperature, then prints "too hot" if the temperature is greater than 80, prints "nice' if the temperature is greater than 68 and no more than 80, prints "chilly" if the temperature is between 60 and 68, or prints "too cold" if the temperature is less than 60.

  5. (Kamin & Reingold)  The U.S. Centers for Disease Control determines obesity according to a "body mass index", computed by

        weight in kilograms / (height in meters)2

    An index of 27.8 or greater for men, or 27.3 or greater for nonpregnant women is considered obese.  Write a sequence of Java statements that prompts for height, weight, and sex, then reports whether a person with that height, weight, and sex is obese.  Additional notes if you're curious: one meter is 39.37 inches, one inch is 2.54 centimeters, one kilogram is 2.2 pounds, and one pound is 454 grams.