Question

Hey, I was wondering if there’s another way to make these methods not reliable to imports...

Hey, I was wondering if there’s another way to make these methods not reliable to imports such as “import java.util.Arrays” and “import java.util.Hash” I am still new in coding and would like to know if there’s another way to do it! Here is the code! (Thank you in advance and I will really appreciate it :) )

/**

This exercise involves implementing several methods. Stubs for each method

with documentation are given here. It is your task to fill out the code in

each method body so that it runs correctly according to the documentation.

You should create a main method to run and test your methods.

Example inputs with output are provided in the comments before each method.

At a minimum, your solutions must pass these tests.

//import NOTICE: Imports are not allowed for these assignments!

*/

public class Exercise

{

/**

Given two ints, return an int array containing the ints in value order.

array2Ints(7, 3) -> {3, 7}

array2Ints(7, 7) -> {7, 7}

array2Ints(3, 7) -> {3, 7}

array2Ints(3, -4) -> {-4, 3}

@param firstNumber int an integer.

@param secondNumber int an integer.

@return int[] An array of the two integers as elements.

**/

public static int[] array2Ints(int firstNumber, int secondNumber) {

if (firstNumber > secondNumber)

{

int a[] = { secondNumber, firstNumber };

return a;

}

else

{

int a[] = { firstNumber, secondNumber };

return a;

}

}//end array2Ints


/**

Given two Strings return a String array containing the strings in alphabetical order.

Note: Capital letters count

array2Strings("washington", "irving") -> {"irving", "washington"}

array2Strings("washington", "Irving") -> {"Irving", "washington"}

array2Strings("Washington", "irving") -> {"Washington", "irving"}

array2Strings("washington", "Washington") -> {"Washington", "washington"} <br>

@param firstString a String.

@param secondString a String.

@return String[] An array of the two Strings as elements.

**/

public static String[] array2Strings(String firstString, String secondString) {

int compare=firstString.compareTo(secondString);

if(compare<0)

{

String []s={firstString,secondString};

return s;

}

else if(compare>0)

{

String []s={secondString,firstString};

return s;

}

else

{

String []s={firstString,secondString};

return s;

}

}//end array2Strings


/**

Given an array of Strings, return the first String in alphabetical order.

Note: Capital letters count


firstString({"fizz", "buzz", "bang", "boom"}) -> "bang"

firstString({"Fizz", "buzz", "bang", "boom"}) -> "Fizz"

firstString({"1", "2", "$$#%^", "pico"}) -> "$$#%^"

firstString({"5", "3", "6", "1", "4"}) -> "1"

@param strings String[] array of Strings.

@return String, alphabetically the first in the array.

**/

public static String firstString(String[] strings) {

// write your code here

Arrays.sort(strings);

return strings[0];

}//end firstString

/**

Given two int arrays of length two, return a length four int array containing the ints in value order.

Hint : use your array2int method

merge2Ints({3, 4}, {1, 2}) -> {1, 2, 3, 4}

merge2Ints({1, 2}, {3, 4}) -> {1, 2, 3, 4}

merge2Ints({7, 7}, {7, 7}) -> {7, 7, 7, 7}

@param firstNumbers int[] an array of integers.

@param secondNumbers int[] an array of integers.

@return int[] An array of the four integers sorted as elements.

**/

public static int[] merge2Ints(int[] firstNumbers, int[] secondNumbers)

{

//write your code here

int[] returnValue = new int[2];

int length = firstNumbers.length + secondNumbers.length;

int[] result = new int[length];

System.arraycopy(firstNumbers, 0, result, 0, firstNumbers.length);

System.arraycopy(secondNumbers, 0, result, firstNumbers.length,secondNumbers.length);

Arrays.sort(result);

return result;

}//end merge2Ints


/**

Given two Strings return a String array containing the strings in alphabetical order.

Note: Capital letters count.

Hint: use your array2Strings method.

merge2Strings({"a", "b"}, {"c", "d"}) -> {"a", "b", "c", "d"}

merge2Strings({"a", "b"}, {"c", "D"}) -> {"D", "a", "b", "c"}

merge2Strings({"d", "c"}, {"b", "a"}) -> {"a", "b", "c", "d"}

merge2Strings({"My", "Dear"}, {"Aunt", "Sally"}) -> {"Aunt", "Dear", "My", "Sally"}

merge2Strings({"my", "dear"}, {"Aunt", "Sally"}) -> {"Aunt", "Sally", "dear", "my"}

merge2Strings({"Irving", "washington"}, {"Irving", "berlin"}) -> {"Irving", "Irving", "berlin", "washington"}

@param firstStrings String[] an array of Strings.

@param secondStrings String[] an array of Strings.

@return String[] An array of the four Strings sorted as elements.

**/

public static String[] merge2Strings(String[] firstStrings, String[] secondStrings) {

// write your code here

int length = firstStrings.length + firstStrings.length;

String[] result = new String[length];

System.arraycopy(firstStrings, 0, result, 0, firstStrings.length);

System.arraycopy(secondStrings, 0, result, firstStrings.length,secondStrings.length);

Arrays.sort(result);

return result;

}//end merge2Strings


/**

Given an int array, return an int array with duplicate ints removed if the array contains duplicate values.

removeDuplicateInts({3}) -> {3}

removeDuplicateInts({1, 2}) -> {1, 2}

removeDuplicateInts({7, 7}) -> {7}

removeDuplicateInts({1, 7, 1, 7, 1}) -> {1, 7}

removeDuplicateInts({1, 2, 3, 4, 5}) -> {1, 2, 3, 4, 5})

removeDuplicateInts({1, 2, 3, 2, 4, 2, 5, 2}) -> {1, 2, 3, 4, 5}

@param numbers int[] an array of integers.

@return int[] An array with the duplicates removed.

**/

public static int[] removeDuplicateInts(int[] numbers) {

//write your code here

if(numbers == null || numbers.length == 0)

                   return null;

            int result[] = new int[1];

            result[0] = numbers[0];

            boolean found ;

            for(int i=1;i<numbers.length;i++)

            {

                   found = false;

                   for(int j=0;j<result.length;j++)

                   {

                          if(result[j] == numbers[i])

                          {

                                found = true;

                                break;

                          }

                   }

                   if(!found)

                   {

                          result = Arrays.copyOf(result, result.length+1);

                          result[result.length-1] = numbers[i];

                   }

            }

            return result;

  }// remove duplicateInts



/**

Given a String array, return an String array with duplicate Strings removed if the array contains duplicate values.

Note: Capital letters count.

removeDuplicateStrings({"a"}) -> {"a"}

removeDuplicateStrings({"a", "b", "c", "d"}) -> {"a", "b", "c", "d"}

removeDuplicateStrings({"a", "a"}) -> {"a"}

removeDuplicateStrings({"A", "a"}) -> {"A", "a"}

removeDuplicateStrings({"these", "are", "the", "times"}) -> {"these", "are", "the", "times"}

removeDuplicateStrings({"these", "times", "are", "the", "times", "they", "are"}) -> {"these", "times", "are", "the", "they"}

@param strings String[] an array of integers.

@return String[] An array with the duplicates removed.

**/

public static String[] removeDuplicateStrings(String[] strings) {

//your code here

if(strings == null || strings.length == 0)

return null;

String result[] = new String[1];

result[0] = strings[0];

boolean found ;

for(int i=1;i<strings.length;i++)

{

found = false;

for(int j=0;j<result.length;j++)

{

if(result[j].equals(strings[i]))

{

found = true;

break;

}

}

if(!found)

{

result = Arrays.copyOf(result, result.length+1);

result[result.length-1] = strings[i];

}

}

return result;

}//end removeDuplicateStrings

}// end exercise

0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class Exercise{

      

       /**

       Given two ints, return an int array containing the ints in value order.

       array2Ints(7, 3) -> {3, 7}

       array2Ints(7, 7) -> {7, 7}

       array2Ints(3, 7) -> {3, 7}

       array2Ints(3, -4) -> {-4, 3}

       @param firstNumber int an integer.

       @param secondNumber int an integer.

       @return int[] An array of the two integers as elements.

       **/

       public static int[] array2Ints(int firstNumber, int secondNumber) {

            

             if (firstNumber > secondNumber)

             {

                    int a[] = { secondNumber, firstNumber };

                    return a;

             }

             else

             {

                    int a[] = { firstNumber, secondNumber };

                    return a;

             }

       }//end array2Ints

      

       /**

       Given two Strings return a String array containing the strings in alphabetical order.

       Note: Capital letters count

       array2Strings("washington", "irving") -> {"irving", "washington"}

       array2Strings("washington", "Irving") -> {"Irving", "washington"}

       array2Strings("Washington", "irving") -> {"Washington", "irving"}

       array2Strings("washington", "Washington") -> {"Washington", "washington"} <br>

       @param firstString a String.

       @param secondString a String.

       @return String[] An array of the two Strings as elements.

       **/

       public static String[] array2Strings(String firstString, String secondString) {

             int compare=firstString.compareTo(secondString);

             if(compare<=0)

             {

                    String []s={firstString,secondString};

                    return s;

             }else

             {

                    String []s={secondString,firstString};

                    return s;

             }

       }//end array2Strings

       /**

       Given an array of Strings, return the first String in alphabetical order.

       Note: Capital letters count

       firstString({"fizz", "buzz", "bang", "boom"}) -> "bang"

       firstString({"Fizz", "buzz", "bang", "boom"}) -> "Fizz"

       firstString({"1", "2", "$$#%^", "pico"}) -> "$$#%^"

       firstString({"5", "3", "6", "1", "4"}) -> "1"

       @param strings String[] array of Strings.

       @return String, alphabetically the first in the array.

       **/

       public static String firstString(String[] strings) {

       // write your code here

             String first = strings[0];

             for(int i = 1;i<strings.length;i++)

                    if(strings[i].compareTo(first) < 0)

                           first = strings[i];

             return first;

       }//end firstString

      

      

       /**

       Hint: use your array2Ints method.

       merge2Ints({3, 4}, {1, 2}) -> {1, 2, 3, 4}

       merge2Ints({1, 2}, {3, 4}) -> {1, 2, 3, 4}

       merge2Ints({7, 7}, {7, 7}) -> {7, 7, 7, 7}

       @param firstNumbers int[] an array of integers.

       @param secondNumbers int[] an array of integers.

       @return int[] An array of the four integers sorted as elements.

       **/

       public static int[] merge2Ints(int[] firstNumbers, int[] secondNumbers)

       {

             int [] returnValue = new int[firstNumbers.length+secondNumbers.length];

            

             int sortFirst[] = array2Ints(firstNumbers[0],firstNumbers[1]);

             int sortSecond[] = array2Ints(secondNumbers[0],secondNumbers[1]);

            

             int i,j,k;

             for(i=0,j=0,k=0;i<sortFirst.length && j<sortSecond.length;k++)

             {

                    if(sortFirst[i] <= sortSecond[j])

                    {

                           returnValue[k] = sortFirst[i];

                           i++;

                    }else

                    {

                           returnValue[k] = sortSecond[j];

                           j++;

                    }

             }

            

             for(;i<sortFirst.length;i++)

                    returnValue[k++] = sortFirst[i];

             for(;j<sortSecond.length;j++)

                    returnValue[k++] = sortSecond[j];

            

             return returnValue;

       } //end merge2Ints

      

       /**

       Note: Capital letters count.

       Hint: use your array2Strings method.

       merge2Strings({"a", "b"}, {"c", "d"}) -> {"a", "b", "c", "d"}

       merge2Strings({"a", "b"}, {"c", "D"}) -> {"D", "a", "b", "c"}

       merge2Strings({"d", "c"}, {"b", "a"}) -> {"a", "b", "c", "d"}

       merge2Strings({"My", "Dear"}, {"Aunt", "Sally"}) -> {"Aunt", "Dear", "My", "Sally"}

       merge2Strings({"my", "dear"}, {"Aunt", "Sally"}) -> {"Aunt", "Sally", "dear", "my"}

       merge2Strings({"Irving", "washington"}, {"Irving", "berlin"}) -> {"Irving", "Irving", "berlin", "washington"}

       @param firstStrings String[] an array of Strings.

       @param secondStrings String[] an array of Strings.

       @return String[] An array of the four Strings sorted as elements.

       **/

       public static String[] merge2Strings(String[] firstStrings, String[] secondStrings) {

             String [] returnValue = new String[firstStrings.length+secondStrings.length];

            

            

             String sortFirst[] = array2Strings(firstStrings[0],firstStrings[1]);

             String sortSecond[] = array2Strings(secondStrings[0],secondStrings[1]);

            

             int i,j,k;

             for(i=0,j=0,k=0;i<sortFirst.length && j<sortSecond.length;k++)

             {

                    if(sortFirst[i].compareTo(sortSecond[j]) <=0)

                    {

                           returnValue[k] = sortFirst[i];

                           i++;

                    }else

                    {

                           returnValue[k] = sortSecond[j];

                           j++;

                    }

             }

            

             for(;i<sortFirst.length;i++)

                    returnValue[k++] = sortFirst[i];

             for(;j<sortSecond.length;j++)

                    returnValue[k++] = sortSecond[j];

            

             return returnValue;

       } //end merge2Strings

      

       /**

       removeDuplicateInts({3}) -> {3}

       removeDuplicateInts({1, 2}) -> {1, 2}

       removeDuplicateInts({7, 7}) -> {7}

       removeDuplicateInts({1, 7, 1, 7, 1}) -> {1, 7}

       removeDuplicateInts({1, 2, 3, 4, 5}) -> {1, 2, 3, 4, 5})

       removeDuplicateInts({1, 2, 3, 2, 4, 2, 5, 2}) -> {1, 2, 3, 4, 5}

       @param numbers int[] an array of integers.

       @return int[] An array with the duplicates removed.

       **/

       public static int[] removeDuplicateInts(int[] numbers) {

             if(numbers == null || numbers.length == 0)

                    return null;

             int result[] = new int[1];

             result[0] = numbers[0];

             boolean found ;

             for(int i=1;i<numbers.length;i++)

             {

                    found = false;

                    for(int j=0;j<result.length;j++)

                    {

                           if(result[j] == numbers[i])

                           {

                                 found = true;

                                 break;

                           }

                    }

                   

                    if(!found)

                    {

                           int tempResult[] = new int[result.length+1];

                           for(int k=0;k<result.length;k++)

                                 tempResult[k] = result[k];

                          

                           tempResult[tempResult.length-1] = numbers[i];

                           result = tempResult;

                          

                    }

             }

            

             return result;

       } //end of removeDuplicateInts

      

       /**

       Note: Capital letters count.

       removeDuplicateStrings({"a"}) -> {"a"}

       removeDuplicateStrings({"a", "b", "c", "d"}) -> {"a", "b", "c", "d"}

       removeDuplicateStrings({"a", "a"}) -> {"a"}

       removeDuplicateStrings({"A", "a"}) -> {"A", "a"}

       removeDuplicateStrings({"these", "are", "the", "times"}) -> {"these", "are", "the", "times"}

       removeDuplicateStrings({"these", "times", "are", "the", "times", "they", "are"}) -> {"these", "times", "are", "the", "they"}

       @param strings String[] an array of integers.

       @return String[] An array with the duplicates removed.

       **/

       public static String[] removeDuplicateStrings(String[] strings) {

             if(strings == null || strings.length == 0)

                    return null;

             String result[] = new String[1];

             result[0] = strings[0];

             boolean found ;

             for(int i=1;i<strings.length;i++)

             {

                    found = false;

                    for(int j=0;j<result.length;j++)

                    {

                           if(result[j].equals(strings[i]))

                           {

                                 found = true;

                                 break;

                           }

                    }

                   

                    if(!found)

                    {

                           String tempResult[] = new String[result.length+1];

                           for(int k=0;k<result.length;k++)

                                 tempResult[k] = result[k];

                          

                           tempResult[tempResult.length-1] = strings[i];

                           result = tempResult;

                          

                    }

             }

            

             return result;

       } //end of removeDuplicateStrings

      

      

}//end Exercise

Add a comment
Know the answer?
Add Answer to:
Hey, I was wondering if there’s another way to make these methods not reliable to imports...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • /**    Given a String and an array of two Strings,    return a three String...

    /**    Given a String and an array of two Strings,    return a three String array containing the strings in alphabetical order.    Note: Capital letters count    sort3Strings("wallace", {"washington", "irving"}) -> {"irving", "wallace", "washington"}    sort3Strings("wallace", {"washington", "Irving"}) -> {"Irving", "wallace", "washington"}    sort3Strings("Washington", {"irving", wallace"}) -> {"Washington", "irving", "wallace"}    sort3Strings("washington", {"washington", "Washington"}) -> {"Washington", "washington", "washington"} **/ public static String[] sort3Strings(String stringValue, String[] stringArray) {    //your code here    return new String[1]; }//end sort3Strings   ...

  • Please write a code in Java where it says your // your code here to run...

    Please write a code in Java where it says your // your code here to run the code smoothly. Import statements are not allowed. _ A Java method is a collection of statements that are grouped together to perform an operation. Some languages also call this operation a Function. When you call the System.out.println() method, for example, the system actually executes several statements in order to display a message on the console. Please write the code according to functions assigned...

  • Please write a code in Java where it says your // your code here to run...

    Please write a code in Java where it says your // your code here to run the code smoothly. Import statements are not allowed for this assignment, so don't use them. _ A Java method is a collection of statements that are grouped together to perform an operation. Some languages also call this operation a Function. When you call the System.out.println() method, for example, the system actually executes several statements in order to display a message on the console. Please...

  • I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][]...

    I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][] t) A method that returns true if from left to right in any row, the integers are increasing, otherwise false. - columnValuesIncrease(int[][] t) A method that returns true if from top to bottom in any column, the integers are increasing, otherwise false. - isSetOf1toN(int[][] t) A method that returns true if the set of integers used is {1, 2, . . . , n}...

  • /** Given an int array, return true if the array contains duplicate values. duplicateInts({3}) -> false...

    /** Given an int array, return true if the array contains duplicate values. duplicateInts({3}) -> false duplicateInts({1, 2}) -> false duplicateInts({7, 7}) -> true duplicateInts({1, 2, 3, 4, 5}) -> false duplicateInts({1, 2, 3, 2, 4, 5}) -> true **/ public static boolean duplicateInts(int[] numbers) { //your code here return false; }//end duplicateInts /** Given a String array, return true if the array contains duplicate values. Note: Capital letters count duplicateStrings({"a"}) -> false duplicateStrings({"a", "b", "c", "d"}) -> false duplicateStrings({"a",...

  • I need help with this Java exercise. I'm not allowed to use imports and I tried...

    I need help with this Java exercise. I'm not allowed to use imports and I tried coding it but ended up failing. /** Given a String array, return an String array with duplicate Strings removed if the array contains duplicate values. <br> Note: Capital letters count. <br> <br> removeDuplicateStrings({"a"}) -> {"a"} <br> removeDuplicateStrings({"a", "b", "c", "d"}) -> {"a", "b", "c", "d"} <br> removeDuplicateStrings({"a", "a"}) -> {"a"} <br> removeDuplicateStrings({"A", "a"}) -> {"A", "a"} <br> removeDuplicateStrings({"these", "are", "the", "times"}) -> {"these", "are",...

  • Using java fix the code I implemented so that it passes the JUnit Tests. MATRIX3 public...

    Using java fix the code I implemented so that it passes the JUnit Tests. MATRIX3 public class Matrix3 { private double[][] matrix; /** * Creates a 3x3 matrix from an 2D array * @param v array containing 3 components of the desired vector */ public Matrix3(double[][] array) { this.matrix = array; } /** * Clones an existing matrix * @param old an existing Matrix3 object */ public Matrix3(Matrix3 old) { matrix = new double[old.matrix.length][]; for(int i = 0; i <...

  • Now this: Suppose that you have been assigned to take over a project from another programmer...

    Now this: Suppose that you have been assigned to take over a project from another programmer who has just been dismissed for writing buggy code. One of the methods you have been asked to rewrite has the following comment and prototype: /* Method: insertValue(value, array) / • Inserts a new value into a sorted array of integers by * creating a new array that is one element larger than the - original array and then copying the old elements into...

  • I need help modifying this program. How would I make sure that the methods is being...

    I need help modifying this program. How would I make sure that the methods is being called and checked in my main method? Here is what the program needs to run as: GDVEGTA GVCEKST The LCS has length 4 The LCS is GVET This is the error that I'm getting: The LCS has length 4 // I got this right The LCS is   //the backtrace is not being called for some reason c++ code: the cpp class: /** * calculate...

  • I am currently using eclipse to write in java. A snapshot of the output would be...

    I am currently using eclipse to write in java. A snapshot of the output would be greatly appreciated to verify that the program is indeed working. Thanks in advance for both your time and effort. Here is the previous exercise code: /////////////////////////////////////////////////////Main /******************************************* * Week 5 lab - exercise 1 and exercise 2: * * ArrayList class with search algorithms * ********************************************/ import java.util.*; /** * Class to test sequential search, sorted search, and binary search algorithms * implemented in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT