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