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

Thread: Array of object

  1. #1
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Array of object

    Could someone explain to me how array of object work? Like in this code below, how does the program know when to use the second shapes in the shapes array? "new ShapesV8(7, 13)"

    class ShapesV8
    {
        //declaration of private instance variables
        private int mySide1, mySide2;
        private double myArea, myHypoteneuse;
     
        //constructor for ojbects of type ShapesV8
        ShapesV8(int s1, int s2)
        {
            mySide1 = s1;
            mySide2 = s2;
            myArea = 0.0;
            myHypoteneuse = 0.0;
        }
     
        //mutator method to calculate the area of a triangle
        public void calcTriArea()
        {
            myArea = mySide1 * mySide2 * .5;
        }
     
        //getter method to return the value of the area of a triangle
        public double getTriArea()
        {
            return myArea;
        }
     
        //mutator method to calculate the hypoteneuse of a triangle
        public void calcHypoteneuse()
        {
            myHypoteneuse = Math.sqrt(Math.pow(mySide1, 2) + Math.pow(mySide2, 2));
        }
     
        //getter method to return the value of the hypoteneuse of a triangle
        public double getHypoteneuse()
        {
            return myHypoteneuse;
        }
     
        //getter method to return the value of side 1 of a triangle
        public int getSide1()
        {
            return mySide1;
        }
     
        //getter method to return the value of side 2 of a triangle
        public int getSide2()
        {
            return mySide2;
        }
    }

    public class ShapesV8Tester
    {   
        public static void main(String[] args)
        {
            //declaration of variables
            int side1A, side2A, side1B, side2B; 
            double hypoteneuseA, triAreaA, hypoteneuseB, triAreaB;
     
            //initialization of array of objects
            ShapesV8[] shapes = {new ShapesV8(10, 5),
                                 new ShapesV8(7, 13)};
            //call methods
            for(int index = 0; index < shapes.length; index++)
            {
                shapes[index].calcTriArea();
                shapes[index].calcHypoteneuse();
            }
     
            //print results
            System.out.println("        Side 1    Side 2     Hypoteneuse       Area");
            for(int index = 0; index < shapes.length; index++)
            {
                System.out.printf("%12d %9d %14.1f %13.1f%n", 
                          shapes[index].getSide1(), shapes[index].getSide2(), 
                          shapes[index].getHypoteneuse(), shapes[index].getTriArea());
            }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Array of object

    how does the program know when to use the second shapes
    What does the program's comments and doc say about how it uses the contents of the array?
    Does it say what elements are in the slots of the array? What is in the second element?


    how array of object work?
    Just like any array.
    Use the .length attribute to get its length.
    Use array notation: arrayName[index]
    to access the element at index.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Array of object

    The elements of the array are use as int s1 and int s2 in the constructor. The program is to calculate the area of triangle. So one triangle have a side of 10 and 5 while the other triangle have a side of 7 and 13.- What I don't understand is how does the program know when to calculate the second triangle after the first. Thanks

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Array of object

    how does the program know when to calculate the second triangle after the first.
    If the program is calculating the two items, then the code must be called two times. Is there a loop that goes through an array?

    Try debugging the code by adding some println statements that print out messages as the code executes.
    The selection of what messages are printed and the order that the messages are printed will show you how the code is executing.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Array of object

    So since there is two items in the shapes array of object, the program automatically run through both of the item?

     ShapesV8[] shapes = {new ShapesV8(10, 5),
                                 new ShapesV8(7, 13)};

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Array of object

    So since there is two items in the shapes array of object, the program automatically run through both of the item?
    Yes.
    Look at the loop where the output is generated. See that it uses the .length attribute to control how many times to loop.
    If you don't understand my answer, don't ignore it, ask a question.

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

    maple1100 (January 20th, 2013)

  8. #7
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Array of object

    So just like a normal array, this array of object have .length of 4?

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Array of object

    All arrays have a .length whose value is determined by the number of slots in the array.

    this array of object have .length of 4?
    Add a println to print it out to see what its value is.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Array of object

    It say the length is 2. Does each new ShapesV8 count as one? Or each number count as one? Kind of lost here sorry. So is new ShapesV8(10, 5) one element and new ShapesV8(7, 13) another elements?
    ShapesV8[] shapes = {new ShapesV8(10, 5),
                                 new ShapesV8(7, 13)};

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Array of object

    each new ShapesV8 count as one
    yes. The contents of an object are not considered when counting the number of objects.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Sorting an object array using string variables from a string array
    By Shadud in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 26th, 2012, 05:50 PM
  2. Reading from ResultSet to Object and from object Object Array
    By anmaston in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 7th, 2011, 06:11 AM
  3. Object array with constructor
    By thedolphin13 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 8th, 2010, 11:02 AM
  4. Array object and constructors
    By TarunN in forum Collections and Generics
    Replies: 14
    Last Post: May 6th, 2010, 04:04 PM
  5. Object array to int array?
    By rsala004 in forum Collections and Generics
    Replies: 1
    Last Post: October 30th, 2009, 04:09 AM