All three statements will successfully transfer money from one account to the other. Statement I removes $50 from the BankAccount object b and transfers it to the SavingsAccount object timsSavings, leaving b with $600 and s with $1,550. Statement II removes $30 from timsSavings and transfers it to the CheckingAccount object daynasChecking, leaving timsSavings with $1,470 and daynasChecking with $2,030. Finally, statement III removes $55 from daynasChecking and transfers it to b, leaving daynasChecking with $1,970 and b with $705.
−Phoenix
Friday, March 16, 2012
Challenge 054’s Solution
Of the five statements, only statement C avoids causing a ClassCastException. This is because of the casting present in each statement. The program first creates three objects: the BankAccount object b, the SavingsAccount object s, and the CheckingAccount object c. Statement A attempts to cast b into a SavingsAccount object, but they are not compatible, so statement A is false. Statement B attempts to cast b into a CheckingAccount object, but they are also not compatible, so statement B is false. Statement C attempts to recast c into a CheckingAccount object, then tells it to perform the deposit method. This is entirely valid, so statement C is true. Statement D attempts to cast s into a CheckingAccount object, but they are not compatible, so statement D is false. Finally, statement E attempts to cast c into a SavingsAccount object, but, again, they are not compatible, so statement E is false.
−Phoenix
−Phoenix
Tuesday, March 13, 2012
Challenge 052’s Solution
Of the five commentators, all five are correct. The first post, written by Dave Dee, claims that the interface class SmartPlayer automatically implements Player’s interface. SmartPlayer inherits all of HumanPlayer’s methods. These methods are implemented from Player, so the statement is true. The second post, written by Dozey, claims that HumanPlayer must implement all of Player’s methods to work. This is true: interfaces, when implemented, should have every method specifically defined within the class that inherits it. The third post, written by Beaky, claims that referencing an object of type Player is not possible. This is true, as interface classes do not have constructors; thus, they cannot be referenced as object types. The fourth post, written by Mick, claims that SmartPlayer can override the defined methods of HumanPlayer. This is true as well, as a subclass is able to override the methods of its superclass. Finally, the fifth post, written by Tich, claims that a client program method can require a parameter of type Player. While this can be required, the usability of the method is questionable, as Player constructors cannot successfully be formed. Thus, on account to technicality, Tich is correct.
−Phoenix
−Phoenix
Monday, March 12, 2012
Challenge 053’s Solution
To complete Challenge 53, the following code must be pasted into BlueJ™:
public class FlyingOstrich implements FlyingObject
{
public void fly()
{
System.out.println("The ostrich gets a running start.");
System.out.println("It starts to flap its wings.");
System.out.println("The ostrich picks up its speed…");
if (!isFlying())
{
System.out.println("…and nothing happens.");
}
else
{
System.out.println("…and runs onto an airplane.");
}
System.out.println("\nOSTRICHES CANNOT FLY!");
}
public boolean isFlying()
{
return false;
}
}
−Phoenix
public class FlyingOstrich implements FlyingObject
{
public void fly()
{
System.out.println("The ostrich gets a running start.");
System.out.println("It starts to flap its wings.");
System.out.println("The ostrich picks up its speed…");
if (!isFlying())
{
System.out.println("…and nothing happens.");
}
else
{
System.out.println("…and runs onto an airplane.");
}
System.out.println("\nOSTRICHES CANNOT FLY!");
}
public boolean isFlying()
{
return false;
}
}
−Phoenix
Friday, March 9, 2012
Challenge 051’s Solution
In Part I of Challenge 51, all four concepts can be seen in the code sample. First, method overriding is seen with method1. This method is originally defined in ClassA, then redefined in ClassB. ClassB will run its own version of method1 instead of the version inherited from ClassA, making method1 an example of method overriding. Next, method overloading is seen with methodB. ClassA has two methodBs: one lacking parameters, and one requiring an int value n. They both have the same name, but are two different methods, making methodB an example of method overloading. After that, information hiding is seen in the int variable var1. This variable has been declared private, so it cannot be directly referred to outside of ClassB. Because of its private status, var1 is an example of information hiding. Finally, procedural abstraction is seen with method3. Procedural abstraction is the use of multiple methods to perform a function. methodA is given an int value n from method3, performs a calculation, then returns the value back to method3, which makes method3 an example of procedural abstraction.
In Part II of Challenge 51, only statement III has a chance of working. To understand why, one first has to analyze the declarations of ob1 and ob2. ob1 is a no-args ClassA object created by the ClassA class, so it can perform all of ClassA’s methods. ob2, while declared as a ClassB object, is created by the ClassA class. This limits ob2 to performing only ClassA’s methods. Statement I has ob1 perform the method2 method. However, method2 can only be performed by ClassB objects, so statement I cannot be performed. Similarly, statement II has ob2 perform the method2 method. ob2 is, like ob1, a ClassA object, so method2 cannot be performed, making statement II false as well. Finally, statement III makes ob1 a ClassB object, then has it perform the method2 method. Because it converts ob1 before asking it to perform method2, statement III can be performed, making it true.
−Phoenix
In Part II of Challenge 51, only statement III has a chance of working. To understand why, one first has to analyze the declarations of ob1 and ob2. ob1 is a no-args ClassA object created by the ClassA class, so it can perform all of ClassA’s methods. ob2, while declared as a ClassB object, is created by the ClassA class. This limits ob2 to performing only ClassA’s methods. Statement I has ob1 perform the method2 method. However, method2 can only be performed by ClassB objects, so statement I cannot be performed. Similarly, statement II has ob2 perform the method2 method. ob2 is, like ob1, a ClassA object, so method2 cannot be performed, making statement II false as well. Finally, statement III makes ob1 a ClassB object, then has it perform the method2 method. Because it converts ob1 before asking it to perform method2, statement III can be performed, making it true.
−Phoenix
Thursday, March 8, 2012
Challenge 050’s Solution
Of the three, only statement II is true. Statement I states that Teacher will inherit Person’s constructors. However, subclasses do not inherit its superclasses’ constructors; thus, statement I is false. Statement II states that Teacher can create methods and instance variables other than those provided by Person. This is true, as subclasses can create its own methods and instance variables—be they public or private. Finally, statement III states that Teacher can override Person’s private constructors. However, these are private—they cannot be accessed outside of Person. Thus, statement III is false.
−Phoenix
−Phoenix
Thursday, March 1, 2012
Challenge 049’s Solution
To complete Challenge 49, the following codes must be placed into BlueJ™:
import java.lang.Math;
public class Circle extends Shape
{
private double radius;
public Circle(String name, double r)
{
super(name);
radius = r;
}
public double area()
{
return (Math.PI * radius * radius);
}
public double perimeter()
{
return (2 * radius * Math.PI);
}
}
public class Square extends Shape
{
private double length;
public Square(String name, double ℓ)
{
super(name);
length = ℓ;
}
public double area()
{
return (length * length);
}
public double perimeter()
{
return (length * 4);
}
}
−Phoenix
import java.lang.Math;
public class Circle extends Shape
{
private double radius;
public Circle(String name, double r)
{
super(name);
radius = r;
}
public double area()
{
return (Math.PI * radius * radius);
}
public double perimeter()
{
return (2 * radius * Math.PI);
}
}
public class Square extends Shape
{
private double length;
public Square(String name, double ℓ)
{
super(name);
length = ℓ;
}
public double area()
{
return (length * length);
}
public double perimeter()
{
return (length * 4);
}
}
−Phoenix
Challenge 048’s Solution
Of the three, only statements II and III are true. This is because of the difference between the == operator and the equals method. First, all three constructors successfully create String objects called s1, s2, and s3 that represent the string "crab". Next are the three statements. Statement I is true if “s1 == s2”. Both s1 and s2 equal the String value "crab"; however, the == operator doesn’t compare the values of s1 and s2, rather their addresses. Basically, it sees whether or not the two references are to the same object. s1 and s2 are two different objects; thus, the statement “s1 == s2” is false. Statement II is true if “s1.equals(s2)”. The equals method compares the values of s1 and s2. Both s1 and s2 equal the String value "crab"; thus, the statement “s1.equals(s2)” is true. Finally, statement III is true if “s3.equals(s2)”. Both s3 and s2 equal the String value "crab"; thus, the statement “s3.equals(s2)” is true.
−Phoenix
−Phoenix
Subscribe to:
Posts (Atom)