Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design a Java program that asks the user to enter an integer number n and then g

ID: 3777890 • Letter: D

Question

Design a Java program that asks the user to enter an integer number n and then generates an array of that many random points (x, y) with x- and y-coordinates in the range between 0 and 100. After this the program must ask the user to enter two diagonal points (x_1, y_1) and (x_2, y_2) of a rectangle with sides parallel to the coordinate axis (see the image below, where possible pairs of diagonal points are shown in red color). The program then prints the coordinates of generated points which are within that rectangle. Aake sure that your program works for both pairs of the diagonal points specifying the rectangle. Use Java API class Point to handle the points.

Explanation / Answer

class ArrayDemo {

    public static void main(String[] args) {

        // declares an array of integers

        int[] anArray;

        // allocates memory for 10 integers

        anArray = new int[10];

          

        // initialize first element

        anArray[0] = 100;

        // initialize second element

        anArray[1] = 200;

        // and so forth

        anArray[2] = 300;

        anArray[3] = 400;

        anArray[4] = 500;

        anArray[5] = 600;

        anArray[6] = 700;

        anArray[7] = 800;

        anArray[8] = 900;

        anArray[9] = 1000;

        System.out.println("Element at index 0: "

                           + anArray[0]);

        System.out.println("Element at index 1: "

                           + anArray[1]);

        System.out.println("Element at index 2: "

                           + anArray[2]);

        System.out.println("Element at index 3: "

                           + anArray[3]);

        System.out.println("Element at index 4: "

                           + anArray[4]);

        System.out.println("Element at index 5: "

                           + anArray[5]);

        System.out.println("Element at index 6: "

                           + anArray[6]);

        System.out.println("Element at index 7: "

                           + anArray[7]);

        System.out.println("Element at index 8: "

                           + anArray[8]);

        System.out.println("Element at index 9: "

                           + anArray[9]);

    }

}