Answered step by step
Verified Expert Solution
Question
1 Approved Answer
/** This exercise involves implementing several methods. Stubs for each method with documentation are given here. It is your task to fill out the code
/** 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. @author Your Name @version put the date here **/ import java.util.Arrays; public class Functions2 { /** Given an int and an array of ints, return true if the array contains the int, false otherwise. hasInt(7, {1, 7, 3}) -> true hasInt(3, {1, 2, 4, 5}) -> false hasInt(4, {5, 3, 6, 1, 4}) -> true **/ public static boolean hasInt(int number, int[] numbers) { // Write your code here return false; } /** Given a String and an array of Strings, return true if the array contains the String, false otherwise. Note: Capital letters count hasString("fizz", {"fizz", "buzz", "bang", "boom"}) -> true hasString("Fizz", {"fizz", "buzz", "bang", "boom"}) -> false hasString("fizz", {"buzz", "bang", "boom"}) -> false hasString("buzz", {"1", "2", "$$#%^", "pico"}) -> false hasString("4", {"5", "3", "6", "1", "4"}) -> true **/ public static boolean hasString(String paramString, String[] strings) { // Write your code here return false; } /** Given an an array of ints, return the largest int in the array. maxInt({1, 7, 3}) -> 7 maxInt({1, 2, 4, 5}) -> 5 maxInt({5, 3, 6, 1, 4}) -> 6 **/ public static int maxInt(int[] numbers) { // Write your code here return -1; } /** 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" **/ public static String firstString(String[] strings) { int compare; String returnValue = strings[0]; for(int i = 1; i < strings.length; i++ ) { compare = returnValue.compareTo(strings[i]); if(compare > 0) { //returnValue is smaller than strings[i] returnValue = strings[i]; } } //END forLoop //System.out.println("Return Value" + returnValue); return returnValue; } /** Given an array of Strings, return an array with the length of the longest string longestString({"a", "big", "fat", "cat"}) -> 3 longestString({"Fizz", "buzz", "bang", "boom"}) -> 4 longestString({"1", "2", "$$#%^", "pico"}) -> 5 longestString({"5", "3", "6", "1", "4"}) -> 1 longestString("These", "Are", "the", "Good", "Old", "days") -> 5 **/ public static int longestString(String[] strings) { // Write your code here return 0; } /** Given an int and an array of ints, return -1 if the array does not contain the int Otherwise return the position of the int in the array. placeInt(7, {7, 3}) -> 0 placeInt(7, {2, 7, 3}) -> 1 placeInt(3, {1, 2, 4, 5}) -> -1 placeInt(4, {5, 3, 6, 1, 4}) -> 4 **/ public static int placeInt(int number, int[] numbers) { // Write your code here return -1; } /** Given a String and an array of Strings, return -1 if the array does not contain the String Otherwise return the position of the String in the arary. placeString("fizz", {"fizz", "buzz", "bang", "boom"}) -> 0 placeString("buzz", {"fizz", "buzz", "bang", "boom"}) -> 1 placeString("bang", {"fizz", "buzz", "bang", "boom"}) -> 2 placeString("boom", {"fizz", "buzz", "bang", "boom"}) -> 3 placeString("Fizz", {"fizz", "buzz", "bang", "boom"}) -> -1 placeString("fizz", {"buzz", "bang", "boom"}) -> -1 placeString("buzz", {"1", "2", "$$#%^", "pico"}) -> -1 placeString("4", {"5", "3", "6", "1", "4"}) -> 4 **/ public static int placeString(String paramString, String[] strings) { // Write your code here return -1; } /** 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} **/ public static int[] array2Ints(int firstNumber, int secondNumber) { if(firstNumber <= secondNumber) { return new int[]{firstNumber, secondNumber}; } else { return new int[]{secondNumber, firstNumber}; } } /** 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"} **/ public static String[] array2Strings(String firstString, String secondString) { // Write your code here return new String[]{"0", "0"}; } /** Given an int and an array of two ints, return an array of 3 ints sorted in value order. sort3Ints(5, {3, 7}) -> {3, 5, 7} sort3Ints(7, {5, 3}) -> {3, 5, 7} sort3Ints(3, {3, 3}) -> {3, 3, 3} sort3Ints(3, {3, -4}) -> {-4, 3, 3} **/ public static int[] sort3Ints(int intValue, int[] intArray) { // Write your code here return new int[]{0, 0, 0}; } /** 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) { // Write your code here return new String[]{"0", "0", "0"}; } /** Given two int arrays of length two, return a length four int array containing the ints in value order. 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} **/ public static int[] merge2Ints(int[] firstNumbers, int[] secondNumbers) { int [] returnValue = new int[firstNumbers.length + secondNumbers.length]; for(int counter = 0; counter < firstNumbers.length; counter++) { returnValue[counter] = firstNumbers[counter]; } for(int counter = 0; counter < secondNumbers.length; counter++) { returnValue[counter + firstNumbers.length] = secondNumbers[counter]; } Arrays.sort(returnValue); return returnValue; /* Code from Class int[] columnOne = array2Ints(firstNumbers[0], firstNumbers[1]); int[] columnTwo = array2Ints(secondNumbers[0], secondNumbers[1]); int [] returnValue = new int[4]; int i=0; int j=0; for(int counter = 0; counter < (firstNumbers.length + secondNumbers.length); counter++) { if(i == firstNumbers.length) { //finish up with column 2 for(int k = counter; k < (firstNumbers.length + secondNumbers.length); k++) { returnValue[k] = columnTwo[j]; j++; } return returnValue; } else if(j == secondNumbers.length) { //finish up with column 1 for(int k = counter; k < (firstNumbers.length + secondNumbers.length); k++) { returnValue[k] = columnOne[i]; i++; } return returnValue; } else { if(columnOne[i] < columnTwo[j]) { returnValue[counter] = columnOne[i]; i++; } else { returnValue[counter] = columnTwo[j]; j++; } } } return returnValue; */ } /** 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"} **/ public static String[] merge2Strings(String[] firstStrings, String[] secondStrings) { // Write your code here return new String[]{"0", "0", "0", "0"}; } /** 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) { // Write your code here return false; } /** 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", "a"}) -> true duplicateStrings({"A", "a"}) -> false duplicateStrings({"these", "are", "the", "times"}) -> false duplicateStrings({"these", "are", "the", "times", "they", "are"}) -> true duplicateStrings({"my", "dear"}, {"Aunt", "Sally"}) -> {"Aunt", "Sally", "dear", "my"} duplicateStrings({"Irving", "washington"}, {"Irving", "berlin"}) -> {"Irving", "Irving", "berlin", "washington"} **/ public static boolean duplicateStrings(String[] strings) { // Write your code here return false; } /** 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} **/ public static int[] removeDuplicateInts(int[] numbers) { // Write your code here return new int[]{0}; } /** 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"}) **/ public static String[] removeDuplicateStrings(String[] strings) { // Write your code here return new String[]{""}; } /** Given two int arrays return true if the arrays contain the same values in the same order. Note: Order matters, see the third example compare2IntArrays({3, 4}, {1}) -> false compare2IntArrays({1, 2}, {1, 2}) -> true compare2IntArrays({1, 2}, {2, 1}) -> false compare2IntArrays({1, 2, 3, 4}, {1, 2, 3, 4}) -> true **/ public static boolean compare2IntArrays(int[] firstNumbers, int[] secondNumbers) { // Write your code here return false; } /** Given two String arrays return true if the arrays contain the same values in the same order. Note: Order matters, see the forth example Note: Capatil letters matter, see the final example compare2StringArrays({"and"}, {"or"}) -> false compare2StringArrays({"and", "but"}, {"or"}) -> false compare2StringArrays({"a", "b", "c", "d"}, {"a", "b", "c", "d"}) -> true compare2StringArrays({"a", "b", "c", "d"}, {"d", "c", "b", "d"}) -> false compare2StringArrays({"a", "b", "c", "d"}, {"A", "b", "C", "d"}) -> false compare2StringArrays({"Aunt", "Sally"}, {"Aunt", "Sally"}) -> true compare2StringArrays({"Aunt", "Sally"}, {"Aunt", "sally"}) -> false **/ public static boolean compare2StringArrays(String[] firstStrings, String[] secondStrings) { // Write your code here return false; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started