Advertisement
And1

Laba1

Feb 17th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.91 KB | None | 0 0
  1. public class Test {
  2.     public static void main(String[] args){
  3.         Point[] lineFractures = new Point[] {
  4.                 new Point (1.0, 2.0),
  5.                 new Point (5.0, 2.1),
  6.                 new Point (1.0,10.0),
  7.                 new Point (6.0, 1.0)};
  8.         Line l1 = new Line (lineFractures);
  9.         System.out.println("l1 length: " + l1.showLength());
  10.         System.out.println(l1);
  11.     }
  12. }
  13.  
  14. ////////////////////////////////////////////////////////
  15. ////////////////////////////////////////////////////////
  16.  
  17. public class Point {
  18.     private double x, y;
  19.  
  20.     public Point (double x, double y){
  21.         this.x = x;
  22.         this.y = y;
  23.     }
  24.  
  25.     public double getX() { return x; }
  26.     public double getY() { return y; }
  27.  
  28.     public String toString() {
  29.         return "(" + x + ", " + y + ")";
  30.     }
  31. }
  32.  
  33. ///////////////////////////////////////////////////////////
  34. ///////////////////////////////////////////////////////////
  35.  
  36. public class Line {
  37.     private Point[] lineFractures;
  38.     public Line (Point[] lineFractures){
  39.         this.lineFractures = new Point[lineFractures.length];
  40.         for (int i = 0; i < lineFractures.length; i++){
  41.             this.lineFractures[i] = lineFractures[i];
  42.         }
  43.     }
  44.     public double showLength (){
  45.         double length = 0;
  46.         for (int i = 1; i < lineFractures.length; i++){
  47.             double tempX = Math.pow(lineFractures[i].getX() - lineFractures[i - 1].getX(), 2);
  48.             double tempY = Math.pow(lineFractures[i].getY() - lineFractures[i - 1].getY(), 2);
  49.             length += Math.sqrt(tempX + tempY);
  50.         }
  51.         return length;
  52.     }
  53.  
  54.     public String toString(){
  55.         String points = "";
  56.         for (int i = 0; i < lineFractures.length; i++){
  57.             points += "x: " + lineFractures[i].getX() + "; y: " + lineFractures[i].getY() + "\n";
  58.         }
  59.         return "Line Fractures: " + points;
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement