Question

In: Computer Science

PointList Class Write a class named PointList that keeps a list of Point objects in an...

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<>();

}

Solutions

Expert Solution

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)


Related Solutions

Write a collection class named "Jumbler". Jumbler takes in an optional list of strings as a...
Write a collection class named "Jumbler". Jumbler takes in an optional list of strings as a parameter to the constuctor with various strings. Jumbler stores random strings and we access the items based on the methods listed below. Jumbler supports the following methods: add() : Add a string to Jumbler get() : return a random string from Jumbler max() : return the largest string in the Jumbler based on the length of the strings in the Jumbler. iterator: returns an...
Write a class named GroceryList that represents a list of items to buy from the market,...
Write a class named GroceryList that represents a list of items to buy from the market, and another class named GroceryItemOrder that represents a request to purchase a particular item in a given quantity (example: four boxes of cookies). It also has client class called GroceryClient which creates objects of GroceryList and GroceryItemOrder. (For this assignment, you will have to submit 3 .java files: one for each class and 3 .class files associated with these .java files. So in total...
Consider a linked list whose nodes are objects of the class Node: class Node {    ...
Consider a linked list whose nodes are objects of the class Node: class Node {     public int data;     public Node next; } prev references a node n1 in the list and curr references the node n2 that is right after n1 in the list. Which of the following statements is used to insert a new node, referenced by newNodebetween prev and curr? Group of answer choices newNode.next = curr; prev.next = newNode; newNode.next = head; head = newNode;...
LANGUAGE PYTHON 3.7 Write a collection class named "Jumbler". Jumbler takes in an optional list of...
LANGUAGE PYTHON 3.7 Write a collection class named "Jumbler". Jumbler takes in an optional list of strings as a parameter to the constuctor with various strings. Jumbler stores random strings and we access the items based on the methods listed below. Jumbler supports the following methods: add() : Add a string to Jumbler get() : return a random string from Jumbler max() : return the largest string in the Jumbler based on the length of the strings in the Jumbler....
Write a program in java that does the following: Create a StudentRecord class that keeps the...
Write a program in java that does the following: Create a StudentRecord class that keeps the following information for a student: first name (String), last name (String), and balance (integer). Provide proper constructor, setter and getter methods. Read the student information (one student per line) from the input file “csc272input.txt”. The information in the file is listed below. You can use it to generate the input file yourself, or use the original input file that is available alone with this...
Design a class named GeoPoint to represent a point with x- and y-coordinates. The class contains:  ...
Design a class named GeoPoint to represent a point with x- and y-coordinates. The class contains:   The data fields x and y that represent the coordinates with gette and setter methods. A no-arg constructor that creates a point (0, 0).   A constructor that constructs a point with specified coordinates. The method equals(GeoPoint p) that returns true if two GeoPoint objects have the same x- and y-coordinates. Write a test program that creates an array of GeoPoint objects. The size of...
Design a class named MyPoint to represent a point with x- and y-coordinates. The class should...
Design a class named MyPoint to represent a point with x- and y-coordinates. The class should contain: Two data fields x and y that represent the coordinates. A no-arg constructor that creates a point at (0, 0). A constructor that creates a point with specified coordinates. Get methods for data fields x and y respectively. A method named distance that returns the distance from this point to another point with specified x- and y-coordinates. Use the formula: root (x2-x1)2 +...
python Design a class named IP_address to represent IP address objects. The IP_addressclass contains the following...
python Design a class named IP_address to represent IP address objects. The IP_addressclass contains the following A number of instance variables/fields to store a table of data. You can design them by your own. A constructor that creates a table with the following: a list of data. ip address a integer to indicate the number of elements in the sum_list/freq_list/average_list A get_ip_address() method that returns the ip address For example, consider the following code fragment: ip_key = '192.168.0.24' data_list =[(0,...
Python Design a class named IP_address to represent IP address objects. The IP_addressclass contains the following...
Python Design a class named IP_address to represent IP address objects. The IP_addressclass contains the following A number of instance variables/fields to store a table of data. You can design them on your own. A constructor that creates a table with the following: a list of data. IP address an integer to indicate the number of elements in the sum_list/freq_list/average_list A get_ip_address() method that returns the IP address For example, consider the following code fragment: ip_key = '192.168.0.24' data_list =[(0,...
please use C++ Create a class named Byte that encapsulates an array of 8 Bit objects....
please use C++ Create a class named Byte that encapsulates an array of 8 Bit objects. The Byte class will provide the following member functions: Byte - word: Bit[8] static Bit array defaults to all Bits false + BITS_PER_BYTE: Integer16 Size of a byte constant in Bits; 8 + Byte() default constructor + Byte(Byte) copy constructor + set(Integer): void sets Bit to true + clear(): void sets to 0 + load(Byte): void sets Byte to the passed Byte + read():...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT