Created
June 16, 2017 21:32
-
-
Save Nifled/48167630612435333b9d5ac7ecdd8bb0 to your computer and use it in GitHub Desktop.
Shape interface + implementations
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 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))); | |
} | |
} |
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 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(); | |
} | |
} |
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 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); | |
} | |
} |
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 interface Shape { | |
public void perimeter(); | |
public void area(); | |
} |
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 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