0

I'm trying to print out an array of 10 objects. For some reason though, when I print out the array, there are anywhere from 15 to 22 elements in the array. I can't figure out why. Can someone please point me in the right direction?

import java.util.Random;

public class Main {
    public static void main( String[] args ) {

        Shape[] myShapes = new Shape[10]; //Array of 10 shapes
        Random rand = new Random(); //Random number generator
        int shape, x1, y1, x2, y2, x3, y3;
        double radius;

        for( int i = 0; i < 10; i++ ) {
            shape = rand.nextInt(3); //randomly select shape
            switch( shape ) {
                case 0: //Triangle
                    x1 = rand.nextInt(101);
                    y1 = rand.nextInt(101);
                    x2 = rand.nextInt(101);
                    y2 = rand.nextInt(101);
                    x3 = rand.nextInt(101);
                    y3 = rand.nextInt(101);
                    myShapes[i] = new Triangle( x1, y1, x2, y2, x3, y3 );
                    System.out.println("Triangle: " + myShapes[i].area());

                case 1: //Rectangle
                    x1 = rand.nextInt(101);
                    y1 = rand.nextInt(101);
                    x2 = rand.nextInt(101);
                    y2 = rand.nextInt(101);
                    myShapes[i] = new Rectangle( x1, y1, x2, y2 );
                    System.out.println("Rectangle: " + myShapes[i].area());

                case 2: //Circle
                    radius = rand.nextDouble()*100.0;
                    x1 = rand.nextInt(101);
                    y1 = rand.nextInt(101);
                    myShapes[i] = new Circle( radius, x1, y1 );
                    System.out.println("Circle: " + myShapes[i].area());
            }
        }
    }
3
  • You are not printing the array in this code. You have a loop of 10 iterations, and in each iteration you print a single line. Can you show the output you got? Commented Nov 13, 2014 at 8:42
  • You created an array of size 10. It can not contain more than 10 elements. Otherwise you would get an ArrayIndexOutOfBoundsException. Commented Nov 13, 2014 at 8:46
  • I dont think it will throw ArrayIndexOutOfBoundsException exception. But it will definitely print (System.out.println) more than 10 elements considering value of shape is not 2 always. Commented Nov 13, 2014 at 8:51

1 Answer 1

3

Can you please use break; for each case?

Your array actually has 10 elements. But it puts content in each elements up to three times - because control continues from case to case. Thus, if case 0 is right, it will put three shapes and print three prints. If case 1 is right, it will put two shapes and print two prints.

If you put break after each case, then on each iteration it will just put one shape and print just once.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.