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 9 of 9

Thread: I'm not sure what I need to do next.

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

    Default How do I go from here?

    So my instructor assigned us to create a program that calculates the area and stores that, along with coordinates, in an array. This has to be done for both triangles and squares. He is having us use separate classes for the project; the main Drawer class, parent class, square class, and triangle class. I've got a portion of it done, but I'm honestly not sure where I need to go from here. I can't seem to figure out how to get the information read from the main part of the program into the methods that I have and I don't know if I need to add more methods to accomplish that. I'm also getting errors in what I do have;

     
    public class Drawer
    {
        //Declare array for shapes to be stored in
        public Parent myShape[] = new Parent[10];
     
     
         /*
         * Description: This method will allow the items to be entered into the array
         *  and will place them in the first empty location
         * Precondition: The  array must already exist and have been set to null.
         * Postcondition:  The item will reside in the array.
         */
        public double add(double z)
        {
            //Use the findFirstEmpty to locate the first null spot
            int x = findFirstEmpty();
            //Take the empty spot and put the item in it
            if (x >= 0)
            {
                myShape[x] = z;
            }
            else
            {
                //indicates that the array is full
                System.out.println("The array is at maximum capacity");
            }
            //Return the spot with the shape in it.
            return z;
        }
     
     
         /**
         * Description: This method will search for the first empty spot in the array 
         * Precondition: The  array must already exist and there must be at least
         *  one item in the array.
         * Postcondition: There will now be one less empty spot since an item will 
         *  be residing in the formerly empty location.
         */
        public int findFirstEmpty()
        {
            /**
             * Function of this method is to scan the array for the first null 
             * position  
             */    
            for (int i = 0; i < myShape.length; i++)
    	{
                /**
                 * Scan the array by using int i to correspond to the position of the 
                 * empty spot in the array.
                 */
                if (myShape[i] == null)
                {
                    //Indicates that the scan located a null spot in the array
                    return i; 
                }  
          	}
            //Indicates that the scan produced no null spots in array
            return -1; 
         }
     
         /**
         * Description: This method is when the bus stops and a child gets off, 
         *  freeing up a seat.
         * Precondition: The bus array must already exist and there must be at least
         *  one child on the bus.
         * Postcondition: There will be one more empty spot since the child that was
         *  residing in that spot got off at the bus stop.
         */
        public void delete(double z)
        {
            for (int x = 0; x < myShape.length; x++)
            //Take the filled seat and remove the child
             if (z.equals(myShape[x]))
             {
                  //Removes a specific person from the bus array
                  myShape[x] = null;  
             }    
        }
     
     
        /*
         * Description: This class holds the methods and other items that are 
         *  inherited by both of the children classes.
         * Precondition: The array must exist.
         * Postcondition:  The children classes will utilize the methods and items in
         *  this section to recieve desired results
         */
        class Parent
        {
            protected double coordx;
            protected double coordy;
     
            /*
             * Description: This method sets the coordinates for the object.
             * Precondition: The array must exist and coordinates must have already
             *  been entered in.
             * Postcondition:  The item in the array will have set coordinates.
             */
              public setCoord(double x, double y)
              {
                  coordx = x;
                  coordy = y;
              }
     
                 public void print()
            {
                //Print out the array and the position of the item in the array.  
     
                //Need to print coordx, coordy, side, and area.
     
        //        for (int x = 0; x < 10; x++)
        //        {
        //           if (names[x] != null)
        //           {
        //               System.out.println(x + " , " + names[x]);
        //           }
        //         }
     
            }
     
        }
     
        public class Square extends Parent
        {
            private double side = 0;
            private double area = 0;
     
            //Get side of square and return it
            public double getSide()
            {
                return side;
            }
            /**
             * Description: This method computes the area of the Square.
             * Precondition:  The sides must have already been entered into the array.
             * Postcondition:  There will be a resulting area.
             */       
            public double computeArea()
            {
               return area  = side * 4;
            }
     
     
     
        }
     
        public class Triangle extends Parent
        {
            private double base;
            private double height;
            private double area;
     
            //Get base of triangle and return it
            public double getBase()
            {
                return base;
            }
            //Get height of triangle and return it
            public double getHeight()
            {
                return height;
            }
     
            /**
             * Description: This method computes the area of the Triangle.
             * Precondition:  The sides must have already been entered into the array.
             * Postcondition:  There will be a resulting area.
             */     
            public double computeArea()
            {
                return   area = .5 * base * height;
            }
     
     
        }
     
        public static void main(String[] args) 
        {
            Drawer d1 = new Drawer();
     
            /**
             * Enter in the first shape; Triangle 
             * base = 10
             * height = 5.1
             * coordinates = (1,2)
             */
            double B = 10;
            double H = 5.1;
            double C1 = 1;                
            double C2 = 2;
     
     
            /**
             * Enter in the second shape; Triangle 
             * base = 3
             * height = 7
             * coordinates = (3,2)
             */
     
     
            /**
             * Enter in the third shape; Square 
             * size = 5
             * coordinates = (7,5)
             */
     
            //Print the array
     
            /**
             * Enter in the fourth shape; Triangle 
             * base = 20
             * height = 30
             * coordinates = (20,2)
             */
     
            //Delete object in array position 1
     
            //Print the array
     
            /**
             * Enter in the fifth shape; Triangle 
             * base = 100
             * height = 35
             * coordinates = (1,20)
             */
     
            /**
             * Enter in the sixth shape; Triangle 
             * base = 25
             * height = 50
             * coordinates = (1,22)
             */
     
            //Print the array
     
            //Change all of the coordinates to (-1,-1)
     
            //Print the array
        }
    }

    The array at the beginning is giving the error; exporting non-public type through public API. However, that's the method my teacher instructed us to use the array.

    I am also getting an error of incompatible types in the add() method (the myShapes[x] = z), and I'm unsure why. It is the same with the delete method, which states that it cannot be dereferenced.

    I have the basics of what I need to be doing for this program, I just keep getting stuck. I would appreciate being pointed in the right direction, and I apologize in advanced at the ignorance I have when it comes to some of this stuff(I will likely ask stupid questions). Thank you!
    Last edited by Wonderlandslost; February 27th, 2012 at 11:59 PM.

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

    Default Re: I'm not sure what I need to do next.

    Please post the full text of the error messages.

    incompatible types in the add() method
    You get that when the right side of an assignment statement is a different type from the left side.
    For example:
    String aStr = 123; // String vs int are not compatible types

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

    Wonderlandslost (February 28th, 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 not sure what I need to do next.

      public double add(double z)
        {
            //Use the findFirstEmpty to locate the first null spot
            int x = findFirstEmpty();
            //Take the empty spot and put the item in it
            if (x >= 0)
            {
                myShape[x] = z;
            }
            else
            {
                //indicates that the array is full
                System.out.println("The array is at maximum capacity");
            }
            //Return the spot with the shape in it.
            return z;
        }

    Error received;
    incompatible types
    required: drawer.Drawer.Parent
    found: double

    Surround with ...

    I thought this would work, since I used this before with Strings to place a 'name' into the array position X

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

    Default Re: I'm not sure what I need to do next.

    Do you understand what the compiler is complaining about?
    A double value is not an object of type Parent.

    What datatype is z?
    What datatype is the myShape array?

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

    Default Re: I'm not sure what I need to do next.

    I can't figure out what datatype myShape is. I've never declared an array without saying 'string' or 'int'...
    As for the datatype of z, it's a double.

    In this case, would my array be an int since I'm setting it to int x?

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,353
    Thanks
    49
    Thanked 2,206 Times in 2,179 Posts

    Default Re: I'm not sure what I need to do next.

    I can't figure out what datatype myShape is
    Look at this line. It defines the myShape array to be of type Parent:
    Parent myShape[] = new Parent[10];

    Is your editor's Find function not working? If you edit your code and do a Find on myShape that line would be found.

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

    Default Re: I'm not sure what I need to do next.

    I honestly don't know at this point. I'm really lost in the program. I went through the book and added in things from the example that was provided, but I'm not sure..

     
     
    public class Drawer
    {
        //Global Data
     
        //Declare array for shapes to be stored in.
        protected Parent myShape[] = new Parent[10];
     
         /*
         * Description: This method will allow the items to be entered into the array
         *  and will place them in the first empty location
         * Precondition: The  array must already exist and have been set to null.
         * Postcondition:  The item will reside in the array.
         */
        public double add(double z)
        {
            //Use the findFirstEmpty to locate the first null spot
            int x = findFirstEmpty();
            //Take the empty spot and put the item in it
            if (x >= 0)
            {
                myShape[x] = z;  
            }
            else
            {
                //indicates that the array is full
                System.out.println("The array is at maximum capacity");
            }
            //Return the spot with the shape in it.
            return z;
        }
     
     
         /**
         * Description: This method will search for the first empty spot in the array 
         * Precondition: The  array must already exist and there must be at least
         *  one item in the array.
         * Postcondition: There will now be one less empty spot since an item will 
         *  be residing in the formerly empty location.
         */
        public int findFirstEmpty()
        {
            /**
             * Function of this method is to scan the array for the first null 
             * position  
             */    
            for (int i = 0; i < myShape.length; i++)
    	{
                /**
                 * Scan the array by using int i to correspond to the position of the 
                 * empty spot in the array.
                 */
                if (myShape[i] == null)
                {
                    //Indicates that the scan located a null spot in the array
                    return i; 
                }  
          	}
            //Indicates that the scan produced no null spots in array
            return -1; 
         }
     
         /**
         * Description: This method is when a item is removed from the array
         * Precondition: The  array must already exist and there must be at least
         *  item in the array.
         * Postcondition: There will be one more empty spot since the item that was
         *  residing in that was removed.
         */
        public void delete(double z)
        {
            for (int x = 0; x < myShape.length; x++)
            //Take the filled spot and remove the item
             if (z.equals(myShape[x]))  //ERROR DOUBLE CANNOT BE DEREFERENCED
             {
                  //Removes a specific item from the  array
                  myShape[x] = null;  
             }    
        }
     
     
        /*
         * Description: This class holds the methods and other items that are 
         *  inherited by both of the  classes.
         * Precondition: The array must exist.
         * Postcondition:  The  classes will utilize the methods and items in
         *  this section to recieve desired results
         */
        public abstract class Parent
        {
            //Initialize the coordinates
            protected double coordx;
            protected double coordy;
     
            /*
             * Name of the shape
             */
            private String shapeName = "";
     
            /**
             * Initializes the shape name
             */
            public Parent(String shapeName)
            {
                this.shapeName = shapeName;
            }
     
            /**
             * Gets the kind of shape
             */
            public String getShapeName()
            {
                return shapeName;
            }
     
            @Override
            public String toString()
            {
                return "Shape is a " + shapeName;
            }
     
            //Abstract methods
            public abstract double computeArea();
     
            /*
             * 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 double setCoord(double x, double y)
              {
                  coordx = x;
                  coordy = y;
              }
     
                 public void print()
            {
                //Print out the array and the position of the item in the array.  
     
                //Need to print coordx, coordy, side, and area.
            }
        }
     
        public class Square extends Parent
        {
            //Data fields
            private double side = 0;
            private double area = 0;
     
            //Constructor
            public Square()
            {
                super("Square");
            }
     
            /*
             * Creates a new square of with a specified size 
             */
            public Square(double side)
            {
                super("Square");
                this.side = side;
            }
     
            //Methods
            /*
             * Get the side
             */
            public double getSide()
            {
                return side;
            }
     
            /**
             * Description: This method computes the area of the Square.
             * Precondition:  The sides must have already been entered into the array.
             * Postcondition:  There will be a resulting area.
             */  
            @Override
            public double computeArea()
            {
               return area = side * side;
            }    
        }
     
        public class Triangle extends Parent
        {
            //Data fields
            private double base = 0;
            private double height = 0;
            private double area = 0;
     
            //Constructors
            public Triangle()
            {
                super("Triangle");
            }
     
            /**
             * Constructs a triangle of a specific size.
             */    
            public Triangle(double base, double height)
            {
                super("Triangle");
                this.base = base;
                this.height = height;
            }
     
            //Methods 
            /**
             * Get the height
             */
            public double getHeight()
            {
                return height;
            }
     
            /**
             * Get the base
             */
            public double getBase()
            {
                return base;
            }
     
            /**
             * 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;
            }  
        }
     
        public static void main(String[] args) 
        {
            Drawer d1 = new Drawer();
     
            /**
             * Enter in the first shape; Triangle 
             * base = 10
             * height = 5.1
             * coordinates = (1,2)
             */
     
     
            /**
             * Enter in the second shape; Triangle 
             * base = 3
             * height = 7
             * coordinates = (3,2)
             */
     
     
            /**
             * Enter in the third shape; Square 
             * size = 5
             * coordinates = (7,5)
             */
     
            //Print the array
     
            /**
             * Enter in the fourth shape; Triangle 
             * base = 20
             * height = 30
             * coordinates = (20,2)
             */
     
            //Delete object in array position 1
     
            //Print the array
     
            /**
             * Enter in the fifth shape; Triangle 
             * base = 100
             * height = 35
             * coordinates = (1,20)
             */
     
            /**
             * Enter in the sixth shape; Triangle 
             * base = 25
             * height = 50
             * coordinates = (1,22)
             */
     
            //Print the array
     
            //Change all of the coordinates to (-1,-1)
     
            //Print the array
        }
    }
    Last edited by Wonderlandslost; February 28th, 2012 at 09:19 PM.

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,353
    Thanks
    49
    Thanked 2,206 Times in 2,179 Posts

    Default Re: I'm not sure what I need to do next.

    Have you fixed the problem? Does the code compile now?

    What relationship is there between a double (the value in z) and a Parent object?
    Is there a double value in the Parent object you want to access or set?

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

    Wonderlandslost (March 1st, 2012)

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

    Default Re: I'm not sure what I need to do next.

    I'm still unsure. However, my instructor cancelled the project and assigned a new one in its stead. I will return to this project as soon as I can, just so I can understand it better, but as of this moment my time has to be spent on the other project.
    Thank you once more for your help, I really appreciate it.