So I'm writing a program that has a main class ArrayDrawer and two classes; square and triangle. Currently, this is what I have;
public abstract class ArrayDrawer
{
//Global Data
/**
* Create array with object scope to store squares in
*/
protected Square[] squares = new Square[10];
/**
* Create array with object scope to store triangles in
*/
protected Triangle[] triangles = new Triangle[10];
/*
* Description: This method will allow the squares to be entered into the array
* and will place them in the first empty location
* Precondition: The square array must already exist and have been set to null.
* Postcondition: The square will reside in the square array.
*/
public void addSquare(Square z)
{
/**
* Use findFirstEmptySquare to locate the first null spot in the square array
*/
int x = findFirstEmptySquare();
/**
* Take the empty spot and place the square in it
*/
if (x >= 0)
{
squares[x] = z;
}
else
{
//indicates that the array is full
System.out.println("The array is at maximum capacity");
}
}
/**
* Description: This method will search for the first empty spot in the square array
* Precondition: The square array must already exist
* Postcondition: There will now be one less empty spot since a square will
* be residing in the formerly empty location.
*/
private int findFirstEmptySquare()
{
/**
* Function of this method is to scan the array for the first null
* position
*/
for (int i = 0; i < squares.length; i++)
{
/**
* Scan the array by using int i to correspond to the position of the
* empty spot in the square array.
*/
if (squares[i] == null)
{
//Indicates that the scan located a null spot in the array
return i;
}
}
//Indicates that the scan produced no null spots in array
return -1;
}
/**
* Description: This method is when a square is removed from the square array
* Precondition: The square array must already exist and there must be at least
* square in the array.
* Postcondition: There will be one more empty spot since the square that was
* residing in that was removed.
*/
public void deleteSquare(int p)
{
for (int x = 0; x < squares.length; x++)
//Take the filled seat and remove the child
if (p == x)
{
//Removes a specific triangle from the bus array
squares[x] = null;
}
}
/*
* Description: This method will allow the triangles to be entered into the triangle array
* and will place them in the first empty location
* Precondition: The triangle array must already exist and have been set to null.
* Postcondition: The triangle will reside in the array.
*/
public void addTriangle(Triangle z)
{
/**
* Use findFirstEmptySquare to locate the first null spot in the triangle array
*/
int x = findFirstEmptyTriangle();
/**
* Take the empty spot and place the square in it
*/
if (x >= 0)
{
triangles[x] = z;
}
else
{
//indicates that the array is full
System.out.println("The array is at maximum capacity");
}
}
/**
* Description: This method will search for the first empty spot in the triangle array.
* Precondition: The array must already exist.
* Postcondition: There will now be one less empty spot since a triangle will
* be residing in the formerly empty location.
*/
private int findFirstEmptyTriangle()
{
/**
* Function of this method is to scan the array for the first null
* position
*/
for (int i = 0; i < triangles.length; i++)
{
/**
* Scan the array by using int i to correspond to the position of the
* empty spot in the array.
*/
if (triangles[i] == null)
{
//Indicates that the scan located a null spot in the array
return i;
}
}
//Indicates that the scan produced no null spots in array
return -1;
}
/**
* Description: This method is when a triangle is removed from the triangle array
* Precondition: The triangle array must already exist and there must be at least
* triangle in the array.
* Postcondition: There will be one more empty spot since the triangle that was
* residing in that was removed.
*/
public void deleteTriangle(int p)
{
for (int x = 0; x < triangles.length; x++)
//Take the filled seat and remove the child
if (p == x)
{
//Removes a specific triangle from the bus array
triangles[x] = null;
}
}
/**
* Description: This method will print all of the occupied positions in the arrays.
* It will first print squares, then triangles.
* Precondition: The arrays must already exist and there must be at least
* item in the arrays
* Postcondition: There will be a printout of all of the occupied slots in the arrays
* and what is occupying them
*/
public void print()
{
/**
* Print out the Square array
*/
squares.print();
/**
* Print out the Triangle array
*/
triangles.print();
}
public abstract double computeArea();
public class Square extends ArrayDrawer
{
//Data fields
private double side = 0;
private double area = 0;
private double coordx = 0;
private double coordy = 0;
/**
* Description: This method computes the area of the Square.
* Precondition: The sides must have already been entered into the array.
* Postcondition: There will be a resulting area.
*/
@Override
public double computeArea()
{
return area = side * side;
//Test
//System.out.println(area);
}
/*
* Description: This method sets the coordinates for the object.
* Precondition: The array must exist and coordinates must have already
* been entered in.
* Postcondition: The item in the array will have set coordinates.
*/
public void setCoord(double x, double y)
{
coordx = x;
coordy = y;
}
/**
* Description: Prints out the information located in the square array
* Precondition: There is something residing in the square array
* Postcondition: N/A
*/
@Override
public void print()
{
for (int x = 0; x < 10; x++)
{
if (squares[x] != null)
{
System.out.println(squares[x]);
}
}
//Prints Coordx
//Prints Coordy
//Prints Side
//Prints Area
}
}
public class Triangle extends ArrayDrawer
{
//Data fields
private double base = 0;
private double height = 0;
private double area = 0;
private double coordx = 0;
private double coordy = 0;
/**
* Description: This method computes the area of the Triangle.
* Precondition: The sides must have already been entered into the array.
* Postcondition: There will be a resulting area.
*/
@Override
public double computeArea()
{
return area = .5 * base * height;
//Test
//System.out.println(area);
}
/*
* Description: This method sets the coordinates for the object.
* Precondition: The array must exist and coordinates must have already
* been entered in.
* Postcondition: The item in the array will have set coordinates.
*/
public void setCoord(double x, double y)
{
coordx = 0;
coordy = 0;
}
/**
* Description: Prints out the information located in the triangle array
* Precondition: There is something residing in the triangle array
* Postcondition: N/A
*/
@Override
public void print()
{
for (int x = 0; x < 10; x++)
{
if (squares[x] != null)
{
System.out.println(squares[x]);
}
}
//Prints Coordx
//Prints Coordy
//Prints Base
//Prints Height
//Prints Area
}
}
public static void main(String[] args)
{
ArrayDrawer ar1 = new ArrayDrawer();
/**
* Create a triangle with the following information:
* Base = 1
* Height = 2
* Coords(3,4)
*/
double b = 1;
double h = 2;
double cx = 3;
double cy = 4;
ar1.addTriangle(b);
ar1.addTriangle(h);
ar1.addTriangle(cx);
ar1.addTriangle(cy);
/**
* Create a triangle with the following information:
* Base = 5
* Height = 6
* Coords(7,8)
*/
b = 5;
h = 6;
cx = 7;
cy = 8;
ar1.addTriangle(b);
ar1.addTriangle(h);
ar1.addTriangle(cx);
ar1.addTriangle(cy);
/**
* Create a square with the following information:
* Size = 9
* Coords(10,11)
*/
double s = 0;
cx = 10;
cy = 11;
ar1.addSquare(s);
ar1.addSquare(cx);
ar1.addSquare(cy);
/**
* Print all geometric objects with the following parameters:
* Square array prints first
* Triangle array prints second
* prints: "After creating a square with size = 9, coords = (10,11), this
* is what is in the array", same for triangle
*/
System.out.println("After creating a square with size = 9 and coordinates "
+ "(10,11), this is what resides in the two arrays: ");
/**
* Create a triangle with the following information:
* Base = 12
* Height = 13
* Coords(14,15)
*/
b = 12;
h = 13;
cx = 14;
cy = 15;
ar1.addTriangle(b);
ar1.addTriangle(h);
ar1.addTriangle(cx);
ar1.addTriangle(cy);
/**
* Delete the triangle in position 1
*/
/**
* Print all geometric objects with the following parameters:
* Square array prints first
* Triangle array prints second
* prints: "After deleting the triangle in position 1 of the triangle array,
* this is what remains:"
*/
System.out.println("After deleting the triangle in position 1 of the triangle array," +
" this is what remains:");
/**
* Create a triangle with the following information:
* Base = 16
* Height = 17
* Coords(18,19)
*/
b = 16;
h = 17;
cx = 18;
cy = 19;
ar1.addTriangle(b);
ar1.addTriangle(h);
ar1.addTriangle(cx);
ar1.addTriangle(cy);
/**
* Print all geometric objects with the following parameters:
* Square array prints first
* Triangle array prints second
* prints: "After creating a triangle with base = 16, height = 17 and
* coordinates (18,19), this is what resides in the arrays: "
*/
System.out.println("After creating a triangle with base = 16, height = 17 and "
+" coordinates (18,19), this is what resides in the arrays: ");
/**
* Create a triangle with the following information:
* Base = 20
* Height = 21
* Coords(22,23)
*/
b = 20;
h = 21;
cx = 22;
cy = 23;
ar1.addTriangle(b);
ar1.addTriangle(h);
ar1.addTriangle(cx);
ar1.addTriangle(cy);
/**
* Create a square with the following information:
* Size = 24
* Coords(25,26)
*/
s = 24;
cx = 25;
cy = 26;
ar1.addSquare(s);
ar1.addSquare(cx);
ar1.addSquare(cy);
/**
* Print all geometric objects with the following parameters:
* Square array prints first
* Triangle array prints second
* prints: "After creating a square with size = 24 and coordinates (25,26),
* this is what remains in the array: "
*/
System.out.println("After creating a square with size = 24 and coordinates (25,26),"
+ " this is what remains in the array: ");
/**
* Change the coordinates of all objects to (-1,-1)
*/
/**
* Print all geometric objects with the following parameters:
* Square array prints first
* Triangle array prints second
* prints: "After changing the coordinates of all objects to (-1, -1),
* this is what remains in the array: "
*/
System.out.println("After changing the coordinates of all objects to (-1, -1), "
+ "this is what remains in the array: ");
}
}
The problems I'm having are:
1. I was testing the add method (using the main method to add the first base value to the triangle) and noticed that it gave this error on the ar1.addTriangle(base);
"method addTriangle in class arraydrawer.ArrayDrawer cannot be applied to given types;
required: arraydrawer.ArrayDrawer.Triangle
found: double
reason: actual argument double cannot be converted to arraydrawer.ArrayDrawer.Triangle by method invocation conversion"
I'm unsure why it is doing so, but I think it has to do with the fact that I have it set up to addTriangle(Parent z)...I'm not sure how to fix that.
2. With the print functions, it is giving me problems when I call on the methods in the classes from the print method in the main class.
"cannot find symbol
symbol: method print()
location: variable squares of type arraydrawer.ArrayDrawer.Square[]"
I can't figure out exactly why, since they exist.
3. When I'm working on the program, trying to get it to do what I want, would I have to use the addTriangle/Square to get the information desired in (be it base, side, or coordinates), or is there another method? Is there any examples of multiple things being added to the same position of an array? I've only every put one item into an array, not multiple pieces of information.
Thank you in advanced to anyone who helps! I really appreciate it. With all the help I've received on this site, I'm definitely learning.