Question

In: Computer Science

Write a static method remove(int v, int[] in) that will return a new array of the...

Write a static method remove(int v, int[] in) that will return a new array of the integers in the given array, but with the value v removed. For example, if v is 3 and in contains 0, 1, 3, 2, 3, 0, 3, and 1, the method will return an array containing 0, 1, 2, 0, and 1. Hint: You can follow two steps to solve this problem: Create an array in the method, let say you called it result. Before you create the result array, you need to find the number of values that will be in the result. Assign all values from in array to results array except v value. Return the array result. (JAVA) the main method and the class should be in 2 separate files

Solutions

Expert Solution

//RemoveValueFromArray.java
public class RemoveValueFromArray {
    public static int[] remove(int v, int[] in){
        int count = 0;

        for(int i = 0;i<in.length;i++) {
            if(in[i] != v){
                count += 1;
            }
        }

        int result[] = new int[count];
        int k = 0;
        for(int i = 0;i<in.length;i++) {
            if(in[i] != v){
                result[k++] = in[i];
            }
        }
        return result;
    }
}

///////////////////////////////////////////////////////////////////////

//TestCode.java
public class TestCode  {
    public static void main(String[] args) {
        int arr[] = {0, 1, 3, 2, 3, 0, 3, 1};
        int res[] = RemoveValueFromArray.remove(3, arr);

        for(int i = 0;i<res.length;i++){
            System.out.print(res[i]+" ");
        }
    }
}


Related Solutions

Write a method public static void minMax(int[] arr) that takes an array of unique ints of...
Write a method public static void minMax(int[] arr) that takes an array of unique ints of length at least two as an argument, and swaps the smallest value of the array into the 0th position and swaps the largest value of the array into the last position. For example, if int[] a = {4, 3, 2, 6, 1, 5}, the method call minMax(a) should modify the array so that it is {1, 3, 2, 5, 4, 6}. The method should...
Write a method which is passed A[], which is an array of int, and an int...
Write a method which is passed A[], which is an array of int, and an int passingScore. The method returns the number of items in A[] which are greater than or equal to passingScore. Write a method which is passed an array of int A[]. The method returns true if A[] is the same backwards and forwards. Write a method same( ), which is passed two arrays of int. The method returns true if the two arrays contain exactly the...
Write a method with the following header: public static char calculateLetterGrade(int grade, int totalMarks) This method...
Write a method with the following header: public static char calculateLetterGrade(int grade, int totalMarks) This method calculates grade/totalMarks and returns a letter grade based on the following scale: 0-50 = F 51-60 = D 61-70 = C 71-80 = B 81-100 = A
Create a method:         Public Static boolean isSubArray489(int[] intArray) This method has an array of integers as its...
Create a method:         Public Static boolean isSubArray489(int[] intArray) This method has an array of integers as its input parameter, we'll say that a SubArray 9,8,7is that three integers, 9,8,7values appearing decreasing order one by one in the array. Return true if the array does contain any SubArray “987” Notice that any 3 integers that presenting a decreasing by one order must return True. (987, 876,765,654, 543, 432,321, 210 are all True) Call this method in your main function, and pass in...
Given the method static int test(int x, int y, int z){ return(x=y+z); } Which of the...
Given the method static int test(int x, int y, int z){ return(x=y+z); } Which of the follwing is true: public static void main(String[] args) { A System.out.println(test ( 7, 14, 23)) ; B System.out.println(test ( test ( 7,9, 14) , 23 ); C System.out.println( test ( 14, 23 ) ; D System.out.println(test(1,2,4), 14, 23)) ;
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an...
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an argument and returns the sum for every other Int from n down to 1. For example, sumForEachOther(8) should return 20, since 8+6+4+ 2=20.And the call sumForEachOther(9) should return 25 since 9+7+5 + 3+1-=25. Your method must use recursion.
Write a method with the following header: public static void showGradeDistribution(int a, int b, int c,...
Write a method with the following header: public static void showGradeDistribution(int a, int b, int c, int d, int f) It should print a graph (using asterisks) for each of the letters entered in the reverse order of the parameter list and with a label. In addition, if A and B grades sum is equal or exceeds that of grades C and D and F, the message “Strong class!” should be displayed. For example a method call of: showGradeDistribution(5,7,4,4,3); Would...
Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer called removeItem.
Java programming:Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer called removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (Note that after deleting the element, the array size is reduced by 1.) You may assume that the array is unsorted.Now re-do this exercise and name it ExInsertionSort....
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT