Skip to content

Instantly share code, notes, and snippets.

@Ellpeck Ellpeck/Circle.java Secret
Created Nov 26, 2019

Embed
What would you like to do?
public class Circle extends Shape {
public double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double calcArea() {
return Math.PI * this.radius * this.radius;
}
@Override
public double calcCircumference() {
return 2 * Math.PI * this.radius;
}
}
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Shape> shapes = new ArrayList<>();
shapes.add(new Rectangle(10, 5.25));
shapes.add(new Circle(10));
shapes.add(new Circle(11.153));
shapes.add(new RATriangle(10, 10));
shapes.add(new RATriangle(3.5, 7.5));
double totalArea = 0;
double totalCirc = 0;
for (int i = 0; i < shapes.size(); i++) {
Shape shape = shapes.get(i);
totalArea += shape.calcArea();
totalCirc += shape.calcCircumference();
}
System.out.println("Average area: " + totalArea / shapes.size());
System.out.println("Average circ: " + totalCirc / shapes.size());
}
}
public class RATriangle extends Shape {
public double a;
public double b;
public RATriangle(double a, double b) {
this.a = a;
this.b = b;
}
@Override
public double calcArea() {
return (this.a * this.b) / 2;
}
@Override
public double calcCircumference() {
return this.a + this.b + Math.sqrt(this.a * this.a + this.b * this.b);
}
}
public class Rectangle extends Shape {
public double width;
public double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
@Override
public double calcArea() {
return this.width * this.height;
}
@Override
public double calcCircumference() {
return 2 * (this.width + this.height);
}
}
public class Shape {
public double calcArea() {
return 0;
}
public double calcCircumference() {
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.