In: Computer Science
PointList Class
Write a class named PointList that keeps a list of Point objects in an ArrayList. The PointList class should accept any object that is an instance of the Point class,, or a subclass of Point. Demonstrate the class in an application.
This is My point class….
public class Point<T>
{
private T xCoordinate;
private T yCoordinate;
public Point(T x, T y)
{
xCoordinate = x;
yCoordinate = y;
}
public void setX(T ){
xCoordinate = x;
}
public void setY(T y) {
yCoordinate = y;
}
public T getX(){
return xCoordinate;
}
public T getY(){
return yCoordinate;
}
}
And this is the starting of my code…my teacher wants it this way
import java.util.ArrayList;
public final class PointList<T extends Point<? extends Number>>
{
ArrayList<T> arrList = new ArrayList<>();
}
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
Note: Since you did not provide any details about the functionalities of PointList class, I have just created methods to add point, remove point, check if a point exists, and a toString method which returns a string containing all points. Also updated Point class to add equals() and toString() methods to make sure it works fine with the new PointList class.
// PointList.java
import java.util.ArrayList;
public final class PointList<T extends Point<? extends Number>> {
// list of points
private ArrayList<T> arrList;
// constructor to initialize empty list
public PointList() {
arrList = new ArrayList<T>();
}
// method to add a Point to the list
public void addPoint(T p) {
arrList.add(p);
}
// method to check if a point exists on the list
public boolean containsPoint(T p) {
return arrList.contains(p);
}
// method to remove a point from the list if exists
public void removePoint(T p) {
if (arrList.contains(p)) {
arrList.remove(p);
}
}
// method to return the size of the list
public int size() {
return arrList.size();
}
// returns a string containing all points data
public String toString() {
String data = "";
for (T p : arrList) {
data += p.toString() + "\n";
}
return data.trim();
}
}
//updated Point.java file (added equals and toString methods)
public class Point<T>
{
private T xCoordinate;
private T yCoordinate;
public Point(T x, T y)
{
xCoordinate = x;
yCoordinate = y;
}
public void setX(T x) {
xCoordinate = x;
}
public void setY(T y) {
yCoordinate = y;
}
public T getX() {
return xCoordinate;
}
public T getY() {
return yCoordinate;
}
// returns true if the parameter is a Point with same values.
// needed for contains() and remove() methods of array list to work
public boolean equals(Object ob) {
if (ob instanceof Point) {
Point p = (Point) ob;
return this.xCoordinate.equals(p.xCoordinate)
&& this.yCoordinate.equals(p.yCoordinate);
}
return false;
}
//returns a String containing point in (x, y) format
public String toString() {
return "(" + xCoordinate + ", " + yCoordinate + ")";
}
}
// Test.java
public class Test {
public static void main(String[] args) {
//creating a PointList object
PointList<Point<Integer>> pList = new PointList<Point<Integer>>();
//adding some points
for (int i = 0; i <= 5; i++) {
Point<Integer> p = new Point<Integer>(i, 5 - i);
pList.addPoint(p);
}
//testing all methods in PointList
System.out.println("Size: " + pList.size());
System.out.println("Points:");
System.out.println(pList);
System.out.println("Contains (2, 3): "
+ pList.containsPoint(new Point<Integer>(2, 3)));
System.out.println("Removing (2, 3)");
pList.removePoint(new Point<Integer>(2, 3));
System.out.println("Points:");
System.out.println(pList);
}
}
//OUTPUT
Size: 6
Points:
(0, 5)
(1, 4)
(2, 3)
(3, 2)
(4, 1)
(5, 0)
Contains (2, 3): true
Removing (2, 3)
Points:
(0, 5)
(1, 4)
(3, 2)
(4, 1)
(5, 0)