Advertisement
mmayoub

School, 19.09.2017, Point and Triangle

Sep 19th, 2017
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.71 KB | None | 0 0
  1. Point class
  2. =============
  3.  
  4. public class Point {
  5.     private String name;
  6.     private double x;
  7.     private double y;
  8.  
  9.     public Point(String name, double x, double y) {
  10.         this.name = name;
  11.         this.x = x;
  12.         this.y = y;
  13.     }
  14.  
  15.     public Point(Point p) {
  16.         this.name = p.name;
  17.         this.x = p.x;
  18.         this.y = p.y;
  19.     }
  20.  
  21.     public String getName() {
  22.         return name;
  23.     }
  24.  
  25.     public void setName(String name) {
  26.         this.name = name;
  27.     }
  28.  
  29.     public double getX() {
  30.         return x;
  31.     }
  32.  
  33.     public void setX(double x) {
  34.         this.x = x;
  35.     }
  36.  
  37.     public double getY() {
  38.         return y;
  39.     }
  40.  
  41.     public void setY(double y) {
  42.         this.y = y;
  43.     }
  44.  
  45.     public double distanceTo(Point p) {
  46.         double dx = this.x - p.x;
  47.         double dy = this.y - p.y;
  48.  
  49.         double d = Math.sqrt(dx * dx + dy * dy);
  50.  
  51.         return d;
  52.     }
  53.  
  54.     @Override
  55.     public String toString() {
  56.         return "Point [name=" + name + ", x=" + x + ", y=" + y + "]";
  57.     }
  58. }
  59.  
  60.  
  61. Triangle class
  62. ===============
  63. import java.util.Arrays;
  64.  
  65. public class Triangle {
  66.     private Point[] points;
  67.  
  68.     public Triangle(Point[] points) {
  69.         this.points = points;
  70.     }
  71.  
  72.     public Triangle(Point p1, Point p2, Point p3) {
  73.         this.points = new Point[3];
  74.         this.points[0] = new Point(p1);
  75.         this.points[1] = new Point(p2);
  76.         this.points[2] = new Point(p3);
  77.     }
  78.  
  79.     public Point[] getPoints() {
  80.         return points;
  81.     }
  82.  
  83.     public Point getPoint(int i) {
  84.         // i is 1 or 2 or 3
  85.         return this.points[i - 1];
  86.     }
  87.  
  88.     public void setPoint(int i, Point point) {
  89.         // i is 1 or 2 or 3
  90.         this.points[i - 1] = new Point(point);
  91.     }
  92.  
  93.     public double getPerimeter() {
  94.         // not a good way
  95.         double l1 = points[0].distanceTo(points[1]);
  96.         double l2 = points[1].distanceTo(points[2]);
  97.         double l3 = points[2].distanceTo(points[0]);
  98.  
  99.         return l1 + l2 + l3;
  100.     }
  101.  
  102.     public double getArea() {
  103.         double l1 = points[0].distanceTo(points[1]);
  104.         double l2 = points[1].distanceTo(points[2]);
  105.         double l3 = points[2].distanceTo(points[0]);
  106.  
  107.         double s = (l1 + l2 + l3) / 2;
  108.  
  109.         return Math.sqrt(s * (s - l1) * (s - l2) * (s - l3));
  110.     }
  111.  
  112.     @Override
  113.     public String toString() {
  114.         return "Triangle [points=" + Arrays.toString(points) + "]";
  115.     }
  116.  
  117. }
  118.  
  119. Tester class
  120. ==============
  121.  
  122. public class Tester {
  123.  
  124.     public static void main(String[] args) {
  125.         Point p1 = new Point("Aa", 0, 3);
  126.         Point p2 = new Point("Ax", 0, 0);
  127.         Point p3 = new Point("Ag", 4, 0);
  128.  
  129.         Triangle t1 = new Triangle(p1, p2, p3);
  130.  
  131.         System.out.println(t1);
  132.         System.out.println(t1.getPerimeter());
  133.         System.out.println(t1.getArea());
  134.  
  135.         t1.setPoint(1, new Point("D", 3, 6));
  136.  
  137.         t1.getPoint(2).setX(4);
  138.         t1.getPoint(2).setY(8);
  139.  
  140.         t1.setPoint(3, new Point("E", 5, 15));
  141.         System.out.println(t1);
  142.         System.out.println(t1.getPerimeter());
  143.         System.out.println(t1.getArea());
  144.     }
  145.  
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement