Hello! My friend recommended that I try this site if I needed advice on my programs, so I thought 'hey, why not?'. I'm working on a program that is to draw shapes and store them in arrays. I kinda can't figure out how to make my print functions work correctly. I also don't know what I'm doing wrong when trying to add in the information. Any advice on what to do next would be absolutely wonderful!

package arraydrawer;

 * Description: 
 *      This program takes input and puts it into an the shape array specified.
 *      The two shapes available are Triangle and Square.  The area is calculated and
 *      the user recieves a print out of the designated tasks the program does with
 *      the array information.
 */
public class ArrayDrawer
{
    //Global Data
    protected double coordx;
    protected double coordy;
    /**
     * Create arrays with object score to store the squares in
     * First array will store the side information 
     * Second array with store the area
     * Third array will store the coordinates
     */
    protected Square[] squares = new Square[10];
    
    /**
    * Create arrays with object score to store the triangles in
    * First array will hold the base information
    * Second array will store the area information
    * Third array will store the height information
    * Fourth array will store the coordinates
    */
    protected Triangle[] triangles = new Triangle[10];
    
    protected double[] x;
    protected double[] y;
  
    
     /*
     * 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 s)
    {
        /**
         * Use findFirstEmptySquare to locate the first null spot in the square array
         */ 
        int i = findFirstEmptySquare();
        /**
         * Take the empty spot and place the square in it
         */
        if (i >= 0)
        {
            squares[i] = s;
        }
        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.
     */
    public 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 i = 0; i <= squares.length; i++)
            //Take the filled seat and remove the child
             if (p == i)
             {
                  //Removes a specific triangle from the bus array
                  squares[i] = 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 b)
    {
        /**
         * Use findFirstEmptySquare to locate the first null spot in the triangle array
         */
        int t = findFirstEmptyTriangle();
       /**
         * Take the empty spot and place the square in it
         */
        if (t >= 0)
        {
               triangles[t] = b; 
        }
        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.
     */
    public 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 j = 0; j <= triangles.length; j++)
        //Take the filled seat and remove the child
         if (p == j)
         {
              //Removes a specific triangle from the bus array
              triangles[j] = 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
         */
        for(Square sq: squares)
        {
            if (sq != null)
            {
                sq.squarePrint();
            }
        }
        /**
         * Print out the Triangle array
         */
        for (Triangle tr: triangles)
        {
            if (tr != null)
            {
                tr.trianglePrint();
            }
        }       
    }
  
    public static void main(String[] args)
    {
        ArrayDrawer ar1 = new ArrayDrawer();
        
        double base, height, coordx, coordy, side;
        Square s;
        Triangle b;
        Square sq = new Square();
        Triangle tr = new Triangle();
        
        /**
         * Create a triangle with the following information:
         * Base = 1
         * Height = 2
         * Coords(3,4)
         */

         base = 1;
         height = 2;
         coordx = 3;
         coordy = 4;
         tr.setBase(base);
         tr.setHeight(height);
         tr.setCoord(coordx, coordy);
         tr.trianglePrint();
         
             
        /**
         * Create a triangle with the following information:
         * Base = 5
         * Height = 6
         * Coords(7,8)
         */

         base = 5;
         height = 6;
         coordx = 3;
         coordy = 4;
        
        /**
         * Create a square with the following information:
         * Size = 9
         * Coords(10,11)
         */
      //   addSquare rInfo1 = new addSquare(0, {10, 11});
         //s = 0;
         side = 0;
         coordx = 10;
         coordy = 11;
         
        /**
         * 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: ");
        ar1.print();

        /**
         * Create a triangle with the following information:
         * Base = 12
         * Height = 13
         * Coords(14,15)
         */
        base = 12;
        height = 13;
        coordx = 14;
        coordy = 15;
                
        /**
         * Delete the triangle in position 1
         */
        ar1.deleteTriangle(0);
        
        /**
         *  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:");
        ar1.print();
        
        /**
         * Create a triangle with the following information:
         * Base = 16
         * Height = 17
         * Coords(18,19)
         */
        base = 16;
        height = 17;
        coordx = 18;
        coordy = 19;
                
        /**
         *  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: ");
        ar1.print();
        
        /**
         * Create a triangle with the following information:
         * Base = 20
         * Height = 21
         * Coords(22,23)
         */
         //b = 20;
        base = 20;
        height = 21;
        coordx = 22;
        coordy = 23;
               
        /**
         * Create a square with the following information:
         * Size = 24
         * Coords(25,26)
         */
        //s = 24;
        side = 24;
        coordx = 25;
        coordy = 26; 
        
         /**
         *  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: ");
        ar1.print();
        
        /**
         * Change the coordinates of all objects to (-1,-1)
         */
        coordx = -1;
        coordy = -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: ");
        ar1.print();
    }
}
/*
 * Description:
 * This holds all the information/functions that the square class is capable
 * of enacting
 */
package arraydrawer;

public class Square extends ArrayDrawer
{
    
    //Data fields
//    protected double side = 0;
//    protected double area = 0;
//    protected double coordx = 0;
//    protected double coordy = 0;
    
    protected double[] sqSide = new double[10];
    protected double[] sqArea = new double[10];
    protected double[] sqCoordx = new double[10];
    protected double[] sqCoordy = new double[10];
    
  
    
    public double setSide(double s)
    {
        int c = findFirstEmptySquare();
        if (c >= 0)
        {
            sqSide[c] = s;
        }      
        return s;
    }
    
    /**
     * 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(double side)
    {
        int z = findFirstEmptySquare();
        if (z >= 0)
        {
            sqArea[z] = side * side;
        }
        return side;
    }
   
    
    /*
     * 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)
      {
          int e = findFirstEmptySquare();
          if (e >= 0)
          {
              sqCoordx[e] = x;
              sqCoordy[e] = y;
          }
      }
     
       /**
        * Description: Prints out the information located in the square array
        * Precondition: There is something residing in the square array
        * Postcondition: N/A
        */
       public void squarePrint()
       {
           for (int r = 0; r < 10; r++)
           {
              if (squares[r] != null)
              {
                  System.out.print("Location in array is: " + r);
                  System.out.println("Side: " + squares[r] + " Area: " + sqArea[r] + " Coordinates: "
                          + sqCoordx[r] + " , " + sqCoordy[r]);
              }
           }
       } 
       
       public void setSquare(double s)
       {
           int a = findFirstEmptySquare();
           if (a >= 0)
           {
               ArrayDrawer.addSquare(sqSide[a]);
               ArrayDrawer.addSquare(sqArea[a]);
               ArrayDrawer.addSquare(sqCoordx[a]);
               ArrayDrawer.addSquare(sqCoordy[a]);
                       
           }
       }
       
}

Oh, the parts that are giving me trouble are the parts under the Square class, under SetSquare(and how to get that information there :/). As well as the print function there.

Thank you for any advice you can give!!! =)

Try something like this, it seems to make a little more sense to me. Remove the declaration of the four arrays used here from the package arraydrawer and create them global and static in class ArrayDrawer. Hopefully this will at least get you going on setSquare.

public void setSquare(double s)
       {
           int a = findFirstEmptySquare();
           if (a >= 0)
           {	
               ArrayDrawer.sqSide[a] = s;
               ArrayDrawer.sqArea[a] = s;
               ArrayDrawer.sqCoordx[a] = s;
               ArrayDrawer.sqCoordy[a] = s;
            }
       }

Try something like this, it seems to make a little more sense to me. Remove the declaration of the four arrays used here from the package arraydrawer and create them global and static in class ArrayDrawer. Hopefully this will at least get you going on setSquare.

public void setSquare(double s)
       {
           int a = findFirstEmptySquare();
           if (a >= 0)
           {	
               ArrayDrawer.sqSide[a] = s;
               ArrayDrawer.sqArea[a] = s;
               ArrayDrawer.sqCoordx[a] = s;
               ArrayDrawer.sqCoordy[a] = s;
            }
       }

Oh dear! I meant to put this;

public void setSquare()
       {
           int a = findFirstEmptySquare();
           if (a >= 0)
           {	
               ArrayDrawer.sqSide[a] = s;
               ArrayDrawer.sqArea[a] = s;
               ArrayDrawer.sqCoordx[a] = s;
               ArrayDrawer.sqCoordy[a] = s;
            }
       }

Without the double s. My mistake! I'm supposed to be taking the arrays that I set up in square and put them into the Square[] squares array in the ArrayDrawer

This article has been dead for over six months. Start a new discussion instead.