Skip to content

Instantly share code, notes, and snippets.

@13andrew13
Created January 26, 2017 22:42
Show Gist options
  • Save 13andrew13/17e5465d348dd15400625cc03495a45e to your computer and use it in GitHub Desktop.
Save 13andrew13/17e5465d348dd15400625cc03495a45e to your computer and use it in GitHub Desktop.
Создать классы: 1. Point (Immutable): double x, double y. Methods: getters . 2. Line (Immutable): Point start, Point end. Methods: double getLength( ). 3. Lines: ArrayList<Line> lines. Methods: void add(Line line), double sumLength( ), Line longestLine( ).
public class Line {
private final Point start;
private final Point end;
public Line(Point start,Point end){
this.start = new Point(start.getX(),start.getY());
this.end = new Point(end.getX(),end.getY());
}
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()+"]";
}
}
public class Lines {
private ArrayList<Line> lines= new ArrayList<Line>();
public void add(Line line){
lines.add(line);
}
public double sumLength(){
double sum = 0;
for(Line line:lines)
sum += line.getLength();
return sum;
}
public String lengthString(){
return "Lines length is " + sumLength();
}
}
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);
System.out.println(line1.toString() + line1.getLength());
System.out.println(lines.lengthString());
}
}
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