# Array of object

• January 20th, 2013, 07:31 AM
maple1100
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)"

Code :

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

Code :

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

• January 20th, 2013, 08:00 AM
Norm
Re: Array of object
Quote:

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?

Quote:

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.
• January 20th, 2013, 08:07 AM
maple1100
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
• January 20th, 2013, 08:13 AM
Norm
Re: Array of object
Quote:

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.
• January 20th, 2013, 08:21 AM
maple1100
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?

Code :

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

• January 20th, 2013, 08:24 AM
Norm
Re: Array of object
Quote:

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.
• January 20th, 2013, 08:30 AM
maple1100
Re: Array of object
So just like a normal array, this array of object have .length of 4?
• January 20th, 2013, 08:59 AM
Norm
Re: Array of object
All arrays have a .length whose value is determined by the number of slots in the array.

Quote:

this array of object have .length of 4?
Add a println to print it out to see what its value is.
• January 20th, 2013, 09:11 AM
maple1100
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?
Code :

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

• January 20th, 2013, 09:13 AM
Norm
Re: Array of object
Quote:

each new ShapesV8 count as one
yes. The contents of an object are not considered when counting the number of objects.