0

I am working on homework so I just want to fix my compile error so I can keep working on it. I need to make a PointList class 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 sub-class of Point.

I keep getting a compiler error that says

> required: Point<Integer>
  found: no arguments
  reason: actual and formal argument lists differ in length
  where E is a type-variable:
  E extends Object declared in class PointList

I really don't understand what I am missing I've gone through the book and can't seem to see why I get this error. I have Point class made and test done but can't seem to get it to compile.

public class PointListTest{

public static void main(String[] args){

//create point ints
Point<Integer> pointOne = new Point<Integer>(1,2);
Point<Integer> pointTwo = new Point<Integer>(3,4);
Point<Integer> pointThree = new Point<Integer>(5,6);

//make object PointList pointlist for int
PointList<Point<Integer>> newPointList = new PointList<Point<Integer>>();

//add points to list
newPointList.add(pointOne);
newPointList.add(pointTwo);
newPointList.add(pointThree);

This is what I have so far for PointList

public class PointList<E>{

   private ArrayList<Point> myPoint;

   E data;

   public PointList(E obj){

      ArrayList<Point> myPoint = new ArrayList<Point>();

      data = obj;

   }

}

Point class

public class Point<T extends Number>{

//field values
private T xCordinate;
private T yCordinate;

//constructor
//@param x the x cordinate
//@param y the y cordinate
public Point(T x, T y){
   xCordinate = x;
   yCordinate = y;
}//end constructor

public void setX(T x){

   xCordinate = x;

}//end setX

public void setY(T y){

   yCordinate = y;

}//end setY

public T getX(){

   return xCordinate;

}//end getX

public T getY(){

   return yCordinate;

}//end getY

}//end pointlist

If I could just get it to compile so that I could keep working on it I'd be extremely grateful.

6
  • hi! welcome to SO! may we see your point object? and how come your PointList object is using a .add() method? Commented Feb 19, 2019 at 3:05
  • Thanks I think that my Point class is not having issues but I will post it just in case. I had an add method in my PointList class at one point but I deleted it. I've been beating my head at this since this morning. Commented Feb 19, 2019 at 3:10
  • Your PointList constructor is requiring you to have an argument. Do note that the way you instantiated your PointList object, you didn't pass any data. My suggestion is for you to go through your PointList object and check from there and tell me what you think. Another thing, for your PointList class, take advantage of inheritance. PointList<E> extends List<E> Commented Feb 19, 2019 at 3:14
  • If I use something like this PointList newPointList = new PointList(pointOne); It works but it doesn't use generics to instantiate the object. I thought if I used <Point<Integer>> it would also pass a point object? Does it not? Commented Feb 19, 2019 at 3:21
  • No it does not pass a Point object. It just declares that it can accept a generic type of that certain object. Commented Feb 19, 2019 at 3:23

2 Answers 2

1

Your PointList has the only constructor with one argument:

public PointList(E obj){

      ArrayList<Point> myPoint = new ArrayList<Point>();

      data = obj;

}

But you try to call constructor with zero arguments:

PointList<Point<Integer>> newPointList = new PointList<Point<Integer>>();

So, compiler can't find the corresponding one. See Providing Constructors for Your Classes tutorial and Constructor overloading in Java.

Sign up to request clarification or add additional context in comments.

Comments

0

For PointList

// Take advantage of inheritance
public class PointList<E> extends ArrayList<E> {
   // This object will now have the methods
}

Usage

// If you're using Java 8 onwards, you may not need to specify the type upon instantiation
PointList<Point<Integer>> pointList = new PointList<>();
pointList.add(new Point<Integer>(x, y));

If you want to use your own PointList

public class PointList<E> {
   private List<E> dataList;

   public PointList() {
      dataList = new ArrayList<>();
   }

   public void add(E obj) {
      dataList.add(obj);
   }
}

As mentioned by one of the comments, there seems to be a mismatch with your constructor and with your object creation.

2 Comments

Thank you I added a no-args constructor! Now I just need to figure out how to print out the array values and not the memory address. I can't seem to get extends ArrayList<E> to work so that I can use the toString method. I get Error: add(E#1) in PointList cannot implement add(E#2) in List. What does that mean?
If you want to create your own implementation of add() in your extended PointList, then you can do: @Override public boolean add(E e) { return super.add(e); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.