I'm trying to add a new shape to an ArrayList<Shape>
, but I'm getting an error
The method add (Shape) in the type
ArrayList<Shape>
is not applicable for the arguments (Shape.Rectangle)
Shape.Rectangle
is a custom class I wrote. The arguments for it are:
Rectangle(String name, Double length, Double width)
I get this error for the circle and triangle as well.
If I change the ArrayList
type to Object
instead of Shape
, the create()
method works. However, the method display()
stops working.
This is my code:
public static void create(ArrayList<Shape> list) throws FileNotFoundException {
// Create shape objects using data from an input file
File dataFile = new File("./src/shape/dataFile.txt");
Scanner scan = new Scanner(dataFile);
while (scan.hasNextLine()) {
String shapeType = scan.nextLine();
if (shapeType.equals("Rectangle")) {
list.add(new Rectangle(scan.nextLine(), scan.nextDouble(), scan.nextDouble()));
}
else if (shapeType.equals("Circle")) {
list.add(new Circle(scan.nextLine(), scan.nextDouble()));
}
else if (shapeType.equals("Triangle")) {
list.add( new Triangle(scan.nextLine(), scan.nextDouble(), scan.nextDouble(), scan.nextDouble()));
}
}
scan.close();
}
public static void display(ArrayList<Shape> list) {
for (int i = 0; i < list.size(); i++) {
Shape shape = list.get(i);
String name = shape.getName();
Double area = shape.area();
System.out.println(name);
System.out.println(area);
}
}
If i change the ArrayList type to Object instead of Shape, the create() method works. However, I receive errors in the display() method when trying to getName() and area().
Edit: The code for Shape and Rectangle is:
abstract class Shape {
/**
* The name of this shape
*/
private String name;
/**
* Default constructor
* Creates a Shape instance with a default name such as "Shape"
*/
public Shape() {
this.name = "Shape";
}
/**
* Second constructor
* Given a name, creates a Shape instance with the name
* @param name Name of the new shape
*/
public Shape(String name) {
this.name = name;
}
/**
* Retrieves the name of this shape
* @return Returns the name of shape
*/
public String getName() {
return name;
}
/**
* Given a name, changes the name of this shape to the new name
* @param newName New name of the shape
*/
public void setName(String newName) {
this.name = newName;
}
/**
* Returns the area of this shape
* @return Return of the area of shape
*/
abstract double area();
/**
* Compares this with some other object
* This method overrides the java equals method
* @param obj A reference to some other object
* @return true if some other object is equal to this shape, false otherwise
*/
public boolean equals(Object obj) {
if(obj == this.name) {
return true;
}
else {
return false;
}
}
/**
* Represents this shape as a string literal
* This method overrides the Java toString method
* @return A string representation of the shape
*/
public String toString() {
return this.getClass().getSimpleName() + ": " + this.name;
}
/**
* Rectangle Subclass
*/
public static class Rectangle {
/**
* Instance variables
* Name, length, and width of shape.
*/
private String name;
private Double length, width;
/**
* Default Constructor
* Creates a default rectangle instance.
*/
public Rectangle() {
this.name = "Rectangle";
this.length = 1.0;
this.width = 1.0;
}
/**
* Second Constructor
* Given a name, a length, and a width, creates a rectangle instance with the name, the length, and the width.
* @param name, length, width Create a rectangle with these arguments
*/
public Rectangle(String name, Double length, Double width) {
this.name = name;
this.length = length;
this.width = width;
}
/**
* getName
* Returns the name of this rectangle
* @return name Return name of this rectangle
*/
public String getName() {
return name;
}
/**
* setName
* Given a name, replace the name of this rectangle
* @param newName Name to replace the original name of this rectangle
*/
public void setName(String newName) {
this.name = newName;
}
/**
* getLength
* Returns the length of this rectangle
* @return length
*/
public Double getLength() {
return length;
}
/**
* setLength
* Given a length, changes the length of this rectangle to the new length.
* @param newLength
*/
public void setLength(Double newLength) {
this.length = newLength;
}
/**
* getWidth
* Returns the width of this rectangle
* @Return width
*/
public Double getWidth() {
return width;
}
/**
* setWidth
* Given a width, changes the width of this rectangle to the new width
* @param newWidth
*/
public void setWidth(Double newWidth) {
this.width = newWidth;
}
/**
* Area
* Returns the area of this rectangle
* @return area
*/
public Double area() {
double area = this.length * this.width;
return area;
}
/**
* Equals
* Compares this rectangle with some other object.
* @param obj Reference object to compare to
* @return true if some other object is equal to this rectangle, false otherwise
*/
public boolean equals(Object obj) {
if(obj == this.name) {
return true;
}
else {
return false;
}
}
/**
* toString
* Represents this rectangle as a string literal.
* @return A string representation of this rectangle
*/
public String toString() {
return this.getClass().getSimpleName() + ": " + this.name;
}
}
The list expects arguments of class Shape
, however you are passing Rectangle
.
You might want to make Rectangle
, Circle
and Triangle
classes to inherit from Shape
.