Free Response Practice Question (with solution)- Point 2D Array


<-- Back to Square Function Matrix Home to Additional FRQ Practice-->





Point 2D Array


Part 1: Point.java

Write the complete class definition for the Point class. The Point class consists of the following:


Additionally,


Part 2: Point2DArray.java

Write the complete class definition for the Point2DArray class. The Point2DArray class consists of a main() method that does the following:


Note: Points on axes (x-axis or y-axis) and the origin does not belong to any quadrant

Use the information given in image below to find the quadrant of a point.


Solution:

View Solution

Point.java

public class Point {

    private int x;
    private int y;
    
    public Point()
    {
        this.x = 0;
        this.y = 0;
    }
    
    public Point(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
    
    public int getX()
    {
        return x;
    }
    
    public int getY()
    {
        return y;
    }
    
    public String toString()
    {
        return "("+this.x+","+this.y+")";
    }
    
    public String getQuadrant()
    {
        String quad = null;
        if ((this.x ==0) || (this.y ==0)) quad = "0";
        else
        if ((this.x >0) && (this.y >0)) quad = "I";
        else
        if ((this.x < 0) && (this.y > 0)) quad = "II";
        else
        if ((this.x < 0) && (this.y < 0)) quad = "III";
        else
        if ((this.x > 0) && (this.y < 0)) quad = "IV";
        return quad;
    }
}

Point2DArray.java

import java.util.InputMismatchException;
import java.util.Scanner;

public class Point2DArray {

    public static void main(String args[])
    {
        int ROW = 3;
        int COL = 3;
        Point[][] pointA = new Point[ROW][COL];
        
        try (Scanner input = new Scanner(System.in)) {
        int x=0;
        int y=0;
        System.out.println("Enter the values of point objects: ");
        for (int i=0; i< ROW ; i++)
        {
            for (int j=0; j < COL; j++)
            {
                System.out.print("Enter the x coordinate of ["+ i +"] ["+j+"] point: ");
                x= input.nextInt();
                System.out.print("Enter the y coordinate of ["+ i +"] ["+j+"] point: ");
                y= input.nextInt();
                pointA[i][j] = new Point(x,y);
            }
        }
            
        System.out.println("Point objects in the matrix are: ");
        int count0=0;
        int count1=0;
        int count2=0;
        int count3=0;
        int count4=0;
        String quad=null;
        for (int k=0; k < ROW; k++)
        {
            for (int j=0; j < COL; j++)
            {
                System.out.print(pointA[k][j]+ " ");
                quad = pointA[k][j].getQuadrant();
                if (quad.equals("0")) count0++;
                else
                    if (quad.equals("I")) count1++;
                    else
                        if (quad.equals("II")) count2++;
                        else
                            if (quad.equals("III")) count3++;
                            else
                                if (quad.equals("IV")) count4++;
            }
            System.out.println();
        }

        System.out.println("The number of points is Quad I are: "+ count1);
        System.out.println("The number of points is Quad II are: "+ count2);
        System.out.println("The number of points is Quad III are: "+ count3);
        System.out.println("The number of points is Quad IV are: "+ count4);
        System.out.println("The number of points which do not belong to any quadrant are: "+ count0);
        
        } catch (InputMismatchException  e)
         {
            System.out.println("Error reading input");
         }
    }
}



Sample Output:

View Output



Java project files (with Runner code):


<-- Back to Square Function Matrix Home to Additional FRQ Practice-->