Last active
August 29, 2015 14:25
-
-
Save OsandaMalith/583d711867cef53e86cb to your computer and use it in GitHub Desktop.
Calculate Perimeter, Area and checks for square based on X and Y cordinates
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
package SRectangle; | |
// CC-BY : Osanda Malith Jayathissa | |
public class Point { | |
private int x, y; | |
public Point(int x, int y) { | |
this.x=(x); | |
this.y=(y); | |
} | |
public int getX() { | |
return x; | |
} | |
public void setX(int x) { | |
this.x = x; | |
} | |
public int getY() { | |
return y; | |
} | |
public void setY(int y) { | |
this.y = y; | |
} | |
} |
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
package SRectangle; | |
// CC-BY : Osanda Malith Jayathissa | |
public class Rectangle { | |
private Point tl; | |
private Point tr; | |
private Point bl; | |
private Point br; | |
public Rectangle(Point tl, Point tr, Point bl, Point br) { | |
if( tl.getX() < 20.0 && tl.getY() < 20.0 | |
&& tr.getY() < 20.0 && tr.getY() < 20.0 | |
&& bl.getY() < 20.0 && bl.getY() < 20.0 | |
&& br.getY() < 20.0 && br.getY() < 20.0) { | |
this.tl = (tl); | |
this.tr = (tr); | |
this.bl = (bl); | |
this.br = (br); | |
} else System.err.println("Value should be below 20.0"); | |
if( tl.getX() != bl.getX() ) System.err.println("error: Top left and bottom left must have same X values"); | |
if( tr.getX() != br.getX() ) System.err.println("error: Top right and bottom right must have same X values"); | |
} | |
public Point getTopLeft() { return tl; } | |
public Point getTopRight() { return tr; } | |
public Point getBottomLeft() { return bl; } | |
public Point getBottomRight() { return br; } | |
public double getWidth() { | |
Point tr = getTopRight(); | |
Point tl = getTopLeft(); | |
return tr.getX() - tl.getX(); | |
} | |
public double getHeight (){ | |
Point tr = getTopRight(); | |
Point br = getBottomRight(); | |
return tr.getY() - br.getY(); | |
} | |
public double getPerimeter() { return 2*(getHeight() + getWidth()); } | |
public double getArea() { return getHeight() * getWidth(); } | |
public boolean isSquare() { return getHeight() == getWidth(); } | |
} |
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
package SRectangle; | |
// CC-BY : Osanda Malith Jayathissa | |
public class Tester { | |
public static void main(String[] args) { | |
Point tr = new Point(1,3); Point tl = new Point (3,3); | |
Point br = new Point (1,1); Point bl = new Point (3,1); | |
Rectangle rect = new Rectangle (tr,tl,br,bl); | |
System.out.println("Height: " + rect.getHeight()); | |
System.out.println("Width: " + rect.getWidth()); | |
System.out.println("Area: " + rect.getArea()); | |
System.out.println("Perimeter: " + rect.getPerimeter()); | |
System.out.println("Is square?: "+ rect.isSquare()); | |
tr = new Point(1,7); tl = new Point (3,7); | |
br = new Point (1,1); bl = new Point(3,1); | |
rect = new Rectangle (tr,tl,br,bl); | |
System.out.println("\n\n"); | |
System.out.println("Height: " + rect.getHeight()); | |
System.out.println("Width: " + rect.getWidth()); | |
System.out.println("Area: " + rect.getArea()); | |
System.out.println("Perimeter: " + rect.getPerimeter()); | |
System.out.println("Is square?: "+ (rect.isSquare() ? "yes":"no")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment