Problem 5: Method definition (15 minutes) [9 points] You are given below the headers of 3 methods called reverseArray, and Your job is to complete the definition incrementByMin, pickRandomValue. of each one of these methods as per the provided guidelines. reverseArray is a method that accepts an array of values called and int arr prints its elements backwards. public void reverseArray(int[] arr) { for (int i=arr.length-1;i>=0;i++) System.out.print(arr[i]); } is a method that increments each of the array elements found in incrementByMin the input array of values called by the value of the minimum element in int arr arr. public void incrementByMin(int[] arr) { int min = Integer.MAX_VALUE; for (int i=0;i<arr.length;i++) if (arr[i]<min) min = arr[i]; for (int i=0;i<arr.length;i++) arr[i] += min; } pickRandomValue is a method that accepts as a parameter a 2-dimensional array of values called and returns an value that is randomly selected int arr int from arr. public int pickRandomValue(int[][] arr) { int i = (int)(Math.random()*arr.length); int j = (int)(Math.random()*arr[0].length); return arr[i][j]; } Problem 6: Completing code fragments [12 points] (15 minutes) Complete the definition of the class given below. This class represents a Polynomials second degree polynomial of the form: . The three coefficients and are stored in a, b, c 1-D array called that serves as an instance variable for This class coef Polynomials. has two additional instance variables and representing the roots of the String x1 x2 polynomial public class Polynomials { // Put the instance variables here double[] coef; String x1, x2; // Constructor: instantiate coef, then //insert a, b and c as its 1st, 2nd and 3rd elements
Problem 5: Method definition (15 minutes) [9 points] You are given below the headers of 3 methods called reverseArray, incrementByMin, and pickRandomValue. Your job is to complete the definition of each one of these methods as per the provided guidelines. reverseArray is a method that accepts an array of int values called arr and prints its elements backwards. public void reverseArray(int[] arr) { for (int i=arr.length-1;i>=0;i++) System.out.print(arr[i]); } incrementByMin is a method that increments each of the array elements found in the input array of int values called arr by the value of the minimum element in arr. public void incrementByMin(int[] arr) { int min = Integer.MAX_VALUE; for (int i=0;i<arr.length;i++) if (arr[i]<min) min = arr[i]; for (int i=0;i<arr.length;i++) arr[i] += min; } pickRandomValue is a method that accepts as a parameter a 2-dimensional array of int values called arr and returns an int value that is randomly selected from arr. public int pickRandomValue(int[][] arr) { int i = (int)(Math.random()*arr.length); int j = (int)(Math.random()*arr[0].length); return arr[i][j]; } Problem 6: Completing code fragments (15 minutes) [12 points] Complete the definition of the class Polynomials given below. This class represents a second degree polynomial of the form: . The three coefficients a, b, and c are stored in 1-D array called coef that serves as an instance variable for Polynomials. This class has two additional String instance variables x1 and x2 representing the roots of the polynomial public class Polynomials { // Put the instance variables here double[] coef; String x1, x2; // Constructor: instantiate coef, then //insert a, b and c as its 1st, 2nd and 3rd elements
FinalExamFall12.docx