Thursday, April 26, 2012

Challenge 054

ClassCastException occurs when you attempt to cast an object of one data type to another type. Also, calling an object to a subclass of which it is not an instance.


(A) ((SavingsAccount) b).addInterest(); Does not match because b refers to BankAccount not SavingsAccount

(B) ((CheckingAccount) b).withdraw(200); Does not match because b refers to BankAccount not CheckingAccount

(C) ((CheckingAccount) c).deposit(800); Is the only one of the five statements that works because c refers to CheckingAccount and the statement is calling for a deposit in CheckingAccount

(D) ((CheckingAccount) s).withdraw(150); Does not match because s refers to SavingsAccount not CheckingAccount

(E) ((SavingsAccount) c).addInterest(); Does not match because c refers to CheckingAccount not SavingsAccount


(Does not match means it does not match with the subclass)

Nick Armold helped me with this challenge

Thursday, March 22, 2012

challenge 052

The first statement is true due to high-order inheritance. SmartPlayer implements the Player interface because HumanPlayer implements the interface which has SmartPlayer as a subclass, therefore being implemented with HumanPlayer due to inheritance.

The second statement is also true because HumanPlayer implements the interface Player. Player contains the methods int getMove() and void updateDisplay() and you stated that HumanPlayer implements the Player interface, so it also inherits the two methods within the class.

The third statement is false. You are able to declare a reference of type Player because HumanPlayer and SmartPlayer share the Player interface. To reference variables, the types need to share a superclass or interface, which they do (Player interface). Player can therefore be referenced because Player also refers to HumanPlayer and SmartPlayer and is in the Player interface.
(I used the following website to help me understand more about referencing:
http://stackoverflow.com/questions/4207732/how-to-declare-object-reference-variables-and-assign-them-values-according-to-us)

The fourth statement is true because in inheritance, a method that is declared can be overridden by any class that extends the primary class. SmartPlayer inherits the Player interface and its components and is able to alter/override any method.

The fifth statement is true because any interface that has the capability of being referenced can hold a parameter. Since HumanPlayer and SmartPlayer inherit the Player interface, it can take parameters for the HumanPlayer and SmartPlayer method.

Wednesday, March 21, 2012

Challenge 039

Statement I is incorrect because the super class that will be inherited does not contain any parameters. You know this because super(); contains nothing within the parenthesis (no parameters). However, the Hen class has the parameter(String species).

Statement II is the only correct statement because it contains super which allows the class to be inherited, and it contains the parameter inside the inherited class which is a string value, "species".

Statement III is incorrect because mySpecies = species is simply the Bird class' constructor which is a private instance variable, therefore making it impossible to reach.

Challenge 048

I       s1 == s2              false
II      s1.equals(s2)       true
III     s3.equals(s2)       true

Statement I is false because == is an operator that means the two objects have equality to one another. However, the objects are not the same (s1 and s2 are two separate unequal objects).

Statement II is true because equals refers to the values of the two objects being equal. This is the case because they both have the String value "crab, they are simply stated in two different ways

Statement III is true because s3(= s1) does equal s2(new String("crab"). The values are the same (String "crab"). However if it were stated: s3 == s2, it would have been false because s3 and s2 are two different objects.


The following website explains the == operator:
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

Challenge 049


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);
    }
    //Nick Armold helped me with the Math.PI
    //area for a circle = (pi)r^2)
    public double perimeter()
    {
        return (2 * Math.PI * radius);
    }
    //formula for perimeter is 2x(pi)x(r)
}

public class Square extends Shape
{
    private double length;
   
    public Square(String name, double s)
   
    {
        super(name);
        length = s;
    }
   
    public double area();
    {
        return (side * side);
    }
    //formula for area
    public double perimeter();
    {
        return (4 * side);
    }
    //formula for perimeter
}  
//Nick Armold helped me with this challenge

Wednesday, March 14, 2012

Challenge 045: Whaddup Gridworld???


import info.gridworld.actor.Actor;
import info.gridworld.actor.Rock;
import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;

public class RollingStone extends Rock
{
    public RollingStone()
    {
        super();
        setDirection(90);
    }
   
    public void act ()
    {
        if (canMove())
            move();
        else
            turn();
    }
   
    public void turn()
    {
        setDirection(getDirection() + Location.HALF_CIRCLE);
    }
   
    public void move ()
    {
        Grid<Actor> gr = getGrid();
       
        if (gr == null)
            return;
       
        Location loc = getLocation();
        Location next = loc.getAdjacentLocation(getDirection());
       
        Actor neighbor = gr.get(next);
       
        if (neighbor != null)
            neighbor.removeSelfFromGrid();
       
        if (gr.isValid(next))
            moveTo(next);
           
            else
                removeSelfFromGrid();
    }
   
    public boolean canMove()
    {
        Grid<Actor> gg = getGrid();
       
        if(gg == null)
            return false;
           
        Location last = getLocation();
        Location next = last.getAdjacentLocation(getDirection());
       
        Actor neighbor = gg.get(next);
            return true;
       
    }
}      

Monday, March 5, 2012

Challenge 044: Variety is the Spice of Life


for loop:

int sum = 0;
for (int index = 0; index < arr.length; index++)

{
    sum += arr[index];
}


for-each loop:

int sum =0;
for (int index:arr)

{
    sum += index;
}

I also referenced the following link:

http://forums.devarticles.com/java-development-38/java-changing-while-loop-to-for-loop-169122.html