So my instructor assigned us to create a program that calculates the area and stores that, along with coordinates, in an array. This has to be done for both triangles and squares. He is having us use separate classes for the project; the main Drawer class, parent class, square class, and triangle class. I've got a portion of it done, but I'm honestly not sure where I need to go from here. I can't seem to figure out how to get the information read from the main part of the program into the methods that I have and I don't know if I need to add more methods to accomplish that. I'm also getting errors in what I do have;
public class Drawer
{
//Declare array for shapes to be stored in
public Parent myShape[] = new Parent[10];
/*
* Description: This method will allow the items to be entered into the array
* and will place them in the first empty location
* Precondition: The array must already exist and have been set to null.
* Postcondition: The item will reside in the array.
*/
public double add(double z)
{
//Use the findFirstEmpty to locate the first null spot
int x = findFirstEmpty();
//Take the empty spot and put the item in it
if (x >= 0)
{
myShape[x] = z;
}
else
{
//indicates that the array is full
System.out.println("The array is at maximum capacity");
}
//Return the spot with the shape in it.
return z;
}
/**
* Description: This method will search for the first empty spot in the array
* Precondition: The array must already exist and there must be at least
* one item in the array.
* Postcondition: There will now be one less empty spot since an item will
* be residing in the formerly empty location.
*/
public int findFirstEmpty()
{
/**
* Function of this method is to scan the array for the first null
* position
*/
for (int i = 0; i < myShape.length; i++)
{
/**
* Scan the array by using int i to correspond to the position of the
* empty spot in the array.
*/
if (myShape[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 the bus stops and a child gets off,
* freeing up a seat.
* Precondition: The bus array must already exist and there must be at least
* one child on the bus.
* Postcondition: There will be one more empty spot since the child that was
* residing in that spot got off at the bus stop.
*/
public void delete(double z)
{
for (int x = 0; x < myShape.length; x++)
//Take the filled seat and remove the child
if (z.equals(myShape[x]))
{
//Removes a specific person from the bus array
myShape[x] = null;
}
}
/*
* Description: This class holds the methods and other items that are
* inherited by both of the children classes.
* Precondition: The array must exist.
* Postcondition: The children classes will utilize the methods and items in
* this section to recieve desired results
*/
class Parent
{
protected double coordx;
protected double coordy;
/*
* 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 setCoord(double x, double y)
{
coordx = x;
coordy = y;
}
public void print()
{
//Print out the array and the position of the item in the array.
//Need to print coordx, coordy, side, and area.
// for (int x = 0; x < 10; x++)
// {
// if (names[x] != null)
// {
// System.out.println(x + " , " + names[x]);
// }
// }
}
}
public class Square extends Parent
{
private double side = 0;
private double area = 0;
//Get side of square and return it
public double getSide()
{
return side;
}
/**
* 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.
*/
public double computeArea()
{
return area = side * 4;
}
}
public class Triangle extends Parent
{
private double base;
private double height;
private double area;
//Get base of triangle and return it
public double getBase()
{
return base;
}
//Get height of triangle and return it
public double getHeight()
{
return height;
}
/**
* 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.
*/
public double computeArea()
{
return area = .5 * base * height;
}
}
public static void main(String[] args)
{
Drawer d1 = new Drawer();
/**
* Enter in the first shape; Triangle
* base = 10
* height = 5.1
* coordinates = (1,2)
*/
double B = 10;
double H = 5.1;
double C1 = 1;
double C2 = 2;
/**
* Enter in the second shape; Triangle
* base = 3
* height = 7
* coordinates = (3,2)
*/
/**
* Enter in the third shape; Square
* size = 5
* coordinates = (7,5)
*/
//Print the array
/**
* Enter in the fourth shape; Triangle
* base = 20
* height = 30
* coordinates = (20,2)
*/
//Delete object in array position 1
//Print the array
/**
* Enter in the fifth shape; Triangle
* base = 100
* height = 35
* coordinates = (1,20)
*/
/**
* Enter in the sixth shape; Triangle
* base = 25
* height = 50
* coordinates = (1,22)
*/
//Print the array
//Change all of the coordinates to (-1,-1)
//Print the array
}
}
The array at the beginning is giving the error; exporting non-public type through public API. However, that's the method my teacher instructed us to use the array.
I am also getting an error of incompatible types in the add() method (the myShapes[x] = z), and I'm unsure why. It is the same with the delete method, which states that it cannot be dereferenced.
I have the basics of what I need to be doing for this program, I just keep getting stuck. I would appreciate being pointed in the right direction. Also, any references would be much appreciated! Thank you!