Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TestAll {
- public static void main(String args[]) {
- Circle c1 = new Circle(); {
- radius = 5;
- }
- Point p1 = new Point(); {
- width = 5;
- }
- Point p2 = new Point(); {
- width = 5;
- height = 7;
- }
- Square s1 = new Square(p1);
- Rectangle r1 = new Rectangle(p2);
- System.out.println("Details of c1:");
- System.out.println("radius: " + c1.radius());
- System.out.println("area: " + c1.area());
- System.out.println("perimeter: " + c1.perimeter());
- System.out.println("Details of s1: ");
- System.out.println("side length: " + s1.width());
- System.out.println("area: " + s1.area());
- System.out.println("perimeter: " + s1.perimeter());
- System.out.println("Details of r1: ");
- System.out.println("width: " + r1.width());
- System.out.println("height: " + r1.height());
- System.out.println("area: " + r1.area());
- System.out.println("perimeter: " + r1.perimeter());
- }
- }}
- 1./////////////////////////////////////////////////////////////////////
- public class Point {
- private int width;
- private int height;
- public Point(int theWidth, int theHeight) {
- width = theWidth;
- height = theHeight;
- }
- public int getWidth() {
- return width;
- }
- public int getHeight() {
- return height;
- }
- public void setHeight(int theHeight) {
- height = theHeight;
- }
- public void setWidth(int theWidth) {
- width = theWidth;
- }
- }
- 2.///////////////////////////////////////
- public interface FigureGeometry
- {
- final float PI = 3.14f;
- float getArea ();
- float getPerimeter ();
- }
- 3.//////////////////////////////////////////////
- public class Circle implements FigureGeometry {
- private float radius;
- public Circle(float theRadius) {
- radius = theRadius;
- }
- public float getRadius ()
- {
- return radius;
- }
- public float getArea ()
- {
- return getRadius() * getRadius() * PI;
- }
- public float getPerimeter ()
- {
- return getRadius() * 2 * PI;
- }
- public void setRadius ( float theRadius )
- {
- radius = theRadius;
- }
- }
- 4.////////////////////////////////////////////////////////
- public class Square implements FigureGeometry {
- private Point point;
- public Square(Point p) {
- point = p;
- }
- public int getSideLength() {
- return point.getWidth();
- }
- public float getArea() {
- return getSideLength() * getSideLength();
- }
- public float getPerimeter() {
- return getSideLength() * 4;
- }
- public void setPoint(Point p) {
- point = p;
- }
- }
- 5.////////////////////////////////////////////////
- public class Rectangle {
- private Point point;
- public Rectangle (Point p) {
- point = p;
- }
- public int getWidth() {
- return point.getWidth();
- }
- public int getHeight() {
- return point.getHeight();
- }
- public float getArea() {
- return getWidth() * getHeight();
- }
- public float getPerimeter() {
- return ( getWidth() + getHeight() ) * 2;
- }
- public void setPoint(Point p) {
- point = p;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement