Skip to content

Instantly share code, notes, and snippets.

@Nifled
Created June 16, 2017 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nifled/48167630612435333b9d5ac7ecdd8bb0 to your computer and use it in GitHub Desktop.
Save Nifled/48167630612435333b9d5ac7ecdd8bb0 to your computer and use it in GitHub Desktop.
Shape interface + implementations
public class Circle implements Shape {
private int radius;
public Circle(int r) {
this.radius = r;
}
@Override
public void perimeter() {
System.out.println("Area = " + (2 * Math.PI * radius));
}
@Override
public void area() {
System.out.println("Area = " + (Math.PI * Math.pow(radius, 2)));
}
}
public class Main {
public static void main(String[] args) {
Shape square = new Square(2);
square.area();
Shape rec = new Rectangle(2, 4);
rec.area();
Circle cir = new Circle(5);
cir.area();
}
}
public class Rectangle implements Shape {
private int base, height;
public Rectangle(int h, int b) {
this.base = b;
this.height = h;
}
@Override
public void perimeter() {
double calculatePer = Math.pow(base, 2) + Math.pow(height, 2);
System.out.println("Perimeter = " + calculatePer);
}
@Override
public void area() {
double calculateArea = base * height;
System.out.println("Area = " + calculateArea);
}
}
public interface Shape {
public void perimeter();
public void area();
}
public class Square implements Shape {
private int length;
public Square(int len) {
this.length = len;
}
@Override
public void perimeter() {
System.out.println("perimeter");
}
@Override
public void area() {
double calculatedArea = Math.pow(length, 2);
System.out.println("Area = " + calculatedArea);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment