Question

In: Computer Science

use a match almost once. consider rhe following instance variable and method. private int[]arr; puplic int...

use a match almost once.
consider rhe following instance variable and method.
private int[]arr;
puplic int i(intx){
   \\PRECOND
   for (intk =arr.length-1;k>=0;k--){
   \\LOOPINVAR
   is(arr[k]<x)returnk;
   }
   \\POSTCOND
   RETURN-1;
}
Let:int m=f(n)

                                       1.assert(a!=null);
                                       2.assert(a.length>0);
                                       3.assert(true)
                                       4.assert(false);
                                       5. All values in positions 0 through m are less than n.
                                       6. All values in positions m+1 through arr.lenght-1 are greater than or equal to n.
                                       7.All the values in positions m+1 through arr.lenght-1 are less then n.
                                       8.The smallest value is at position m.
                                       9.The largest value that is smaller that n is at position m.
                                       10.All values in positions k+1 through arr.length-1 are greater than or equal to n.
                                       11.All values in positions k though arr.length-1 are less than n.
                                       12.all values in positions 0 through k are less than n
                                       13.int[] arr={3,9,3,5,8}
                                       assertTrue(f(4)==2);
                                       14.int[] arr={1,9,3,5,8}
                                       assertTrue(f(4)==0);
The best Preconditions is ____
junit test for thr method that fails____
Best postondition____
junit test for the method that passes_____
Loop invariant_____

Solutions

Expert Solution

The best Preconditions is ____
Answer:
1.assert(a!=null);
Explanation: Array must be checked if it is null or not. Otherwise NullPointerException will be thrown on next line.

junit test for thr method that fails____
Answer:
14.int[] arr={1,9,3,5,8}
assertTrue(f(4)==0);
Explanation: f(4) will return 2 as loop starts from last element and breaks when comparing number 3 which is at position 2 in array.
So this test case will fail. As here f(4) will return 2. And assertion is made on comparison with 0.

Best postondition____
Answer:
10.All values in positions k+1 through arr.length-1 are greater than or equal to n.
Description: It will come to the this point when all the values are greater than or equal to n.

junit test for the method that passes_____
Answer:
13.int[] arr={3,9,3,5,8}
assertTrue(f(4)==2);
Explanation: f(4) will return 2 as loop starts from last element and breaks when comparing number 3 which is at position 2 in array.
So this test case will pass. As here f(4) will return 2. And assertion is made on comparison with 2.

Loop invariant_____
Answer:
6. All values in positions m+1 through arr.lenght-1 are greater than or equal to n.
Explanation: Since m is the index of array at which point the value is less than n,
so all values from m+1 till arr.length-1 are greater than or equal to n.


Related Solutions

Consider the following code: void swap(int arr[], int i, int j) {        int temp = arr[i];...
Consider the following code: void swap(int arr[], int i, int j) {        int temp = arr[i];        arr[i] = arr[j];        arr[j] = temp; } void function(int arr[], int length) {        for (int i = 0; i<length / 2; i++)               swap(arr, i, (length / 2 + i) % length); } If the input to the function was int arr[] = { 6, 1, 8, 2, 5, 4, 3, 7 }; function(arr,8); What values would be stored in the array after calling the...
   private static void merge(int arr[], int l, int m, int r) {        //...
   private static void merge(int arr[], int l, int m, int r) {        // Find sizes of two subarrays to be merged        int n1 = m - l + 1;        int n2 = r - m;        /* Create temp arrays */        int L[] = new int[n1];        int R[] = new int[n2];        /* Copy data to temp arrays */        for (int i = 0; i...
public class Date { private int dMonth; //variable to store the month private int dDay; //variable...
public class Date { private int dMonth; //variable to store the month private int dDay; //variable to store the day private int dYear; //variable to store the year //Default constructor //Data members dMonth, dDay, and dYear are set to //the default values //Postcondition: dMonth = 1; dDay = 1; dYear = 1900; public Date() { dMonth = 1; dDay = 1; dYear = 1900; } //Constructor to set the date //Data members dMonth, dDay, and dYear are set //according to...
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
Complete the required methods: public class SongList { // instance variables private Song m_last; private int...
Complete the required methods: public class SongList { // instance variables private Song m_last; private int m_numElements; // constructor // Do not make any changes to this method! public SongList() { m_last = null; m_numElements = 0; } // check whether the list is empty // Do not make any changes to this method! boolean isEmpty() { if (m_last == null) return true; else return false; } // return the size of the list (# of Song nodes) // Do...
Consider the following java class: class Student {    private int student_Number;    private String student_Name;    public Student(int...
Consider the following java class: class Student {    private int student_Number;    private String student_Name;    public Student(int stNo,String name) {         student_Number=stNo;         student_Name=name;      }     public String getName() {       return student_Name;     }      public int getNumber() {       return student_Number;      }     public void setName(String st_name) {       student_Name = st_name;     } } Write a Tester class named StudentTester which contains the following instruction: Use the contractor to create a student object where student_Number =12567, student_Name = “Ali”. Use the setName method to change the name of...
Re-write this method using iteration ONLY JAVA class Main { public static void merge(int arr[], int...
Re-write this method using iteration ONLY JAVA class Main { public static void merge(int arr[], int l, int m, int r) { int n1 = m - l + 1; int n2 = r-m; int left[] = new int[n1]; int right[] = new int[n2]; for (int i = 0; i < n1; ++i) left[i] = arr[l + i]; for (int j = 0; j < n2; ++j) right[j] = arr[m + 1 + j]; int i=0; //left int j=0; //...
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...
Consider the following recursive method in Java public static int mystery(int n) {   if (n ==...
Consider the following recursive method in Java public static int mystery(int n) {   if (n == 0)   return 1;    else    return 4 * mystery (n - 1);   } What is the output of  mystery (3) using the code segment above Show your work on your trace file
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT