Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.30 KB | None | 0 0
  1. public class Main {
  2.     public static void main(String[] args) {
  3.         Point p = new Point(5, 6);
  4.         Point q = new Point(-1, -8);
  5.         LineSegment l = new LineSegment(p, q);
  6.         System.out.println(l);
  7.         System.out.println("The midpoint of ths line segment is: " + l.midpoint());
  8.         System.out.println("The length of this lin segment is: " + l.lengthOfSegment());
  9.         System.out.println("The slope of this line segment is: " + l.slope());
  10.     }
  11. }
  12.  
  13. class Point {
  14.     double x;
  15.     double y;
  16.  
  17.     public Point(double x, double y) {
  18.         this.x = x;
  19.         this.y = y;
  20.     }
  21.  
  22.     double distanceFromOrigin() {
  23.         return Math.sqrt(x * x + y * y);
  24.     }
  25.  
  26.     public String toString() {
  27.         return "(" + x + ", " + y + ")";
  28.     }
  29.  
  30. }
  31.  
  32. class LineSegment {
  33.     Point a;
  34.     Point b;
  35.  
  36.     public LineSegment(Point a, Point b) {
  37.         this.a = a;
  38.         this.b = b;
  39.     }
  40.  
  41.     public String toString() {
  42.         return "LIne segment from " + a + " to " + b;
  43.     }
  44.  
  45.     double lengthOfSegment() {
  46.         return Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2));
  47.     }
  48.  
  49.     Point midpoint() {
  50.         return new Point((a.x + b.x) / 2, (a.y + b.y) / 2);
  51.     }
  52.  
  53.     double slope() {
  54.         return (a.y - b.y) / (a.x - b.x);
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement