Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Test {
- public static void main(String[] args){
- Point[] lineFractures = new Point[] {
- new Point (1.0, 2.0),
- new Point (5.0, 2.1),
- new Point (1.0,10.0),
- new Point (6.0, 1.0)};
- Line l1 = new Line (lineFractures);
- System.out.println("l1 length: " + l1.showLength());
- System.out.println(l1);
- }
- }
- ////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////
- public class Point {
- private double x, y;
- public Point (double x, double y){
- this.x = x;
- this.y = y;
- }
- public double getX() { return x; }
- public double getY() { return y; }
- public String toString() {
- return "(" + x + ", " + y + ")";
- }
- }
- ///////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////
- public class Line {
- private Point[] lineFractures;
- public Line (Point[] lineFractures){
- this.lineFractures = new Point[lineFractures.length];
- for (int i = 0; i < lineFractures.length; i++){
- this.lineFractures[i] = lineFractures[i];
- }
- }
- public double showLength (){
- double length = 0;
- for (int i = 1; i < lineFractures.length; i++){
- double tempX = Math.pow(lineFractures[i].getX() - lineFractures[i - 1].getX(), 2);
- double tempY = Math.pow(lineFractures[i].getY() - lineFractures[i - 1].getY(), 2);
- length += Math.sqrt(tempX + tempY);
- }
- return length;
- }
- public String toString(){
- String points = "";
- for (int i = 0; i < lineFractures.length; i++){
- points += "x: " + lineFractures[i].getX() + "; y: " + lineFractures[i].getY() + "\n";
- }
- return "Line Fractures: " + points;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement