Question

import java.io.PrintStream; import java.util.Arrays; public class joker { public static int smallest(int[] v1, int[] v2) {...

    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");
      }

   }
}

Homework Answers

Answer #1
        /**
         * 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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length();...
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length(); for (int i = 0; i < array.length; i++) { if (array[i].length() < minLength) minLength = array[i].length(); } return minLength; } public static void main(String[] args) { Scanner in = new Scanner(System.in); String[] strings = new String[50]; for (int i = 0; i < strings.length; i++) { System.out.print("Enter string " + (i + 1) + ": "); strings[i] = in.nextLine(); } System.out.println("Length of smallest...
import java.util.Scanner; import java.util.Random; public class DiceRoll {    public static final int SIDES = 6;...
import java.util.Scanner; import java.util.Random; public class DiceRoll {    public static final int SIDES = 6;    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("Enter the number of times a 6 sided die should be rolled ");        Scanner keyboard = new Scanner(System.in);        Random r = new Random();               int times = keyboard.nextInt();        boolean valid = false;        while(!valid) {           ...
Stack2540Array import java .io .*; import java . util .*; public class Stack2540Array { int CAPACITY...
Stack2540Array import java .io .*; import java . util .*; public class Stack2540Array { int CAPACITY = 128; int top ; String [] stack ; public Stack2540Array () { stack = new String [ CAPACITY ]; top = -1; } 1 3.1 Implement the stack ADT using array 3 TASKS public int size () { return top + 1; } public boolean isEmpty () { return (top == -1); } public String top () { if ( top == -1)...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        }        int new_k = scan.nextInt(); System.out.println(""+smallerK(new_stack, new_k));    }     public static int smallerK(Stack s, int k) {       ...
import java.util.Scanner; public class AroundTheClock {    public static void main(String[] args)    {       ...
import java.util.Scanner; public class AroundTheClock {    public static void main(String[] args)    {        Scanner input = new Scanner(System.in);        int departureTime = input.nextInt();        int travelTime = input.nextInt();        int arrivalTime;            departureTime =12;            departureTime = 0;            arrivalTime = (int) (departureTime + travelTime);            (arrivalTime = (arrivalTime >=12));            if (arrivalTime = arrivalTime %12)            {            System.out.println(arrivalTime);...
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.text.ParseException; import java.util.*; public class SJF { public static...
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.text.ParseException; import java.util.*; public class SJF { public static void readFromFile() throws IOException { BufferedReader bufReader = new BufferedReader(new FileReader("processes.txt")); ArrayList<String> listOfLines = new ArrayList<>(); String line = bufReader.readLine(); while (line != null) { listOfLines.add(line); line = bufReader.readLine(); } bufReader.close(); System.out.println("Content of ArrayLiList:"); // split by new line int num = 0; for (String line1 : listOfLines) { String line2[] = line1.split(","); // int burstTime = Integer.parseInt(line2[3].trim()); // String retrival = listOfLines.get(0); System.out.println("...
import java.util.Scanner; public class InRange { private int min; private int max; public InRange(int initialMin, int...
import java.util.Scanner; public class InRange { private int min; private int max; public InRange(int initialMin, int initialMax) { min = initialMin; max = initialMax; } // You need to write two instance methods: // 1.) A method named inRange, which takes an int. // This returns true if the int is within the given range // (inclusive), else false. // // 2.) A method named outOfRange which takes an int. // This returns false if the int is within the...
Please solve this problem in java. import java.util.Arrays; public class PriorityQueue { /* This class is...
Please solve this problem in java. import java.util.Arrays; public class PriorityQueue { /* This class is finished for you. */ private static class Customer implements Comparable { private double donation; public Customer(double donation) { this.donation = donation; } public double getDonation() { return donation; } public void donate(double amount) { donation += amount; } public int compareTo(Customer other) { double diff = donation - other.donation; if (diff < 0) { return -1; } else if (diff > 0) { return...
public class Mystery { public static String mystery(String str, int input) { String result = "";...
public class Mystery { public static String mystery(String str, int input) { String result = ""; for (int i = 0; i < str.length() - 1; i++) { if (input == 0) { str = ""; result = str; } if (input == -2) { result = str.substring(2, 4); } if (input == 1) { result = str.substring(0, 1); } if (input == 2) { result = str.substring(0, 2); } if (input == 3) { result = str.substring(2, 3); }...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....