A polyline is a line with segments formed by a list of points of type MyPoint. L
ID: 3736660 • Letter: A
Question
A polyline is a line with segments formed by a list of points of type MyPoint. Let's use the ArrayList (dynamically allocated array) to keep the points, but upcast to List in the instance variable It contains an ArrayList of MyPoint objects . A default (or "no-argument" or "no-arg") constructor that construct an empty ArrayList. An overloaded constructor that constructs a PolyLine object with the given list of MyPoint A tostring) method that returns a string description of the polyline in this format: (x1,y1)(x2,y2)(x3,y3)...) A method appendMyPoint(MyPoint) which append a MyPoint object to the end of the current polyline . An overloaded method appendMyPoint (int x, int y) which append a MyPoint(x, y) to the end of the current polyline . A method getSize() that returns the number of MyPoint objects in the current polyline . A method getMyPoint(int index) that returns the MyPoint object at the specified position in this polyline . A method getLength that returns the total length of this polyine A method removeLastMyPoint that removes the last MyPoint from the current polyine . A method join(PolyLine another) that joins the given polyline to the end of the current polyline Write the PolyLine class in the answer box below assuming that the MyPoint class has been done for you in the system For example: Test Result POLy Line line = new PolyLine(); line.appendMyPoint (new MyPoint(1, 2)); line.appendMyPoint (3, 4); line.appendMyPoint(5, 6); System.out.println(line); Answer: (penalty regime: 0 %) 1 import java.util.*; 2+1 class PolyLine { private ListExplanation / Answer
Please find the code below with detailed inline comments.
CODE
===================
import java.util.*;
class MyPoint {
private int x, y;
public MyPoint(int x, int y) {
super();
this.x = x;
this.y = y;
}
/**
* @return the x
*/
public int getX() {
return x;
}
/**
* @param x the x to set
*/
public void setX(int x) {
this.x = x;
}
/**
* @return the y
*/
public int getY() {
return y;
}
/**
* @param y the y to set
*/
public void setY(int y) {
this.y = y;
}
}
public class PolyLine {
private List<MyPoint> points;
public PolyLine() {
points = new ArrayList<>();
}
public PolyLine(ArrayList<MyPoint> points) {
this.points = points;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
String res = "{";
for(MyPoint p : points) {
res += "(" + p.getX() + "," + p.getY() + ")";
}
res += "}";
return res;
}
public void appendMyPoint(MyPoint point) {
points.add(point);
}
public void appendMyPoint(int x, int y) {
points.add(new MyPoint(x, y));
}
public int getSize() {
return points.size();
}
public MyPoint getPoint(int index) {
if(index >= points.size())
return null;
return points.get(index);
}
public void removeMyLastPoint() {
points.remove(getSize() - 1);
}
public void join(PolyLine other) {
points.addAll(other.points);
}
public double getTotalLength() {
double length = 0;
for(int i=1; i<getSize(); i++) {
int x2 = points.get(i).getX();
int y2 = points.get(i).getY();
int x1 = points.get(i-1).getX();
int y1 = points.get(i-1).getY();
length += Math.sqrt((x2 - x1) * ((x2 - x1) + (y2 - y1) * (y2 - y1));
}
return length;
}
}