Thursday, January 26, 2012

Challenge 040’s Solution

Of the three, only statements 1 and 3 are valid. This is because of how the classes are written. The Dancer class contains the leap() method. The BalletDancer class is an extension of the Dancer class; thus, the BalletDancer class can be used to create Dancer objects and perform Dancer methods. However, the Dancer class cannot be used to create BalletDancer objects or perform BalletDancer-specific methods, like the glide() method. Object b is from the BalletDancer class—it can perform both the leap() method and the glide() method—so statement 1 is valid. However, object d is from the Dancer class—it can perform the leap() method, but not the glide() method—so statement 3 is valid, while statement 2 is invalid.

−Phoenix

Wednesday, January 25, 2012

Challenge 039’s Solution

Of the three, only option II will correctly implement code for the constructor of the Hen class. First, the Bird class is the superclass of Hen; thus, a Hen object must inherit the parameters of a Bird object. To do this, one must use the statement “super()” as the first statement in the Hen constructor. This immediately eliminates option III, which lacks the super() statement. Next, the super() statement looks in the Bird class for a constructor that takes the parameters listed within its parenthesis. Option I simply states “super();”, so Java™ will look in the Bird class for a no-args constructor. However, Bird doesn’t contain a no-args constructor, so option I is eliminated. On the other hand, option II states “super(species);”, so Java™ will look in the Bird class for a constructor containing the parameter species. The constructor Bird contains this; thus, only option II correctly implements code for the constructor of the Hen class.

−Phoenix

PS: Line 9 of the Owl code mistakenly says “Systme” instead of “System”.
PPS: Line 8 of the Hen code is missing a “"”.

Challenge 038’s Solution

To answer the Free Response Question #1, one can write the following code:
    /**
     * Postcondition: Prints all notes in noteList, one per line.
     *                Notes are numbered 1, 2, ...
     *                Each number is followed by a period and a space.
     */
    public void printNotes()
    {
        for(int n = 0; n <= noteList.size() - 1; n++)
        {
            System.out.println(n + 1 + ". " + noteList.get(n).toString());
        }
    }

To answer the Free Response Question #2, one can (again) write the following code:
public class PartTimeEmployee extends Employee
{
    private double workFraction;
    private boolean isUnionMember;
    public PartTimeEmployee(String name, String city, boolean isCitizen, double salary, double work, boolean isMember)
    {
        super(name, city, isCitizen, salary);
        workFraction = work;
        if(salary > 15000)
        {
            isMember = false;
        }
        else
        {
            isMember = true;
        }
    }
    public double getWork()
    {
        return workFraction;
    }
    public boolean getMemberStatus()
    {
        return isUnionMember;
    }
}

−Phoenix

Friday, January 6, 2012

Challenge 037’s Solution

To complete Challenge 37, the following method must be added to the Robot class:

    public String toString()
    {
        if (hasRemoteDisableFeature)
        {
            return "• Memory: " + memoryCapacity + " GB \n• Model Number: " + model + "\n• Can be Remotely Disabled \n \n2011 (ͻ) copyleft " + make + ", Inc.";
        }
        else
        {
            return "• Memory: " + memoryCapacity + " GB \n• Model Number: " + model + "\n• Can’t be Remotely Disabled \n \n2011 (ͻ) copyleft " + make + ", Inc.";
        }
    }

−Phoenix

Thursday, January 5, 2012

Challenge 036’s Solution

When Cindy’s statement is entered into the code, BlueJ™ will not compile, stating it “cannot find symbol - constructor Robot()”. This is because the no-args constructor Robot() does not exist. While the code does contain a public method named “Robot”, it requires parameters for the variables mem, mk, mod, and feature. If Cindy were to enter in parameters for each of these variables—requiring one int, two String, and one boolean value, respectively—there would be no error. Cindy could also place “public Robot() {}” into the code. However, this would not create the desired robot; rather, it would simply prevent an error statement.
 
−Phoenix