Created
January 30, 2017 16:27
-
-
Save 13andrew13/2124cca0e24adec13b7fc4f248e3db40 to your computer and use it in GitHub Desktop.
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
public class Line { | |
private final Point start; | |
private final Point end; | |
public Line(Point start,Point end){ | |
this.start = start; | |
this.end = end; | |
} | |
public double getLength(){ | |
return length(); | |
} | |
private double length(){ | |
double length = Math.sqrt((Math.pow(end.getX()-start.getX(),2)+Math.pow(end.getY()-start.getY(),2))); | |
return length; | |
} | |
@Override | |
public String toString() { | |
return "Line startpoint[" + start.toString()+"] endpoint [ "+ end.toString()+"] length is "+ length(); | |
} | |
} |
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
public class Lines { | |
private ArrayList<Line> lines= new ArrayList<Line>(); | |
public void add(Line line){ | |
lines.add(line); | |
} | |
private double sumLength(){ | |
double sum = 0; | |
for(Line line:lines) | |
sum += line.getLength(); | |
return sum; | |
} | |
private Line longestLine(){ | |
Line line = lines.get(0); | |
double max = 0; | |
for(Line l: lines){ | |
if(l.getLength()>max) { | |
max = l.getLength(); | |
line = l; | |
} | |
} | |
return line; | |
} | |
public void printLines(){ | |
for(Line line: lines){ | |
System.out.println(line.toString()); | |
} | |
} | |
public String longestLinetoString() { | |
return "The longest line is "+ longestLine().toString(); | |
} | |
public String lengthString(){ | |
return "Lines length is " + sumLength(); | |
} | |
} |
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
public class LinesRunner { | |
public static void main(String[] args) { | |
Point point1 = new Point(2,3); | |
Point point2 = new Point(3,5); | |
Point point3 = new Point(5,3); | |
Point point4 = new Point(3,1); | |
Line line1 = new Line(point1,point2); | |
Line line2 = new Line(point3,point4); | |
Lines lines = new Lines(); | |
lines.add(line1); | |
lines.add(line2); | |
lines.longestLinetoString(); | |
System.out.println(line1.toString() ); //print a line | |
System.out.println(lines.lengthString()); //print the sum of lengths of lines | |
System.out.println(lines.longestLinetoString()); //print the longest line | |
lines.printLines(); | |
} | |
} |
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
public class Point { | |
private final double x; | |
private final double y; | |
public Point(double x , double y){ | |
this.x = x; | |
this.y = y; | |
} | |
public double getX() { | |
return x; | |
} | |
public double getY() { | |
return y; | |
} | |
@Override | |
public String toString() { | |
return "x = "+ x+ " y ="+y; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment