Click here to Skip to main content
15,068,471 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Java
<pre>import java.util.Random;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
	    Shape array[] = new Shape[50];
	    Random rand = new Random();
        Scanner eisodos = new Scanner(System.in);

	    int i, rand_num, x, y, sum, p;

	    p = 0;
	    sum = 0;
	    for(i=0; i<50; i++){
            rand_num = rand.nextInt(2);
            if(rand_num == 1){
                System.out.println("Δώσε την περίμετρο του κύκλου: ");
                x = eisodos.nextInt();
                while(x<10 || x>50){
                    System.out.flush();
                    System.out.println("Δώσε την περίμετρο του κύκλου: ");
                    x = eisodos.nextInt();
                }
                array[i] = new Circle(x);
            }
            else {
                System.out.print("Δώσε την βάση: ");
                x = eisodos.nextInt();
                System.out.print("Δώσε το ύψος: ");
                y = eisodos.nextInt();
                while((x<10 || x >50) || (y<10 || y>50)){
                    System.out.flush();
                    System.out.print("Δώσε την βάση: ");
                    x = eisodos.nextInt();
                    System.out.print("Δώσε το ύψος: ");
                    y = eisodos.nextInt();
                }
                array[i] = new Rectangle(x, y);
            }
        }
    }
}


What I have tried:

I have this array, how to i check what object have in array?
Posted
Updated 29-Aug-21 7:28am

1 solution

You can use instanceof to check if an element in your array is specific class.
Have a look at Java instanceof Keyword[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900