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:
- int x: represents the x coordinate of a point.
- int y: represents the y coordinate of a point.
Additionally,
- The Point class contains a default constructor, that initializes a generic point with all value set to 0 (zero).
- The class also consists of a parameterised constructor Point (int x, int y), to initialize the x and y coordinates of a point to the given values.
- Accessor methods for all the properties of the Point class.
- A helper method (getQuadrant()) that returns a string depending on which quadrant the point lies in (I, II, III, IV).
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:
- Creates a 2D array of size 3X3 of point objects. [ You can either choose to take the point object coordinates as input from the user or initialise the array in the program itself]
- Use the helper method (getQuadrant()) of the Point class to calculate and print the number of points in each of the four quadrants. You must also print the number of points which are not in any quadrant.
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 SolutionPoint.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 OutputJava project files (with Runner code):
<-- Back to Square Function Matrix | Home to Additional FRQ Practice--> |