-
-
Save Ellpeck/8cf63c747313e070b7475d99e2bed5a1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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