1.11M Members

I just keep getting stuck..

 
0
 

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.

 
0
 

your first problem:
in your ArrayDrawer, you have this method:

addTriangle(Triangle z)

but when you call it, you pass a double as parameter, not a Triangle. since a double is not a Triangle, your compiler can't process it.

for your print problems:

/**
         * Print out the Square array
         */
        squares.print();
 
        /**
         * Print out the Triangle array
         */
        triangles.print();

your error message does not say that your ArrayDraw doesn't contain a print class, it states that for your element squares there isn't one.

as for your third point, I have no idea what you're trying to ask. can you be a bit more clear?

 
0
 

your first problem:
in your ArrayDrawer, you have this method:

addTriangle(Triangle z)

but when you call it, you pass a double as parameter, not a Triangle. since a double is not a Triangle, your compiler can't process it.

Okay, that makes sense. But when I change that to something like 'double z' I get an error like this;

public void addTriangle(double 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");
        }

    }

"incompatible types
required: arraydrawer.ArrayDrawer.Triangle
found: double"
I was also wondering if the add method itself should be adding the many things I need to put in one slot of the array at the same time...something like;

public void addTriangle(double base, double height, double area, double coords..)
{
//find first empty....
//use if statement to pinpoint/add things to the first empty slot...
//add in base
//add in height
//add in coords
//add in area...

Or if something else would make sense. I was also wondering if Coords could actually be a Point (I've seen it done in examples)

 
0
 

Try changing it in the other place. Leave the method definition as it was and
change what you pass to the method to be a Triangle object.
Create a Triangle object from the variables: b,h, etc and pass that to the addTriangle method.

 
0
 

Try changing it in the other place. Leave the method definition as it was and
change what you pass to the method to be a Triangle object.
Create a Triangle object from the variables: b,h, etc and pass that to the addTriangle method.

I'm sorry if this is a stupid question, but you're referring to the addTriangle method correct?
So when I'm in the main method, I need to pass in a triangle object. How would I do that? Honestly, I don't get how to pass a Triangle object. I tried to pass the things into addTriangle as is and they aren't working.

 
0
 

So when I'm in the main method, I need to pass in a triangle object.

Yes.
Create a Triangle object: Triangle tri = new Triangle(....);
Pass it in the call: ari.addTriangle(tri);

 
0
 

Yes.
Create a Triangle object: Triangle tri = new Triangle(....);
Pass it in the call: ari.addTriangle(tri);

Okay, so I created the object.

public static void main(String[] args)
    {
        ArrayDrawer ar1 = new ArrayDrawer();
        /**
         * ERROR MESSAGE
         * arraydrawer.ArrayDrawer is abstract; cannot be instantiate
         */
        Triangle tri = new Triangle();
        /**
         * ERROR MESSAGE
         * non-static variable this cannot be referenced from a static context
         * 
         */

I don't know what that means at all. I'm sorry if I'm being annoying about this, by the way. I really just don't get it. :/ Thank you for your help!

 
0
 

non-static variable this cannot be referenced from a static context

I need to see the full text of the error messages from the compiler. Your edited version leaves out important information.

public abstract class ArrayDrawer

You can NOT create an instance of an abstract class. You need to extend the class with a new class and define methods as needed so you can create an instance of the class

How did you get this 400+ line source program?

You should have been compiling it as soon as you could and as often as you could to have resolved these errors long before you got to 400 lines.

 
0
 

I need to see the full text of the error messages from the compiler. Your edited version leaves out important information.


You can NOT create an instance of an abstract class. You need to extend the class with a new class and define methods as needed so you can create an instance of the class

How did you get this 400+ line source program?

You should have been compiling it as soon as you could and as often as you could to have resolved these errors long before you got to 400 lines.

Edited version? I copied an pasted exactly what it was telling me happened. So I didn't need the abstract at all? I thought I did because in my book, they never do anything without using an abstract class.
I was working on pieces of it at a time. A
And I'll stick to this forum, easier layout.

 
0
 

Remove the use of abstract.
Here is a sample of an error message from the compiler:

TestSorts.java:138: cannot find symbol
symbol  : variable var
location: class TestSorts
         var = 2;
         ^
 
0
 

Remove the use of abstract.
Here is a sample of an error message from the compiler:

TestSorts.java:138: cannot find symbol
symbol  : variable var
location: class TestSorts
         var = 2;
         ^

I did, so I ended up with this;

public static void main(String[] args)
    {
        ArrayDrawer ar1 = new ArrayDrawer();
        Triangle tri = new Triangle();
}
run:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - non-static variable this cannot be referenced from a static context
	at arraydrawer.ArrayDrawer.main(ArrayDrawer.java:336)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

And I think this is what you mean by error message...right?

 
0
 

That is a message from your IDE. It is NOT a message from the compiler.
If you were to use the javac command to compile your source you would get error messages that looked like the one I posted.

Your source code has errors that will not compile. I don't know why your IDE ignores them and tries to execute your program with those errors.
You need to fix the errors in your source before you can execute it.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article