12 Replies - 654 Views - Last Post: 03 March 2012 - 07:26 AM Rate Topic: -----

#1 wonderlandslost  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-March 12

inheritance/array problem, not sure what I'm doing

Posted 01 March 2012 - 10:20 PM

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.

Is This A Good Question/Topic? 0
  • +

Replies To: inheritance/array problem, not sure what I'm doing

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: inheritance/array problem, not sure what I'm doing

Posted 01 March 2012 - 10:33 PM

I'll stop you right away !!!
You completly missed the concept.

A class shouldn't know about its descendants... the classes that extend it. That is against all OO design.

So having an abstract class ArrayDrawer is OK but the class ArrayDrawer cannot have in its instance variables Square or Triangle.

A class Animal may exist. But a class Animal cannot have an array of Mammal[] and the class Mammal cannot have an array of Dog[] or an array of Cat[]

The whole principle behind inheritance is that the father class does not know anyting about its descendants.

With your desing, if you have a class A that extends class B that extends class C that extends class D a modification in class D may required a change in the code of class A. This is a NO ! No !

Re-think your architecture/design
Was This Post Helpful? 1
  • +
  • -

#3 wonderlandslost  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-March 12

Re: inheritance/array problem, not sure what I'm doing

Posted 01 March 2012 - 10:44 PM

View Postpbl, on 01 March 2012 - 10:33 PM, said:

I'll stop you right away !!!
You completly missed the concept.

A class shouldn't know about its descendants... the classes that extend it. That is against all OO design.

So having an abstract class ArrayDrawer is OK but the class ArrayDrawer cannot have in its instance variables Square or Triangle.

A class Animal may exist. But a class Animal cannot have an array of Mammal[] and the class Mammal cannot have an array of Dog[] or an array of Cat[]

The whole principle behind inheritance is that the father class does not know anyting about its descendants.

With your desing, if you have a class A that extends class B that extends class C that extends class D a modification in class D may required a change in the code of class A. This is a NO ! No !

Re-think your architecture/design


So, I want to make sure that I understand this correctly;
My ArrayDrawer class isn't supposed to know about the square nor the triangle class. So the arrays that I declared in ArrayDrawer shouldn't be there, correct? Since that would indicate that the parent class would know about the children classes. I'm not entirely sure if I'm understanding that properly. Here is a snippet from the directions I received;


This program will be part of a drawing program. The drawing program can draw several shapes of
different sizes.
1. The program will have class Square and class Triangle.
2. The class Square:
3. The class square will have attributes side and area and coordx, and coordy, to describe the
position in two dimensional plane.
4. It will have methods:
5. setCoord(double x, double y) //will set new coordinates (aka location) of the object.
6. computeArea(...) //will compute area
7. print(...) will print user friendly: coordx, coordy, side, area.
1. The class Triangle:
2. The class Triangle will have attributes base, height, and area and coordx, and coordy, to
describe the position in two dimensional plane.
3. It will have methods:
4. setCoord(double x, double y) //will set new coordinates (aka location) of the object.
5. computeArea(...) //will compute area
6. print(...) will print user friendly: coordx, coordy, base, height, area.
//continue
1. The class ArrayDrawer:
2. The class will have array: Square[] squares of size 10. The array will have object scope.
3. The class will have array: Triangle[] triangles of size 10. The array will have object scope.
4. The class ArrayDrawer will have methods addSquare(....) that will find first empty position and
add the specified object into first empty position of the array.
5. The class ArrayDrawer will have methods addTriangle(....) that will find first empty position
and add the specified object into first empty position of the array.
6. It will have methods deleteSquare(int p) that will delete the specified object in position p from
the array.
7. It will have methods deleteTriangle(int p) that will delete the specified object in position p from
the array.
8. It will have methods print(...) that will print all objects currently in both arrays, first all squares,
then all triangles.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: inheritance/array problem, not sure what I'm doing

Posted 01 March 2012 - 11:02 PM

Not to answer all your questions at once but you desserve a hint :)

class Polygone {
    int[] x;    // x coordinates
    int[] y;    // y coordinates

    Polygone(int[] x, int[] y) {
       this.x = x;
       this.y = y;
    }
}

class Rectangle extends Polygone {
   Rectangle(int[] x, int[] y) {     // receives arrays of [4] as parameters
      super(x,y);
   }
}

class Triangle extends Polygone {
   Triangle(int[] x, int[] y) {     // receives arrays of [3] as parameters
      super(x,y);
   }
}


Also does not make sense that a class holds an array of its instance unless really for that

class ArrayDrawerHolder {
    ArrayDrawer[] array;
}



Hope this helps
Was This Post Helpful? 1
  • +
  • -

#5 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: inheritance/array problem, not sure what I'm doing

Posted 01 March 2012 - 11:13 PM

You have a lot of questions, but I'll answer number 2 from the original post. You are calling print() on an array of squares, not the squares themselves. You can't do that in Java. You need to invoke methods on Objects. You might need to do something like this:
for(Square sq: squares) {
   if(sq!=null) // If there is no square, don't print
      sq.print();
}
And then the same for triangles. (In the print() method of your ArrayDrawer class).

As for your other problems with inheritance, there isn't a relationship between an ArrayDrawer and a Square or Triangle, so I don't see why there should be any inheritance in your code, unless you wanted to make a Shape class... but that's another story.

Also, how does this line work if ArrayDrawer is abstract? ArrayDrawer ar1 = new ArrayDrawer();
Was This Post Helpful? 1
  • +
  • -

#6 wonderlandslost  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-March 12

Re: inheritance/array problem, not sure what I'm doing

Posted 02 March 2012 - 09:18 AM

View PostSheph, on 01 March 2012 - 11:13 PM, said:

You have a lot of questions, but I'll answer number 2 from the original post. You are calling print() on an array of squares, not the squares themselves. You can't do that in Java. You need to invoke methods on Objects. You might need to do something like this:
for(Square sq: squares) {
   if(sq!=null) // If there is no square, don't print
      sq.print();
}
And then the same for triangles. (In the print() method of your ArrayDrawer class).

As for your other problems with inheritance, there isn't a relationship between an ArrayDrawer and a Square or Triangle, so I don't see why there should be any inheritance in your code, unless you wanted to make a Shape class... but that's another story.

Also, how does this line work if ArrayDrawer is abstract? ArrayDrawer ar1 = new ArrayDrawer();


Thank you so much for explaining those.
So for your first answer, that makes a lot of sense. Thank you, I'll try it out :)

Oh, okay. I was informed by the instructor that this was an inheritance problem.

As for the last part, I've never even seen abstract before. To be honest, I saw the way that an example did it in my book and tried that. I don't know how to use abstract, nor how to get a line like that to work in this situation.
Was This Post Helpful? 0
  • +
  • -

#7 wonderlandslost  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-March 12

Re: inheritance/array problem, not sure what I'm doing

Posted 02 March 2012 - 11:01 AM

View Postpbl, on 01 March 2012 - 11:02 PM, said:

Not to answer all your questions at once but you desserve a hint :)

class Polygone {
    int[] x;    // x coordinates
    int[] y;    // y coordinates

    Polygone(int[] x, int[] y) {
       this.x = x;
       this.y = y;
    }
}

class Rectangle extends Polygone {
   Rectangle(int[] x, int[] y) {     // receives arrays of [4] as parameters
      super(x,y);
   }
}

class Triangle extends Polygone {
   Triangle(int[] x, int[] y) {     // receives arrays of [3] as parameters
      super(x,y);
   }
}


Also does not make sense that a class holds an array of its instance unless really for that

class ArrayDrawerHolder {
    ArrayDrawer[] array;
}



Hope this helps

Thank you so much! That made a lot more sense. I applied it to the code but for some reason, in the super(x,y) section I'm getting the error;
Constructor ArrayDrawer in class arraydrawer.ArrayDrawer cannot be applied to given types;
required: no arguments
found: double[],double[]
reason: actual and formal argument lists differ in length

call to super must be first statement in constructor

I'm not exactly sure why nor what I did wrong. I followed the layout of what you did, since it made more sense, but I can't seem to find where I erred.

As for the last part of what you said, I'm a bit confused. Could you explain again please(sorry, trying to understand it).
Was This Post Helpful? 0
  • +
  • -

#8 wonderlandslost  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-March 12

Re: inheritance/array problem, not sure what I'm doing

Posted 02 March 2012 - 11:15 AM

[quote name='wonderlandslost' date='02 March 2012 - 09:18 AM' timestamp='1330705128' post='1565951']

View PostSheph, on 01 March 2012 - 11:13 PM, said:

You have a lot of questions, but I'll answer number 2 from the original post. You are calling print() on an array of squares, not the squares themselves. You can't do that in Java. You need to invoke methods on Objects. You might need to do something like this:
for(Square sq: squares) {
   if(sq!=null) // If there is no square, don't print
      sq.print();
}
And then the same for triangles. (In the print() method of your ArrayDrawer class).

I implemented this and it isn't giving any errors. I just had one question; what exactly does Square sq: squares do? Is that like saying; String name = new names()? or something like that at least.
Was This Post Helpful? 0
  • +
  • -

#9 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: inheritance/array problem, not sure what I'm doing

Posted 02 March 2012 - 03:03 PM

That is an extended for loop. It's basically when you just want to loop through a collection of objects and perform something on each of them. It means, for each Square object in the array 'squares', (The square object for the iteration can be accessed with the identifier 'sq') It is basically the same thing as saying,
for(int i = 0; i < squares.length; i++) {
    squares[i].print();
}
As for the abstract class, an abstract class cannot be instantiated. You can't have an object of an abstract class. They are meant to be extended. Check out the oracle tutorial.

http://docs.oracle.c...I/abstract.html
Was This Post Helpful? 1
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: inheritance/array problem, not sure what I'm doing

Posted 02 March 2012 - 03:55 PM

Quote

Constructor ArrayDrawer in class arraydrawer.ArrayDrawer cannot be applied to given types;
required: no arguments
found: double[],double[]


means that your constructor of ArrayDrawer has no parameter something like

public ArrayDrawer() {

and you call super() with 2 double[] as parameters

super(x,y); where x and y are double[]
Was This Post Helpful? 1
  • +
  • -

#11 wonderlandslost  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-March 12

Re: inheritance/array problem, not sure what I'm doing

Posted 02 March 2012 - 09:03 PM

View PostSheph, on 02 March 2012 - 03:03 PM, said:

That is an extended for loop. It's basically when you just want to loop through a collection of objects and perform something on each of them. It means, for each Square object in the array 'squares', (The square object for the iteration can be accessed with the identifier 'sq') It is basically the same thing as saying,
for(int i = 0; i < squares.length; i++) {
    squares[i].print();
}
As for the abstract class, an abstract class cannot be instantiated. You can't have an object of an abstract class. They are meant to be extended. Check out the oracle tutorial.

http://docs.oracle.c...I/abstract.html


Wow! Thank you! That makes a lot more sense. And thanks so much for the link! I read through it. It really helped a lot :)

I really hope I'm not annoying you, but I have two more questions.

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

In the code above, for some reason it's giving me this on int x;
x => cannot access instance variable x from static context
I've never seen anything like that before so I'm not sure what exactly that means. I also don't know what to do with the int p that I was supposed to put in(according to the directions for the project). So, my question is; what exactly does that mean in this instance?

Hopefully this is my last question, but here;
 public static void main(String[] args)
    {
        ArrayDrawer ar1 = new ArrayDrawer();
        Triangle tri = new Triangle();
        Square squ = new Square();
}

When trying to put the things into the array, I thought it would make most sense to do this. for some reason, both triangle and square are giving me this; "non-static variable this cannot be referenced from a static context"
I'm not sure if it has something to do with this, but it also is giving me trouble;
 public double 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");
        }
    }]
(I don't have a return statement yet, but I will shortly. This gives "incompatible types
required: arraydrawer.ArrayDrawer.Triangle
found: double" on triangles[x] = z;

I'm not sure if they are related, but I don't know why they are both happening. (sorry again, I'm trying to figure this out on my own, but it's not going so well..)
Thank you again for your help! It's definitely made a difference.
Was This Post Helpful? 0
  • +
  • -

#12 wonderlandslost  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-March 12

Re: inheritance/array problem, not sure what I'm doing

Posted 02 March 2012 - 09:13 PM

View Postpbl, on 02 March 2012 - 03:55 PM, said:

Quote

Constructor ArrayDrawer in class arraydrawer.ArrayDrawer cannot be applied to given types;
required: no arguments
found: double[],double[]


means that your constructor of ArrayDrawer has no parameter something like

public ArrayDrawer() {

and you call super() with 2 double[] as parameters

super(x,y); where x and y are double[]


Okay, so I'm really trying to understand this. I don't think I've quite got it, but I'm gonna give it a shot.

So from what I see, you mean something like;

protected double[] x;
    protected double[] y;
    
    
    public ArrayDrawer()
    {
        
    }
in my main class. Now I'm not sure what would go in the method. From what I read, it might be something like;

super.x = x;
        super.y = y;

But I tried that in my code, and it gave me an error on the first x and y. "cannot find symbol
symbol: variable x" I thought that declaring them outside of that would make them usable inside the method, or am I understanding that incorrectly?
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: inheritance/array problem, not sure what I'm doing

Posted 03 March 2012 - 07:26 AM

This does not make sense

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


what iterationg with x = 0, 1, 2, 3, ... until x equals p ?
You might want to check that p is not >= squares.length though


public void deleteSquare(int p)
    {
       if(p >= 0 && p < squares.length)
                  squares[x] = null;  
    }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1