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 constructs 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 appends a MyPoint object to the end of the current polyline.
- An overloaded method appendMyPoint(int x, int y) which appends 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 getLength() that returns the total length of this polyline.
- A method removeLastMyPoint() that removes the last MyPoint from the current polyline.
- A method joinPolyLine(PolyLine) 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
PolyLine line = new PolyLine();
line.appendMyPoint(new MyPoint(1, 2));
line.appendMyPoint(3, 4);
line.appendMyPoint(5, 6);
System.out.println(line);
{123456}
Answer: (penalty regime: 0%)
1. import java.util.*;
2. class PolyLine {
private List<MyPoint> points;
Title_with_topic: PolyLine Class Implementation in Java with ArrayList and MyPoint