patternjavaMinor
Reflecting and counting points on a 2D grid
Viewed 0 times
countinggridpointsreflectingand
Problem
First number indicates the number of points, followed by N points
(x,y). Then the next number indicates Q num_queries, followed by Q queries:
X axis. This query is represented as "X i j"
represented as "Y i j"
represented as "C i j"
Sample Input
Sample Output
How can i improve this code for better metrics?
```
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author cypronmaya
*/
class Point {
int x;
int y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
public class Quad_query {
private ArrayList arrayList;
public Quad_query(int capacity) {
this.arrayList = new ArrayList(capacity);
}
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
int num_of_points = scanner.nextInt();
Quad_query quad_query = new Quad_query(num_of_points);
for (int i = num_of_points; --i >= 0;) {
int x = scanner.nextInt();
int y = scanner.nextInt();
quad_query.add(x, y);
}
int num_of_queries = scanner.nextInt();
for (int i = num_of_queries; --i >= 0;) {
char c = scanner.next().charAt(0);
quad_query.query(c, scanner.nextInt() - 1, scanner.nextInt() - 1);
}
}
public void add(int x, int y) {
Point point = new Point(x, y);
arrayList.add(point);
}
public void query(char c, int i, int j) {
switch (c) {
case 'C':
queryC(i, j);
break;
default:
(x,y). Then the next number indicates Q num_queries, followed by Q queries:
- Reflect all points between point i and j both including along the
X axis. This query is represented as "X i j"
- Reflect all points between point i and j both including along the Y axis. This query is
represented as "Y i j"
- Count how many points between point i and j both including lie in each of the 4 quadrants. This query is
represented as "C i j"
Sample Input
4
1 1
-1 1
-1 -1
1 -1
5
C 1 4
X 2 4
C 3 4
Y 1 2
C 1 3Sample Output
1 1 1 1
1 1 0 0
0 2 0 1How can i improve this code for better metrics?
```
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author cypronmaya
*/
class Point {
int x;
int y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
public class Quad_query {
private ArrayList arrayList;
public Quad_query(int capacity) {
this.arrayList = new ArrayList(capacity);
}
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
int num_of_points = scanner.nextInt();
Quad_query quad_query = new Quad_query(num_of_points);
for (int i = num_of_points; --i >= 0;) {
int x = scanner.nextInt();
int y = scanner.nextInt();
quad_query.add(x, y);
}
int num_of_queries = scanner.nextInt();
for (int i = num_of_queries; --i >= 0;) {
char c = scanner.next().charAt(0);
quad_query.query(c, scanner.nextInt() - 1, scanner.nextInt() - 1);
}
}
public void add(int x, int y) {
Point point = new Point(x, y);
arrayList.add(point);
}
public void query(char c, int i, int j) {
switch (c) {
case 'C':
queryC(i, j);
break;
default:
Solution
Instead of initializing your arrayList when you create the instance of the class, do it when you have the num_of_points value from the scanner.
This will mean the array list doesn't need to resize itself (potentially slow operation). In the example you have given with only 4 points it won't make any difference, but if there were a larger number of points you may save some time!
Where you get the value from the scanner
In the
...that does nothing, so you can remove it.
A few notes on code style...
Hope this helps.
arrayList = new ArrayList(num_of_points);This will mean the array list doesn't need to resize itself (potentially slow operation). In the example you have given with only 4 points it won't make any difference, but if there were a larger number of points you may save some time!
Where you get the value from the scanner
char c you trim the value, which you shouldn't need to do as the Scanner should stop when it sees a delimiter (in this case, a space).In the
queryC method, you have a for loop...for (int b = 0; b < len; b++) {
pnt = arrayList.get(b);
}...that does nothing, so you can remove it.
A few notes on code style...
- In the main method your for loops go from X down to 0, but in the query methods you go from 0 up to X. Just wondering why?
- You also use
(0 - pnt.x)where you could use-pnt.x.
Hope this helps.
Code Snippets
arrayList = new ArrayList<Point>(num_of_points);for (int b = 0; b < len; b++) {
pnt = arrayList.get(b);
}Context
StackExchange Code Review Q#7383, answer score: 3
Revisions (0)
No revisions yet.