Note that there are some explanatory texts on larger screens.

plurals
  1. PO.equals() not evaluating to proper boolean
    primarykey
    data
    text
    <pre><code>public class SparseMatrix { private final TreeMap&lt;Integer,TreeMap&lt;Integer,Double&gt;&gt; matrix; private final int rows; private final int cols; public SparseMatrix(int r, int c) { // Creates instances for matrix objects this.matrix = new TreeMap&lt;&gt;(); // Assigns the matrix a number of rows and a number of columns this.rows = r; this.cols = c; } public TreeMap&lt;Integer, TreeMap&lt;Integer, Double&gt;&gt; getMatrix() { return matrix; } public static boolean equals(SparseMatrix a, SparseMatrix b) { if (a.getMatrix().equals(b.getMatrix()) == true) { return true; } else { return false; } } } </code></pre> <p>Keys and values are inputted by the user then there's a test driver to make sure it works right, but it's not evaluating the matrices right. It tells me true every time. I'm assuming it's the == true and the equals comparator. It returns true if all the keys and values of both matrices are equal.</p> <p>"new" command: </p> <pre><code>if (cmd.equals("new")) { String name = input.next(); int rows = input.nextInt(); int cols = input.nextInt(); if (rows &lt; 1 || cols &lt; 1) { System.out.println("new: rows and/or cols less than 1: "); System.exit(1); } SparseMatrix m = new SparseMatrix(rows,cols); int i = input.nextInt(); while (i &gt;= 0) { int j = input.nextInt(); double v = input.nextDouble(); m.set(i,j,v); i = input.nextInt(); } matrix.put(name,m); System.out.printf("new %s = %s\n", name, m); } </code></pre> <p>The test driver part is:</p> <pre><code>String a = input.next(); if (!matrix.containsKey(a)) { System.out.println("equals: no such matrix: " + a); System.exit(1); } String b = input.next(); if (!matrix.containsKey(b)) { System.out.println("equals: no such matrix: " + b); System.exit(1); } System.out.printf("%s.equals(%s) = %b\n", a, b, SparseMatrix.equals(matrix.get(a),matrix.get(b))); </code></pre> <p>And the input looks like:</p> <pre><code>new name1 10 10 // new matrix with 10 rows and 10 columns 10 10 10.0 // int int double treemap -1 // stops the input </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload