CS10, 09S, UCSB

H10: (Based on Horstman, Chapters 7, 8) Total points: ? (printable PDF)

Accepted: on paper, in lecture (1-1:50pm) on Wednesday, May 6th only.
No email submission allowed.

Name: (2 pts)______________________________   UCSBNetID (2 pts)  _____________________

Section (6 pts) Circle one:         Thu 4pm                 Fri 10am                       Fri 11am                  Fri noon


  1. Read Section 8.2 on cohesion and coupling, then answer this question:

    Suppose we have a Complex class that has this constructor:

    public Complex (double real, double imag);
    and these method (only):

    double getReal();
    double getImag();
    Then, suppose we have a QuadraticEqn class that has this constructor:

    public QuadraticEqn(double a, double b, double c);  
    and these methods:

    public void setA(double a);
    public void setB(double b);
    public void setC(double c);
    public double getA();
    public double getB();
    public double getC();
    public int numRealRoots();
    public double realRoot1(); // result of applying + from quadratic formula
    public double realRoot2(); // result of applying - from quadratic formula
    public Complex complexRoot1(); // result of applying + from quadratic formula
    public Complex complexRoot2(); // result of applying - from quadratic formula
    1. (5 pts) Does Complex "depend on" Quadratic?             yes       no

    2. (5 pts) Does QuadraticEqn "depend on" Complex?      yes        no

    3. (5 pts) The Complex class given above is:           mutable      immutable

    4. (5 pts) The QuadraticEqn class given above is:        mutable      immutable


    Please turn over for more problems

  2. (5 pts) Read the section "Common Error 8.1" on p. 343. Then answer: what is the output of invoking the main method of this class?

    public class WhatIsTheOutput1
    {
    public void setToZero(int x)
    {
    x = 0;
    }

    public void tryIt()
    {
    int y=3;
    setToZero(y);
    System.out.println("y=" + y);
    }

    /**
    * main method
    *
    * @param args arguments to main method (ignored in this case)
    */
    public static void main (String [] args)
    {
    WhatIsTheOutput1 wito = new WhatIsTheOutput1();
    wito.tryIt();
    }
    }
  3. Look up the Integer wrapper class in the Java API (you can use this link:
    http://java.sun.com/javase/6/docs/api/java/lang/Integer.html—remember that you can look at this assignment on the website, so you don't have to type that URL in.)

    1. (5 pts) Is the Integer wrapper class (circle one):         mutable       or      immutable?



    2. (5 pts) What are you looking for when deciding whether a class is mutable or immutable?



  4. (5 pts) Read "Advanced Topic 8.1" on p. 346, and then answer the question below. Keep your answer to the previous question in mind, and also how autoboxing (Chapter 7) and references work (Section 2.10). This question is trickier than it may seem.

    What is the output of the main method of this class?

    public class WhatIsTheOutput2
    {
    public void setToZero(Integer x)
    {
    x = 0;
    }

    public void tryIt()
    {
    Integer y=3;
    setToZero(y);
    System.out.println("y=" + y);
    }

    public static void main (String [] args)
    {
    WhatIsTheOutput2 wito = new WhatIsTheOutput2();
    wito.tryIt();
    }
    }

End of H10