I'm working on an assignment for class, and it's working on classes to make us call items from other classes as often as possible. I'm trying to call public int side(int number) from another public int, but it won't let me.
I can't rearrange the code to edit public int side at all because it was included in part of the assignment
package lab6_carl6188;
class Polygon
{
private int[] sideLengths;
public Polygon(int sides, int ... lengths)
{
int index = 0;
sideLengths = new int[sides];
for (int length: lengths)
{
sideLengths[index] = length;
index += 1;
}
}
public int side(int number)
{
return sideLengths[number];
}
public int perimeter()
{
int total = 0;
for (int index = 0; index < sideLengths.length; index += 1)
{
total += side(index);
}
return total;
}
}
class Rectangle{
private int[] sideLengths;
public Rectangle(int length1, int length2) {
sideLengths[0] = length1;
sideLengths[1] = length2;
}
public int area() {
int total = 1;
for(int x = 0; x < sideLengths.length; x++) {
total = total * Polygon.side(x);
}
}
}
}
This whole block is the code. I'm not allowed to edit class Polygon in any way.
public int side(int number)
{
return sideLengths[number];
}
is what I want to call, and I'm calling it this way:
public int area() {
int total = 1;
for(int x = 0; x < sideLengths.length; x++) {
total = total * Polygon.side(x);
}
}
My error is "Cannot make a static reference to the non-static method side(int) from the type Polygon"