Skip to content

Instantly share code, notes, and snippets.

@axsddlr
Last active May 19, 2018 03:30
Show Gist options
  • Save axsddlr/6acf42b5244927836929be290b08d996 to your computer and use it in GitHub Desktop.
Save axsddlr/6acf42b5244927836929be290b08d996 to your computer and use it in GitHub Desktop.
Project 2 - Revised created by Ayysir - https://repl.it/@Ayysir/Project-2-Revised
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 calculateArea() {
return pi * Math.pow(radius, 2);
}
}
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 calculateArea() {
return width * length;
}
}
public interface Shape {
public double calculateArea();
}
public class TestShape {
public static void main(String[] args) {
// Rectangle test
double width = 5, length = 7;
Shape rectangle = new Rectangle(width, length);
System.out.println("Rectangle width: " + width + " and length: " + length
+ "\nArea: " + rectangle.area() + "\n");
// Circle test
double radius = 5;
Shape circle = new Circle(radius);
System.out.println("Circle radius: " + radius
+ "\nArea: " + circle.area() + "\n");
// Triangle test
double a = 5, b = 3, c = 4;
Shape triangle = new Triangle(a,b,c);
System.out.println("Triangle sides lengths: " + a + ", " + b + ", " + c
+ "\nArea: " + triangle.area() + "\n");
}
}
public class Triangle extends Shape {
private final double a, b, c;
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 calculateArea() {
double s = (a + b + c) / 2;
return Math.sqrt(s * (s - a) * (s - b) * (s - c));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment