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!

Hi there. You are receiving incompatible types because x is and int and z is a double.

Also, are you allowed to change from public parent to Shape myShape = new Shape[10] ?

That's nearly right...
myShapes is declared as an array of Parent objects
public Parent myShape[] = new Parent[10];
and, as already said, z is a double. You can't put a double into an an array of Parents. That array can only hold Parent objects, instances of any sub-classes of Parent, or nulls.

Hi there. You are receiving incompatible types because x is and int and z is a double.

Also, are you allowed to change from public parent to Shape myShape = new Shape[10] ?

So, I tried changing all of the x int's into doubles, since z is double, but now it's saying there is a loss of precision.

I don't believe so. These are the directions that were given to us.
The class Drawer:
The class will have array: Parent[] myShapes of size 10. The array will have object scope

That's nearly right...
myShapes is declared as an array of Parent objects
public Parent myShape[] = new Parent[10];
and, as already said, z is a double. You can't put a double into an an array of Parents. That array can only hold Parent objects, instances of any sub-classes of Parent, or nulls.

Alright, that makes sense, I'm unsure how to make that work though. The directions we got included this;
The class Drawer:
The class will have array: Parent[] myShapes of size 10. The array will have object scope.

Which I assumed meant that we need to declare the array outside of Parent, so that we can use it throughout the program. Is that assumption wrong? Or would it make more sense to put the array into the parent class?

public class Drawer
{
    //Declare array for shapes to be stored in
    public Parent myShape[] = new Parent[10]; //ERROR RECEIVED: Exporting nonpublic type through public API
    
    
     /*
     * 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
        double x = findFirstEmpty(); 
        //Take the empty spot and put the item in it
        if (x >= 0)
        {
            myShape[x] = z;  //ERROR RECEIVED: POSSIBLE LOSS OF PRECISION
        }
        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 (double 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)//ERROR RECEIVED: POSSIBLE LOSS OF PRECISION
            {
                //Indicates that the scan located a null spot in the array
                return i; //ERROR RECEIVED: POSSIBLE LOSS OF PRECISION
            }  
      	}
        //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 (double x = 0; x < myShape.length; x++)
        //Take the filled seat and remove the child
         if (z.equals(myShape[x])) //ERROR RECEIVED: DOUBLE CANNOT BE DEREFERENCED
         {
              //Removes a specific person from the bus array
              myShape[x] = null;  //ERROR RECEIVED: POSSIBLE LOSS OF PRECISION
         }    
    }
    
    
    /*
     * 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
    }
}

This is what I changed so far with the errors included next to the item.

it may be worth noting that while you are correctly calculating the area for a triangle, you have the wrong formula down for squares.

now as for the myShapes array scope, the instructions suggest that the array will be declared as a global variable in the Drawer class, any object created lives within the closest brackets surrounding where it was declared, so a global variable (or as your teacher instructed "The array will have object scope.") is declared inside a class but outside the class' methods so that it lives anywhere within said object.

here is an easy example to understand :

public class MyClass{
    //global variables
    public int someNumber;

    //constructor (i noticed you didnt have any for triangle and square classes, might wanna look into those)
    public MyClass(int value){
        someNumber = value;
    }

    //methods
    public int someMethod(int someParameter){
        int localNumber; //declared localy , lives localy
        
        localNumber = someParameter * 2;
        
        return localNumber;
    }

    public void someOtherMethod(){
        //here you can access "someNumber" because its global,
        //but you cant access "localNumber" because its scope was just within "someMethod" and here it doesnt exist.

        someNumber = 0; //ok
        localNumber = 0; //not ok
    }
}

this class doesnt do anything smart so dont focus on that, its just to demonstrate what scope is.

public class Drawer
{
    //Declare array for shapes to be stored in
    public Parent myShape[] = new Parent[10]; //ERROR RECEIVED: Exporting nonpublic type through public API
    
    
     /*
     * 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
        double x = findFirstEmpty(); 
        //Take the empty spot and put the item in it
        if (x >= 0)
        {
            myShape[x] = z;  //ERROR RECEIVED: POSSIBLE LOSS OF PRECISION
        }
        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 (double 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)//ERROR RECEIVED: POSSIBLE LOSS OF PRECISION
            {
                //Indicates that the scan located a null spot in the array
                return i; //ERROR RECEIVED: POSSIBLE LOSS OF PRECISION
            }  
      	}
        //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 (double x = 0; x < myShape.length; x++)
        //Take the filled seat and remove the child
         if (z.equals(myShape[x])) //ERROR RECEIVED: DOUBLE CANNOT BE DEREFERENCED
         {
              //Removes a specific person from the bus array
              myShape[x] = null;  //ERROR RECEIVED: POSSIBLE LOSS OF PRECISION
         }    
    }
    
    
    /*
     * 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
    }
}

This is what I changed so far with the errors included next to the item.

because this is not correct

double x=.... ;
array[x];

and array index can only be an integer so try int x=... this goes for all the places you use arrays with a double declared for its index, because logically speaking array[x.xx] would never exist

it may be worth noting that while you are correctly calculating the area for a triangle, you have the wrong formula down for squares.

now as for the myShapes array scope, the instructions suggest that the array will be declared as a global variable in the Drawer class, any object created lives within the closest brackets surrounding where it was declared, so a global variable (or as your teacher instructed "The array will have object scope.") is declared inside a class but outside the class' methods so that it lives anywhere within said object.

here is an easy example to understand :

public class MyClass{
    //global variables
    public int someNumber;

    //constructor (i noticed you didnt have any for triangle and square classes, might wanna look into those)
    public MyClass(int value){
        someNumber = value;
    }

    //methods
    public int someMethod(int someParameter){
        int localNumber; //declared localy , lives localy
        
        localNumber = someParameter * 2;
        
        return localNumber;
    }

    public void someOtherMethod(){
        //here you can access "someNumber" because its global,
        //but you cant access "localNumber" because its scope was just within "someMethod" and here it doesnt exist.

        someNumber = 0; //ok
        localNumber = 0; //not ok
    }
}

this class doesnt do anything smart so dont focus on that, its just to demonstrate what scope is.

Okay, that makes sense to me. I'm not sure how to apply it though. I understand that there are some that are considered local and others that global. Since mine references Parent, would that mean it stays within the parent class? I'm confused as to where it needs to be located to work. I also tried to add in methods, similar to the one that you mentioned I didn't have;

public class MySquare(double Y)
        {
            myShape= Y;
        }

Within the class Square, but it gives me errors. Was I incorrect in my understanding of what you meant?

because this is not correct

double x=.... ;
array[x];

and array index can only be an integer so try int x=... this goes for all the places you use arrays with a double declared for its index, because logically speaking array[x.xx] would never exist

I thought so, but then how do I deal with the error I get when I try myShape[x] = Z;
in this;

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;
    }

I understand that there are some that are considered local and others that global. Since mine references Parent, would that mean it stays within the parent class? I'm confused as to where it needs to be located to work.

Hmm I'm not sure quiet what you meant but an aray cannot have a double as its index it must be an int value thats why it gives you problems 'loss of precision'.

I thought so, but then how do I deal with the error I get when I try myShape[x] = Z;
in this;

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;
    }

but i see you must have understood my post ... what is the error messages its giving you ? post the full error message

looking at your full code this in the delete method cannot be done:

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;  
         }    
    }

you cannot compare a double to a 'Parent' object using .equals().. what is your Parent object you should have get and set methods if you'd like to compare values from within a object instance like in your other codes, then change delete similar too this

public void delete(double z)
    {
..
..
         if (z==myShape[x].getCoOrdinates())//method getCoOrdinates() will be in the Parent class
         {
..
..
         }    
    }

also

public setCoord(double x, double y)
          {
              coordx = x;
              coordy = y;
              
          }

is giving errors its return type should be void. it is also good practice to name global variables in classes as private and not protected, refering to your Parent class

The OP's fundamental misunderstanding here is that he has "Parent" objects that seem more-or-less reasonable, but uses doubles in method calls where we would expect a Parent. What is the relationship bewtween a double and a Parent in this application?

The class Drawer:
The class will have array: Parent[] myShapes of size 10. The array will have object scope

this says : In the Drawer class , there will be and array of Parent Objects called myShapes, it will be declared as global.

so :

public class Drawer(){
    //global variables
    Parent[] myShapes = new Parent[10];
    //... other global variables

    //constuctor
    public Drawer( ...maybe some arguments... ){
        //assignement of the arguments to the corresponding global variables
    }

    //methods
    //...
    public void doSomething(){
    }
}

I think there may be a language problem here. Java does not have "global" variables, so if you use that term you can create ambiguity and confusion. The nearest Java gets is to declare variables as public static in a public class, although some people refer to public instance variables as "global" if their intent is that they should be shared. We, of course, know better and never share non-final variables with anyone :)

The statement "The array will have object scope" simply implies that the variable will be an instance variable, not a static or local variable. (Java Language Specification, section 6.3)

I think there may be a language problem here. Java does not have "global" variables, so if you use that term you can create ambiguity and confusion. The nearest Java gets is to declare variables as public static in a public class, although some people refer to public instance variables as "global" if their intent is that they should be shared. We, of course, know better and never share non-final variables with anyone :)

The statement "The array will have object scope" simply implies that the variable will be an instance variable, not a static or local variable. (Java Language Specification, section 6.3)

im sorry if the word "global" is not appropriate or if it's missleading, obviously we meant the same thing but back in school my teachers always refered to these variables as "global" so i just assumed it was standard.

Thank you to everyone who offered their assistance with this program. I really appreciate it. Unfortunately, my instructor decided to cancel the project altogether and assigned another in its place. Thank you again to everyone, you all really fueled my understanding for what I was trying to accomplish.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.