I want to use the command "java MagicSquare -t" to test the four following methods which are testIsFullSolution, testRject, testExtend, testNext:
-testIsFullSolution, a method that generates partial solutions and ensures that
the isFullSolution method correctly determines whether each is a complete
solution.
-testReject, a method that generates partial solutions and ensures that the reject method correctly determines whether each should be rejected.
-testExtend, a method that generates partial solutions and ensures that the
extend method correctly extends each with the correct next choice.
-testNext, a method that generates partial solutions and ensures that the, in
each, next method correctly changes the most recent choice that was added to
its next option.
I can successfully compile the program but when I run it, I got the following:
These should be full:
Full sol'n: [[8, 1, 6], [3, 5, 7], [4, 9, 2]]
These should NOT be full:
Full sol'n: [[8, 1, 6], [3, 5, 7], [4, 9, 0]]
These should NOT be rejected:
Not rejected: [[I@7f31245a, [I@6d6f6e28, [I@135fbaa4]
These should be rejected:
Not rejected: [[I@45ee12a7, [I@330bedb4, [I@2503dbd3]
These can NOT be extended:
Exception in thread "main" java.lang.NegativeArraySizeException
at cs445.a3.MagicSquare.extend(MagicSquare.java:111)
at cs445.a3.MagicSquare.testExtend(MagicSquare.java:236)
at cs445.a3.MagicSquare.main(MagicSquare.java:335)
And here is part of my code:
static void testIsFullSolution() {
int[][] fullSolutions = new int[][] {
{8, 1, 6},
{3, 5, 7},
{4, 9, 2},
};
int[][] notFullSolutions = new int[][] {
{8, 1, 6},
{3, 5, 7},
{4, 9, 0},
};
System.err.println("These should be full:");
if (isFullSolution(fullSolutions)) {
System.err.println("\tFull sol'n:\t" + Arrays.deepToString(fullSolutions));
} else {
System.err.println("\tNot full sol'n:\t" + Arrays.deepToString(fullSolutions));
}
System.err.println("These should NOT be full:");
if (isFullSolution(notFullSolutions)) {
System.err.println("\tFull sol'n:\t" + Arrays.deepToString(notFullSolutions));
} else {
System.err.println("\tNot full sol'n:\t" + Arrays.deepToString(notFullSolutions));
}
}
static void testReject() {
int[][] notRejected = new int[][] {
{8, 1, 6},
{3, 5, 7},
{4, 9, 2},
};
int[][] rejected = new int[][] {
{2, 0, 0},
{0, 4, 0},
{0, 0, 6},
};
System.err.println("These should NOT be rejected:");
if (reject(notRejected)) {
System.err.println("\tRejected:\t" + Arrays.toString(notRejected));
} else {
System.err.println("\tNot rejected:\t" + Arrays.toString(notRejected));
}
System.err.println("These should be rejected:");
if (reject(rejected)) {
System.err.println("\tRejected:\t" + Arrays.toString(rejected));
} else {
System.err.println("\tNot rejected:\t" + Arrays.toString(rejected));
}
static void testExtend() {
int [][] noExtend = new int [][] {
{8,1,6},
{3,5,7},
{4,9,2},
};
int [][] extend = new int [][] {
{8,1,6},
{3,5,7},
{4,9,0},
};
System.err.println("These can NOT be extended:");
System.err.println("\tExtended " + Arrays.toString(noExtend) + " to " + Arrays.toString(extend(noExtend)));
System.err.println("These can be extended:");
System.err.println("\tExtended " + Arrays.toString(extend) + " to " + Arrays.toString(extend));
}
static void testNext() {
int [][] noNext = new int [][] {
{8,1,6},
{3,5,7},
{4,9,2},
};
int [][] next = new int [][] {
{8,1,6},
{3,5,7},
{4,9,0},
};
System.err.println("These can NOT be next'd:");
System.err.println("\tNexted " + Arrays.toString(noNext) + " to " + Arrays.deepToString(next(noNext)));
System.err.println("These can be next'd:");
System.err.println("\tNexted " + Arrays.toString(next) + " to " + Arrays.deepToString(next(next)));
}
public static void main(String[] args) {
if (args.length >= 1 && args[0].equals("-t")) {
System.out.println("Running tests...");
testIsFullSolution();
testReject();
testExtend();
testNext();
} else if (args.length >= 2) {
try {
// First get the specified size
size = Integer.parseInt(args[0]);
// Then read the square from the file
int[][] square = readSquare(args[1]);
System.out.println("Starting square:");
printSquare(square);
findBack(square);
System.out.println("\nSolution:");
printSquare(solve(square));
} catch (NumberFormatException e) {
// This happens if the first argument isn't an int
System.err.println("First argument must be size");
} catch (FileNotFoundException e) {
// This happens if the second argument isn't an existing file
System.err.println("File " + args[1] + " not found");
}
} else if (args.length >= 1) {
try {
// First get the specified size
size = Integer.parseInt(args[0]);
// Then initialize to a blank square
int[][] square = new int[size][size];
System.out.println("Starting square:");
printSquare(square);
findBack(square);
System.out.println("\nSolution:");
printSquare(solve(square));
} catch (NumberFormatException e) {
// This happens if the first argument isn't an int
System.err.println("First argument must be size");
}
} else {
System.err.println("See usage in assignment description");
}
}
public static int[][] extend(int[][] square) {
// TODO: Complete this method
int[][] temp;
boolean getFlag = false;
temp = new int[size][size];
for (int i = 0; i < size; i++){
for (int j = 0; j < size; j++){
if (square[i][j] == 0 && !getFlag){
temp[i][j] = 1;
getFlag = true;
}
else{
temp[i][j] = square[i][j];
}
}
}
if (getFlag){
return temp;
}
else{
return null;
}
}
Aucun commentaire:
Enregistrer un commentaire