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
Subscribe to:
Post Comments (Atom)
I'm not saying it's wrong, just asking a question, why is it necessary to have this method in your Square class?
ReplyDeletepublic double area();
{
return (side * side);
}
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.
ReplyDeleteAnd 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?
ReplyDeletesince 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.
ReplyDeleteI 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.
Hi Ryan,
ReplyDeleteThe 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?
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
ReplyDeleteCheck this out, specifically the 3rd bullet point item:
ReplyDeletehttp://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?
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.
ReplyDeleteBingo. 900 XPs awarded.
ReplyDelete