CSCI A201/A597

Takehome Quiz Five

Hints

Second Summer 2000


Fifth set of questions due Wednesday July 12, on paper. Answers will be posted Thursday at noon.

Directions: Print this page and write your answers in the space provided in pencil, by hand. Do not edit this file, do not use more space than this page printed with a reasonable font size is providing. Be concise but precise. Let me know if you need help.

Questions:

  1. Find the errors in the following if statements.
    if quarters > 0 then System.out.println(quarters + "quarters"); 
    There's more than one mistake above.

    Here and in all cases below you just check the syntax.

    if (1 + x > Math.pow(x, Math.sqrt(2)) y = y + x;
    Sometimes the error is a bit more general.
    if (x = 1) y++; else if (x = 2) y = y + 2; 
    This is tricky especially for newcomers to Java.
    if (x && y == 0) p = new Point2D.Double(x, y); 
    
    if (1 <= x <= 10)
    { System.out.println("Enter y:");
      y = console.readDouble();
    }
    These two both exhibit roughly the same problem.
    if (s != "nickels" || s != "pennies"
       || s != "dimes" || s != "quarters")
      System.out.print("Input error!"); 
    You got to be careful with strings and reference types...
    if (input.equalsIgnoreCase("N") || "NO") 
      return; 
    You've seen this above already.
    int x = console.readDouble();
    if (x != null) y = y + x;
    Numbers are primitive types.
    language = "English"; 
    if (country.equals("USA"))
      if (state.equals("PR")) language = "Spanish"; 
    else if (country.equals("China"))
      language = "Chinese"; 
    Draw yourself a diagram to see what happens.

  2. Explain the following terms and give an example for each construct:

    I am sure you will find these quite easy. Just remember: be brief.

  3. Explain the difference between an if/else  /else statement and nested if statements. Give an example for each.

    Since this space is so small I guess a reasonable thing to do is to give good references where this is treated in the book (pages numbers).

  4. Give an example for an if/else /else statement where the order of the tests does not matter. Give an example where the order of the tests matter.

    The examples need not be very long. Is there anything like that in the book?

    
    
    
    

  5. Of the following pairs of strings, which comes first in lexicographic order?

    Lexicographic order is discussed in lecture notes of yesterday and in the book.

    "Tom", "Dick"
    "Tom", "Tomato"
    "church", "Churchill"
    "car manufacturer", "carburetor"
    "Harry" hairy"
    "C++", "Car"
    "Tom", "Tom"
    "Car", "Carl"
    "car", "bar"

  6. Complete the following truth table by finding the truth values of the Boolean expressions for all combinations of the Boolean inputs p, q, and r.

    Just think of all possible cases for the three independent variables, then apply the rules of AND, OR, and NOT (with care so that you don't make mistakes).

    p q r (p && q) || !r !(p && (q || !r))
    false false false ? ?
    false false true ? ?
    false true false ? ?
    ? ? ? ? ?
    ? ? ? ? ?
    ? ? ? ? ?
    ? ? ? ? ?
    ? ? ? ? ?

  7. True or false: A && B is the same as B && A for any Boolean conditions A and B?

    This should be easy, but if it isn't dra the truth table.

  8. Explain the difference between
    s = 0;
    if (x > 0) s++;
    if (y > 0) s++;
    and
    s = 0;
    if (x > 0) s++;
    else if (y > 0) s++;

    Try a few cases for x and y and look at the value of s at the end.

  9. Use De Morgan's law to simplify the following Boolean expressions.

    DeMorgan's laws are on page 212. When you can, eliminate double negation.

    !(x > 0 && y > 0)
    
    
    !(x != 0 || y != 0)
    
    
    !(country.equals("USA") && !state.equals("HI") && !state.equals("AK"))
    
    
    !(x % 4 != 0 || !(x % 100 == 0 && x % 400 == 0))
    
    
    

  10. Make up another Java code example that shows the dangling-else problem, using the following statement. A student with a GPA of at least 1.5, but less than 2, is on probabtion. With less than 1.5, the student is failing.

    Take a look on page 205 for a model.

    
    
    
    
    
    

  11. Explain the difference between the == operator and the equals method when comparing strings.

    Be brief.

  12. Explain the difference between the tests
    r == s
    and
    r.equals(s)
    where both r and s are of type Rectangle.

    Take advantage of the space on the right.

  13. What is wrong with this test to see whether r is null? What happens when this code runs?
    Rectangle r;
    ...
    if (r.equals(null))
      r = new Rectangle(5, 10, 20, 30);

    This should be easy now.

  14. Write Java code to test whether two objects of type Line2D.Double represent the same line when displayed on the graphics screen. Do not use a.equals(b).
    Line2D.Double a;
    Line2D.Double b;
    
    if (your condition goes here)
      g2.drawString("They look the same!", x, y);
    Hint: If p and q are points, then Line2D.Double(p, q) and Line2D.Double(q, p) look the same.

    Not much space. What are the accessors of the Line2D.Double objects? I'd use those that return Points and check to see if Points have a good equal instance method.

  15. Explain why it is more difficult to compare floating-point numbers than integers. Write Java code to test whether an integer n equals 10 and whether a floating-point number x equals 10.

    It must have something to do with the difference between a whole number and a number with a fractional part.

    
    
    
    

  16. Give an example for two floating-point numbers x and y such that Math.abs(x - y) is larger than 1000, but x and y are still identical except for a roundoff error.

    Both the book and the notes mention this. The notes say you cover this case by normalization.

    
    
    

  17. Consider the following test to see whether a point falls inside a rectangle.
    Point2D.Double p = ...
    boolean xInside = false;
    if (x1 <= p.getX() && p.getY() <= x2) 
      xInside = true;
    boolean yInside = false; 
    if (y1 <= p.getY() && p.getY() <= y2)
      yInside = true; 
    if (xInside && yInside) 
      g2.drawString("p is inside the rectangle.", x1, y1);
    Rewrite this code to eliminate the explicit true and false values, by setting xInside and yInside to the values of Boolean expressions.

    Once you do that the code becomes much shorter. Just remember that a boolean variable is meant to store the value of a boolean expression just like a number variable can store the value of an arithmetic expression.

    
    
    
    
    
    
    
    


Last updated: July 7, 2000 by Adrian German for A201