snippetjavaMajor
Implement a Shape abstract class
Viewed 0 times
abstractshapeimplementclass
Problem
To learn more about OOP, @nhgrif challenged me to implement a Shape abstract class (more details in code remarks below. Here is how I did it. Any and all advice appreciated!
Shape.java
Rectangle.java
Circle.java
Triangle.java
```
public class Triangle extends Shape {
private final double a, b, c; // sides
public Triangle() {
this(1,1,1);
}
public Triangle(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
@Override
public double area() {
// Heron's formula:
// A = SquareRoot(s (s - a) (s - b)
Shape.java
/* nhgrif says:
* Phrancis ready for inheritance/polymorphism?
* Given the following abstract class:
*
* public abstract class Shape {
* public abstract double area();
* public abstract double perimeter();
* }
*
* Implement a Circle, Triangle, and Rectangle class which extend the class Shape.
* Ex: public class Circle extends Shape ... etc
*/
public abstract class Shape {
public abstract double area();
public abstract double perimeter();
}Rectangle.java
public class Rectangle extends Shape {
private final double width, length; //sides
public Rectangle() {
this(1,1);
}
public Rectangle(double width, double length) {
this.width = width;
this.length = length;
}
@Override
public double area() {
// A = w * l
return width * length;
}
@Override
public double perimeter() {
// P = 2(w + l)
return 2 * (width + length);
}
}Circle.java
public class Circle extends Shape {
private final double radius;
final double pi = Math.PI;
public Circle() {
this(1);
}
public Circle(double radius) {
this.radius = radius;
}
@Override
public double area() {
// A = π r^2
return pi * Math.pow(radius, 2);
}
public double perimeter() {
// P = 2πr
return 2 * pi * radius;
}
}Triangle.java
```
public class Triangle extends Shape {
private final double a, b, c; // sides
public Triangle() {
this(1,1,1);
}
public Triangle(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
@Override
public double area() {
// Heron's formula:
// A = SquareRoot(s (s - a) (s - b)
Solution
I am generally impressed with the consistency of the implementations, neatness, etc.
I have a comment about the basic premise. The
In addition to that concern, the following are also things you should consider:
-
The
-
I would avoid the 'unit' default constructors. They don't help with anything. When would someone want to call
-
Your classes are missing a
-
You don't have a plan for out-of-bound dimensions. What will you do with negative, NaN, or infinite input?
I have a comment about the basic premise. The
Shape should not be an abstract class, but an interface. It has no concrete implementations for any methods, and making it an abstract class makes it hard to inherit from other places too. Consider this instead:public interface Shape {
public double area();
public double perimeter();
}In addition to that concern, the following are also things you should consider:
-
The
pi field on your Circle class should be private. There's no need to re-expose an already public constant in another way.-
I would avoid the 'unit' default constructors. They don't help with anything. When would someone want to call
new Triangle() and not want to have the dimensions?-
Your classes are missing a
toString() method. These are useful for many reasons, especially debugging.-
You don't have a plan for out-of-bound dimensions. What will you do with negative, NaN, or infinite input?
Code Snippets
public interface Shape {
public double area();
public double perimeter();
}Context
StackExchange Code Review Q#83769, answer score: 21
Revisions (0)
No revisions yet.