Wednesday, March 21, 2012

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

9 comments:

  1. I'm not saying it's wrong, just asking a question, why is it necessary to have this method in your Square class?

    public double area();
    {
    return (side * side);
    }

    ReplyDelete
  2. when this method is implemented, it returns a value that is equivalent to the area of the square. It is the formula for the area of a square.

    ReplyDelete
  3. And if you left it out, what would break? Obviously, we'd lose the ability to calculate the area, but what else would then be wrong?

    ReplyDelete
  4. since abstract class Shape is inherited by the square class, it also inherits the methods. Since the Shape class has the method public abstract double area();, the method needs to have constructors in the square class, and if it is taken out, the abstract double area method will be useless without constructors.

    I think this is what you are asking, I can explain my code but I dont completely understand what would happen if I took out that segment because I cant compile it in the first place. I cannot reference the square class.

    ReplyDelete
  5. Hi Ryan,

    The root of misunderstanding shows here,

    "Since the Shape class has the method public abstract double area();, the method needs to have constructors in the square class,"

    A method does not have constructors. A class can have a constructor method, or multiple constructor methods.

    You do rightly identify that the Shape class is abstract, and it does have a method called area().

    My question is centered around the idea of abstract methods declared in an abstract class. What happens if we create a class that inherits from an abstract class, but our class does not contained one of the abstract methods defined in the abstract super class?

    ReplyDelete
  6. if it is not contained, in the abstract super class, it cannot fulfill the method because it does not exist as far as the super class sees it

    ReplyDelete
  7. Check this out, specifically the 3rd bullet point item:

    http://bit.ly/HgLBTi

    Use what you find there to answer:

    My question is centered around the idea of abstract methods declared in an abstract class. What happens if we create a class that inherits from an abstract class, but our class does not contained one of the abstract methods defined in the abstract super class?

    ReplyDelete
  8. since shape is an abstract class, it does not contain an actual body of code that can be inherited from. Square is a subclass of Shape and its method of area is also inherited. So, if it does not contain one of the abstract methods defined in the abstract super class, the subclass (Square) cannot be instantiated because it does not override each of the abstract methods.

    ReplyDelete