Slowbro
Slowbro

Reputation: 191

Building a tester class?

I am a very new java user, and I have always had trouble with testing my programs. Here I have implement a basic boolean array interface. I want to test it buy adding a couple values and then running each of the interface methods. Please be nice I am not very good at this but I want to get better.

package booleanmatrix;

import java.util.Arrays;

/**
 *
 * @author David
 */
public class ArrayMatrix implements BooleanMatrixs {

    private boolean a[][];
    public int Rows;
    public int Cols;
    public int capacityr = 1;
    public int capacityc = 1;
    public int pT=0;
    public int pF=0;

    public ArrayMatrix() {
        a = new boolean[capacityr][capacityc];
    }



    @Override
    public int getNumberRows() {
        return capacityr;
    }

    @Override
    public int getNumberCols() {
        return capacityc;

    }

    @Override
    public void set(int row, int col) throws IndexOutOfBoundsException {

        if (row > capacityr) {
            capacityr = row;
            boolean B[][] = new boolean[capacityr][capacityc];
            for (int k = 0; k < capacityr; k++) {
                for (int j = 0; j < capacityc; j++) {
                    B[k][j] = a[k][j];
                }
            }
            a = B;
        }
        if (col > capacityc) {
            capacityc = col;
            boolean C[][] = new boolean[capacityr][capacityc];
            for (int k = 0; k < capacityr; k++) {
                for (int j = 0; j < capacityc; j++) {
                    C[k][j] = a[k][j];
                }
            }
            a = C;
        }

        a[row][col] = true;
        pT++;


    }

    @Override
    public void clear(int row, int col) throws IndexOutOfBoundsException {
        if (row > capacityr) {
            capacityr = row;
            boolean B[][] = new boolean[capacityr][capacityc];
            for (int k = 0; k < capacityr; k++) {
                for (int j = 0; j < capacityc; j++) {
                    B[k][j] = a[k][j];
                }
            }
            a = B;
        }
        if (col > capacityc) {
            capacityc = col;
            boolean C[][] = new boolean[capacityr][capacityc];
            for (int k = 0; k < capacityr; k++) {
                for (int j = 0; j < capacityc; j++) {
                    C[k][j] = a[k][j];
                }
            }
            a = C;
        }

        a[row][col] = false;




    }

    @Override
    public void set(int row, int col, boolean value) throws IndexOutOfBoundsException {
        if (row > capacityr) {
            capacityr = row;
            boolean B[][] = new boolean[capacityr][capacityc];
            for (int k = 0; k < capacityr; k++) {
                for (int j = 0; j < capacityc; j++) {
                    B[k][j] = a[k][j];
                }
            }
            a = B;
        }
        if (col > capacityc) {
            capacityc = col;
            boolean C[][] = new boolean[capacityr][capacityc];
            for (int k = 0; k < capacityr; k++) {
                for (int j = 0; j < capacityc; j++) {
                    C[k][j] = a[k][j];
                }
            }
            a = C;
        }

        a[row][col] = value;
        if(value==true){
            pT++;
        }

    }

    @Override
    public void toggle(int row, int col) throws IndexOutOfBoundsException {
        if (row > capacityr) {
            capacityr = row;
            boolean B[][] = new boolean[capacityr][capacityc];
            for (int k = 0; k < capacityr; k++) {
                for (int j = 0; j < capacityc; j++) {
                    B[k][j] = a[k][j];
                }
            }
            a = B;
        }
        if (col > capacityc) {
            capacityc = col;
            boolean C[][] = new boolean[capacityr][capacityc];
            for (int k = 0; k < capacityr; k++) {
                for (int j = 0; j < capacityc; j++) {
                    C[k][j] = a[k][j];
                }
            }
            a = C;
        }

        if (a[row][col] == true) {
            a[row][col] = false;
            pT--;
        } else {
            a[row][col] = true;
            pT++;

        }

    }

    @Override
    public void setAll() {
        Arrays.fill(a, Boolean.TRUE);
        pT=(capacityr*capacityc);

    }

    @Override
    public void clearAll() {
        Arrays.fill(a, Boolean.FALSE);
        pT=0;

    }

    @Override
    public void setAll(boolean value) {
        if (value == false) {
            Arrays.fill(a, Boolean.FALSE);
        } else {
            Arrays.fill(a, Boolean.TRUE);
             pT=(capacityr*capacityc);

        }



    }

    @Override
    public boolean get(int row, int col) throws IndexOutOfBoundsException {
        boolean x;
        x = a[row][col];
        return x;
    }

    @Override
    public int[][] getTruePositions() {

        int count = 0;



        int ret[][] = new int[pT][2];



        for (int k = 0; k < capacityr; k++) {
            for (int j = 0; j < capacityc; j++) {
                if (a[k][j] == true) {
                    ret[count][0] = k;
                    ret[count][1] = j;
                    count++;
                }

            }
        }
        return ret;
    }

    @Override
    public int[][] getFalsePositions() {
             int total = (capacityr*capacityc);
             int P=(total-pT);
             int count=0;



        int ret[][] = new int[P][2];



        for (int k = 0; k < capacityr; k++) {
            for (int j = 0; j < capacityc; j++) {
                if (a[k][j] == false) {
                    ret[count][0] = k;
                    ret[count][1] = j;
                    count++;
                }
            }
        }
        return ret;

    }

    @Override
    public int[][] getPositions(boolean value) {
               int total = (capacityr*capacityc);
             int P=(total-pT);
             int count=0;


if(value==false){
        int retf[][] = new int[P][2];



        for (int k = 0; k < capacityr; k++) {
            for (int j = 0; j < capacityc; j++) {
                if (a[k][j] == false) {
                    retf[count][0] = k;
                    retf[count][1] = j;
                    count++;
                }
            }
        }
        return retf;}
else{ 
    int count2 = 0;



        int ret[][] = new int[pT][2];



        for (int k = 0; k < capacityr; k++) {
            for (int j = 0; j < capacityc; j++) {
                if (a[k][j] == true) {
                    ret[count2][0] = k;
                    ret[count2][1] = j;
                    count2++;
                }

            }
        }
        return ret;

    }}

    @Override
    public int getNumberTrueValues() {
        return pT;

    }

    @Override
    public int getNumberFalseValues() {
          int total = (capacityr*capacityc);
             int P=(total-pT);
             return P;

    }

    @Override
    public int getNumberValues(boolean value) {
        if (value==false){
             int total = (capacityr*capacityc);
             int P=(total-pT);
             return P;
        }
        else{return pT;}}
    @Override
    public String toString(){
        String X= "1)The number of rows"+capacityr+"\n 2)The number of Columns"+capacityc+"\n 3)The number of True Values"+pT+"\n 4)The number of false values"+(capacityc*capacityr-pT);
        return X;
    }


     public static void main(String[] args) {









     }
}

Upvotes: 0

Views: 186

Answers (2)

Jon7
Jon7

Reputation: 7215

Take a look at this JUnit tutorial. It might be just a bit of overkill, but a good unit testing framework is usually the best way to test your code at this level.

Upvotes: 2

Tom
Tom

Reputation: 9643

Add a new class that has a Main method (if you are using eclipse there is an auto click button that does that for you) then instantiate a new variable from your class.

ArrayMatrix arrayVar = new ArrayMatrix();

Then you would be able to access all the methods such as:

arrayVar.getNumberCols();

Upvotes: 0

Related Questions