Classes Test

Read through the following code. Assume that fred and bob have been declared and have been assigned to Turtle objects.

Color color1, color2;
color1 = fred.getColor();
color2 = bob.getColor();

  1. What is the best description of what is happening in the second line?
    1. Setting the color of fred's pen to color1.
    2. Storing fred's pen color in color1.
    3. Asking color1 to set its pen to fred's color.
    4. Declaring a variable called color1.
    5. Creating a new color.

  2. What is the best description of what all this code does?
    1. Swap bob and fred's colors.
    2. Save bob and fred's colors.
    3. Turn bob and fred the same color.
    4. Create two new colors and give them to the two turtles.

  3. A class Rope has a method whose signature is:

    public double getLength()

    Suppose that, in Controller, you have a variable noose that refers to a Rope object. Which of the following are correct uses of the getLength() method? You may circle more than one line.
    1. double height = noose.getLength();
    2. noose.getLength(4.5);
    3. bob.move(noose.getLength());
    4. noose = this.getLength();
    5. None of the above - only objects of the Rope class are permitted to use getLength().

  4. Suppose that I have this code:

    // This makes a turtle with some unknown color
    Turtle joe = createSampleTurtle();
    Turtle bert = new Turtle();

    Write code that I can add here to give fred the same pen color that bob has. You may find it easier to store it in a variable first.

For the next four problems, refer to the the Code for Classes Test handout that you got earlier this week.
  1. Why is it that I need to put this. before rate in the constructor for AccountWithInterest, but I don't need it before rate in addInterest()?

  2. a) Frank starts out with $600 and Tom starts out with $1000. Then, Tom writes a check for $400 to Frank. Write code that will accomplish this (including creating Tom and Frank).

    b) How much does Frank now have in his savings account?

  3. AccountWithInterest ought to also override the deposit() and withdraw() methods so that the interest is computed before the balance is changed. Write below the code you would insert into AccountWithInterest to override deposit().

  4. Our Person objects are bound to run out of money in their checking accounts, because writing a check moves money from the checking of one person to the savings of another. The method transferToChecking() will prevent this: it tells a person to transfer some amount of money from their savings account to their checking. Fill in the body of the method.

    public void transferToChecking(double amount) {

  5. What would happen if I had used getBalance() instead of super.getBalance() in the addInterest() method in AccountWithInterest?

    Suppose that you want to define a class BusinessMan representing a person who does office work. Think through each of the following:
    1. What class would it extend?
    2. What instance variables would it have that its superclass wouldn't?
    3. What would its constructor look like?
    4. What new methods would it have that its superclass does not have?
    5. What methods of its superclass would it override?

    Below, write a definition of this class that includes just the class declaration, the declaration of instance variables, and the signatures, not the contents, of methods. You should have at least one constructor, one method that is new, and one method that overrides a method of the superclass. As usual, each method should have a short comment describing what it does. Indicate in the comment or elsewhere which method(s) are overriding existing ones.

    There are no "standard" classes that you are working with here; just use whatever names seem appropriate for that type of object. If your business man needs to reference a WaterCooler just go ahead and use the class name in your code without worrying about how that class would be defined in Java. Remember, this is just a design question, not really a coding one - you don't have to write any actual Java instructions, just declarations and signatures.