import java.io.PrintStream; import java.util.Arrays; public class joker { public static int smallest(int[] v1, int[] v2) { return 0; } public static int[] convert1(String str) { return new int[1]; } public static String convert2(int[] v) { return ""; } public static void main(String[] args) { testSmallest(); testConvert(); } public static void testSmallest() { System.out.println("Testing your method smallest(...)"); int[][] testVectors1 = new int[][]{{1, 2, 3}, {1, 2, 3, 4}, {1, 2, 3}, {1, 2, 3}, {2, 3, 4}}; int[][] testVectors2 = new int[][]{{1, 2, 3, 4}, {1, 2, 3}, {1, 2, 3}, {2, 3, 4}, {1, 2, 3}}; int[] expectedOutcomes = new int[]{1, 2, 0, 1, 2}; if (expectedOutcomes.length != testVectors2.length || expectedOutcomes.length != testVectors1.length || testVectors1.length != testVectors2.length) { System.out.println("All tables must have the same # of tests"); System.exit(-1); } int nTests = expectedOutcomes.length; for(int test = 0; test < nTests; ++test) { int observedOutcome = smallest(testVectors1[test], testVectors2[test]); PrintStream var10000 = System.out; Arrays.toString(testVectors1[test]).print(Arrays.toString(testVectors2[test])); if (observedOutcome == expectedOutcomes[test]) { System.out.println(" SUCCEEDED"); } else { System.out.println(" FAILED"); } } } public static void testConvert() { String[] strings = new String[]{"", "0", "9", "12", "123"}; int[][] vectors = new int[][]{new int[0], {0}, {9}, {1, 2}, {1, 2, 3}}; System.out.println("\nTesting your method convert1(...)"); if (vectors.length != strings.length) { System.out.println("All tables must have the same # of tests"); System.exit(-1); } PrintStream var10000; int test; for(test = 0; test < strings.length; ++test) { int[] observed = convert1(strings[test]); var10000 = System.out; test.print(strings[test]); System.out.println(Arrays.equals(observed, vectors[test]) ? "SUCCEEDED" : "FAILED"); } System.out.println("\nTesting your method convert2(...)"); for(test = 0; test < vectors.length; ++test) { String observed = convert2(vectors[test]); var10000 = System.out; test.print(Arrays.toString(vectors[test])); System.out.println(observed.equals(strings[test]) ? "SUCCEEDED" : "FAILED"); } } }
/** * This method returns 1 if vector v1 is the "smallest", 2 if it is vector v2, * or 0 otherwise. We define "smallest" as follows; If one of the vector has * fewer elements than the other, it is the smallest one. If both are the same * size, then we look at every element one by one in order. As soon as one of * the two vectors has an element that is < than the corresponding element from * the other vector, then it is recognized as the "smallest" one by our method. **/ public static int smallest(int[] v1, int[] v2) { if(v1.length < v2.length) { return 1; } else if(v1.length > v2.length) { return 2; } else { for(int i=0; i<v1.length; i++) { if(v1[i] < v2[i]) { return 1; } else if(v1[i] > v2[i]) { return 2; } } } return 0; // always returns 0 for now, replace this with your code }// end method smallest /** * This method takes a string that is guaranteed to be made only of digits, * without spaces or anything else. Examples; "123" or "0" Your goal is to * create a new array of int values that will hold each of the digits specified * in the String. Example; if the string passed is "123" your returned array * should contain the int value 1 at index 0, the value 2 at index 1, and the * value 3 at index 2. Once you are done, return the reference to your newly * created array of int values. **/ public static int[] convert1(String str) { int result[] = new int[str.length()]; for(int i=0; i<str.length(); i++) { result[i] = str.charAt(i) - '0'; } return result; // always returns an array with 1 element for now, replace this with your code }// end method convert1 /** * This method does the opposite work of the above convert1 method. It takes a * vector of single-digit int values and return a string with all these digits * one after the other. If one of the values in the vector is not in the range 0 * to 9 inclusive, then simply have this method return an empty string as result * instead. **/ public static String convert2(int[] v) { String s = ""; for(int x: v) { if(x < 0 || x > 9) { return ""; } s += x; } return s; // always returns an empty string for now, replace this with your code }// end method convert2
************************************************** Your test class is having compilation problem.. Hence i just gave the correct code for the methods. Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.
Get Answers For Free
Most questions answered within 1 hours.