samedi 29 février 2020

How to test a cartesian coordinates input (int[][]) to a Java class in Sublime Text 3?

I'm running a correct algorithm for Max Points on a Line on Java using Sublime Text 3 "Command + B".

class Solution{
    int[][] points;
    int n;
    HashMap<Double, Integer> lines = new HashMap<Double, Integer>();
    int horizontalLines;

    public Pair<Integer, Integer> addLine(int i, int j, int count, int duplicates) {
        int x1 = points[i][0];
        int y1 = points[i][1];
        int x2 = points[j][0];
        int y2 = points[j][1];

        if ((x1 == x2) && y1 == y2) 
            duplicates++;
        else if (y1 == y2) {
            horizontalLines += 1;
            count = Math.max(horizontalLines, count);
        } else {
            double slope = 1.0 * (x1 - x2) / (y1 - y2) + 0.0;
            lines.put(slope, lines.getOrDefault(slope, 1) + 1);
            count = Math.max(lines.get(slope), count);
        }
        return new Pair(count, duplicates);
    }

    public int maxPointsWithPointI(int i) {
        lines.clear();
        horizontalLines = 1;
        int count = 1;
        int duplicates = 0;

        for (int j = i + 1; j < n; j++) {
            Pair<Integer, Integer> p = addLine(i, j, count, duplicates);
            count = p.getKey();
            duplicates = p.getValue();
        }
        return count + duplicates;
    }

    public int maxPoints(int[][] points) {
        this.points = points;
        n = points.length;
        if (n < 3) return n;

        int maxCount = 1;
        for (int i = 0; i < n - 1; i++)
            maxCount = Math.max(maxPointsWithPointI(i), maxCount); 
        return maxCount;
    }
}

Errors

b.java:4: error: cannot find symbol
    HashMap<Double, Integer> lines = new HashMap<Double, Integer>();
    ^
  symbol:   class HashMap
  location: class Solution
b.java:7: error: cannot find symbol
    public Pair<Integer, Integer> addLine(int i, int j, int count, int duplicates) {
           ^
  symbol:   class Pair
  location: class Solution
b.java:4: error: cannot find symbol
    HashMap<Double, Integer> lines = new HashMap<Double, Integer>();
                                         ^
  symbol:   class HashMap
  location: class Solution
b.java:23: error: cannot find symbol
        return new Pair(count, duplicates);
                   ^
  symbol:   class Pair
  location: class Solution
b.java:33: error: cannot find symbol
            Pair<Integer, Integer> p = addLine(i, j, count, duplicates);
            ^
  symbol:   class Pair
  location: class Solution
5 errors

I think maybe I should import some packages on the top of the file? What should I do?

Also, I'd like to test the algorithm using class b:

class b{
    public static void main(String[] args){
        Solution myTest = new Solution();
        System.out.println(myTest.maxPoints());
    }
}

How do I pass variables to maxPoints()?

Inputs and Outputs:

Input: [[1,1],[2,2],[3,3]]
Output: 3

Input: [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
Output: 4

$ which java:

/usr/bin/java

$ java -version

java version "1.8.0_60"
Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)

Sublime JavaC Build System

{
  "cmd": ["javac \"$file_name\" && java \"$file_base_name\""],
  "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
  "selector": "source.java",
  "shell": true,
  "quiet":true
}

Thanks! I'm new to Java.

Aucun commentaire:

Enregistrer un commentaire