15-110 FALL 2009 (CORTINA)

HOMEWORK 3 - due Friday, September 18, 2009 in class

Please remember that homework must be handed in on time. No late homework will be accepted.

Review the suggested readings from chapter 5 of your textbook and answer the following questions based on your textbook and the course lecture material.

  1. (2 pts) Let the variable b be of type boolean. What value is assigned to b in each of these four assignment statements? Show your work for full credit.

    1. b = !(false && (true || false));

    2. b = (Math.pow(4.0, 3.0) > Math.pow(3.0, 4.0));

    3. String s = new String("AUTUMN");
      b = s.equals("FALL");

    4. b = ((b || !b) && (!b || b));

  2. (1 pt) Assume x and y are int variables. Consider the Java code fragment below:

    if (x < 0) { 
          if (x - y < 0) 
                System.out.println("B"); 
          else 
                System.out.println("C"); 
    }     
    else 
          System.out.println("A"); 
    

    1. Draw a flowchart to illustrate the flow of execution of the fragment.

    2. Draw a graph like the one shown below, shading in the area where "A" would be output in one color, the area where "B" would be output in another color, and the area where "C" would be output in a third color. If the border of an area is included in the shaded area, draw the border as a solid line. Otherwise, draw the border as a dashed line.

                       y ^
                         |
                         |
                         |
                         |            x
             ------------------------->
                         |
                         |
                         |
                         |
                         |
      

  3. (1.5 pts) A car dealership gives a discount to the customer if at least one of these conditions apply:

    1. Complete the following Java statement that prints out whether the customer gets a discount or not assuming age is an int variable, veteran is a boolean variable, and both variables have been initialized already.

      if ( (age ____ 25) ____ (veteran ____ true) ) 
          System.out.println("DISCOUNT APPLIES"); 
      else 
          System.out.println("NO DISCOUNT");
      

    2. Use DeMorgan's law to rewrite the if statement in part (a) without using the same logical operator you used in part (a).

    3. Rewrite the if statement in part (a) without using the keywords true or false.

  4. (1 pt) Let average be an int that is always in the range [0,100]. Complete the following Java code fragment so the correct letter grade is output based on the following rules: 90-100=A, 80-89=B, 70-79=C, 60-69=D, 0-59=R. Do not use unnecessary conditions in your answer.

    if (___________________________)  
        System.out.println("A"); 
    else if (_____________________________) 
        System.out.println("B"); 
    else if (_____________________________) 
        System.out.println("R"); 
    else if (_____________________________)  
        System.out.println("C"); 
    else  
        System.out.println("D"); 
    

  5. (1 pt) Suppose Lays E. Student writes the following Java code:

    if (x > 0)
    if (y > 0)
    z = 5;
    else
    z = 3;
    
    1. Which if statement does the else match up to? Indent the code correctly to show this.

    2. If we wanted the other if statement to match up with the single else, show how to modify the code to do this, indenting correctly. (Do not change the order of the statements or the conditions in the statements.)

  6. (1.5 pts) 5. Write a simple Java class named MusicSuggester with a main method that prompts the user to enter a musical genre. If the user enters "POP", print out the message "Kelly Clarkson". Or if the user enters "RAP", print out the message "Kanye West". Or if the user enters "COUNTRY", print out the message "Kenny Chesney". Otherwise, print out "No suggestion available.".

    Keep in mind that the user might not enter a genre using all uppercase letters. For example, if the user enters "Pop" or "pop", you should treat that as "POP". Otherwise, the genre must match exactly. For example, "MODERN COUNTRY" and "RAPPING" are not matches.

    Remember: You should try to write this by hand without using a computer to test your code. On a written exam, you will have to write Java code without the help of a computer.

  7. (2 pts) Using the Robot API given in class (or click HERE), write static methods for the following operations. Determine what return type is needed (think: does your method return any answer?).

    1. Write a method named randomMove that generates a random number between 1 and 100. If the number is even, turn right, otherwise back up one position. (To back up, the robot should end up in the position directly behind it facing in the same direction.)

    2. Write a method named fancyMove that determines if the robot is on a light square and can move forward, it moves forward one position. Otherwise, if the robot is on a light square and cannot move forward, it turns left. Otherwise, if the robot is on a dark square, it turns around.