Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: I'm confused

  1. #1
    Member
    Join Date
    Feb 2012
    Posts
    43
    My Mood
    Confused
    Thanks
    33
    Thanked 0 Times in 0 Posts

    Default I'm confused

    I'm working on a program with a main class; ArrayDrawer and two subclasses; square and triangle. Here is what I've gotten so far;

    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 use of override and the abstract is based on an example in my book. However, the book also has a few other things that I'm not sure if I need, such as;

    public double getSide()
    {
    return side;
    }
    //and
     
    publicSquare()
    {
    super("Square");
    }
     
    public Square(double side)
    {
    super("Square");
    this.side = side;
    }

    I see them in the example, but I'm unsure if they are necessary for the code itself.

    I also looked up things on coordinates and saw that many people use something like
    public Point coord = new Point(0,0);
    coord = new Point(x,y);

    Would that be something that would work with my array problem? I've never seen anything done with it prior to now, so I'm not sure how it works nor how to implement it if it would prove useful.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,441
    Thanks
    49
    Thanked 2,217 Times in 2,190 Posts

    Default Re: I'm confused

    Can you explain your problem? Do you have any questions?
    Do you get compiler errors? If so, please copy and paste the full text here.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Wonderlandslost (March 2nd, 2012)

  4. #3
    Member
    Join Date
    Feb 2012
    Posts
    43
    My Mood
    Confused
    Thanks
    33
    Thanked 0 Times in 0 Posts

    Default Re: I'm confused

    The problem I'm trying to solve is creating an ArrayDrawer class that has the arrays square[] and triangle[] (as listed in code). I have to take those and add them into the first open position. I have code that does that from a problem I did before, which I think you helped me with. So, I tried to use that code for the findfirstEmpty and the add..

    the compiler errors I receive for this are as follows;

    /**
         * 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();
     
        }
    on the prints;
    cannot find symbol
    symbol: method print()
    location: variable triangles of type arraydrawer.ArrayDrawer.Triangle[]

    ArrayDrawer ar1 = new ArrayDrawer();
    (in the main method)
    arraydrawer.ArrayDrawer is abstract; cannot be instantiated

    Surround with ...
    (I've never seen this before...)


    As for the point things that I mentioned in the original post; the problem requires me to add in coordinates for the shape. I've never done something like that so I was looking at different methods of doing so and noticed that Point could be used to add in coordinates. I just wasn't sure if that was helpful for this problem. Would it make sense to implement that?
    As for adding things to the array, I'm not sure how to add multiple items to a single spot in the array. I've never done so before, so I think I'm going about doing it wrong. From what I understand, doing something like;

    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");
            }
     
        }
    wont work because it's in the ArrayDrawer class, not the triangle class. However, if I change the beginning to double, I get an error on the part where it sets the array stating that there are incompatible types. I've seen some code like;
    public addTriangle(double base, double height....)
    with all the things I need, but I wasn't sure how to go about using something like that and putting my items in a single slot in the array.
    (sorry if that seemed jumbled)

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,441
    Thanks
    49
    Thanked 2,217 Times in 2,190 Posts

    Default Re: I'm confused

    cannot find symbol
    symbol: method print()
    Where is the method: print defined? The compiler can not find its definition.

    For the other error messages, please copy the full text of the error message. What you posted doesn't have all the information needed to find the error.

  6. The Following User Says Thank You to Norm For This Useful Post:

    Wonderlandslost (March 2nd, 2012)

  7. #5
    Member
    Join Date
    Feb 2012
    Posts
    43
    My Mood
    Confused
    Thanks
    33
    Thanked 0 Times in 0 Posts

    Default Re: I'm confused

    arraydrawer.ArrayDrawer is abstract; cannot be instantiated

    Surround with ...
    That is what the compiler I'm using is telling me as the error for;
    public static void main(String[] args)
        {
            ArrayDrawer ar1 = new ArrayDrawer();
    }

    as for the print method, I've tried something like;
    /**
         * 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.print();
                }
            }
     
            /**
             * Print out the Triangle array
             */
            for (Triangle tr: triangles)
            {
                if (tr != null)
                {
                    tr.print();
                }
            }       
        }
    Which actually worked out

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,441
    Thanks
    49
    Thanked 2,217 Times in 2,190 Posts

    Default Re: I'm confused

    Did you solve your problem now?
    I don't see where you have posted any error messages or any new questions.

  9. #7
    Member
    Join Date
    Feb 2012
    Posts
    43
    My Mood
    Confused
    Thanks
    33
    Thanked 0 Times in 0 Posts

    Default Re: I'm confused

    I solved the print portion of it, yes
    I am curious about abstract though. I'm using the abstract method ArrayDrawer for this program (Since my book showed using that for a similar example). Yet when I go to use it in the main;

     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 get those as errors. At least, that's what netbeans is telling me. I don't understand what an abstract does or why it is causing that problem.

  10. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,441
    Thanks
    49
    Thanked 2,217 Times in 2,190 Posts

    Default Re: I'm confused

    Cross posted at I just keep getting stuck.. - Java | DaniWeb
    Why are you posting this same problem on two forums?

    Choose one and continue there.

Similar Threads

  1. help me , im confused!!!!
    By sephskie in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 11th, 2011, 10:52 AM
  2. Confused..
    By jadowers in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 5th, 2011, 12:56 PM
  3. [SOLVED] confused
    By joon in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 12th, 2011, 11:40 AM
  4. I'm confused...
    By acolar in forum Object Oriented Programming
    Replies: 1
    Last Post: April 14th, 2011, 12:14 PM
  5. Confusion with C/C++
    By Eric in forum Java Applets
    Replies: 0
    Last Post: December 22nd, 2008, 01:18 PM

Tags for this Thread