Question

Please use java code. Implement a test class named BatArray1Test that tests all the functionality of...

Please use java code.

Implement a test class named BatArray1Test that tests all the functionality of the following given 3 methods, including the invalid arguments:

1) commonEnd

Given two arrays of ints, a and b, return true if they have the same first element or they have the same last element. This method should return false if either array is empty or null.

2) midThree

Given an array of integers, return a new array of length 3 containing the elements from the middle of the array. This method should return null if the array length is even or less than 3.

3) maxTriple

Given an array of integers, look at the first, last, and middle values in the array, and return the largest value. This method should return 0 if the length of the array is even.

"Like" for any responses within 3 hours.

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

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

// BatArray1Test.java

public class BatArray1Test {

   public static void main(String[] args) {
       int array1[]={4,6,7,8,9,10,21};
       int array2[]={4,7,8,9,2,10,21};
       if(commonEnd(array1,array2))
       {
           System.out.println("Both arrays having command End");
       }
       else
       {
           System.out.println("Both arrays not having command End");
       }
      
       int midArray[]=midThree(array1);
       if(midArray!=null)
       {
           for(int i=0;i<midArray.length;i++)
           {
               System.out.println(midArray[i]+" ");
           }          
       }

       int largest=maxTriple(array1);
       if(largest!=0)
       System.out.println("Largest Value :"+largest);

   }

   private static int maxTriple(int[] array1) {
       int max=0;
       if(array1.length%2==0)
       {
           return 0;
       }
       else
       {
           max=array1[0];
           if(max<array1[(array1.length/2)+1])
           {
               max=array1[(array1.length/2)+1];
           }
           if(max<array1[array1.length-1])
           {
               max=array1[array1.length-1];
           }
       }
       return max;
   }

   private static int[] midThree(int[] array1) {
       int arr[]=null;
       if(array1.length%2==0 || array1.length<3)
       {
           return arr;
       }
       else
       {
           arr=new int[3];
           arr[0]=array1[0];
           arr[1]=array1[(array1.length/2)+1];
           arr[2]=array1[array1.length-1];
       }
       return arr;
   }

   public static boolean commonEnd(int arr1[],int arr2[])
   {
       boolean b=false;
       if(arr1==null || arr2==null)
           b=false;
       else if(arr1.length==0 || arr2.length==0)
           b=false;
       if(arr1[0]==arr2[0] && arr1[arr1.length-1]==arr2[arr2.length-1])
           b=true;
      
       return b;
   }
}
_________________________

Output:

Both arrays having command End
4
9
21
Largest Value :21


_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
Please use java code. Implement a test class named BatArray1Test that tests all the functionality of...
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 two arrays of ints, a and b, write a java method that returns true if...

    Given two arrays of ints, a and b, write a java method that returns true if they have the same first element or they have the same last element. This method should return false if either array is empty or null. "Like" for responses within 3 hours.

  • Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method...

    Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method, but for now leave it empty. Then write a Java method getAverage which takes an array of integers as it’s argument. The method calculate the average of all the elements in the array, and returns that average. The method should work for an array of integers of any length greater than or equal to one. The method signature is shown below: public static double getAverage(...

  • You must create a Java class named TenArrayMethods in a file named TenArrayMethods.java. This class must...

    You must create a Java class named TenArrayMethods in a file named TenArrayMethods.java. This class must include all the following described methods. Each of these methods should be public and static.Write a method named getFirst, that takes an Array of int as an argument and returns the value of the first element of the array. NO array lists. Write a method named getLast, that takes an Array of int as an argument and returns the value of the last element...

  • Create a new ArrayPractice project in Eclipse and a class named ArrayPractice.java. Put everything that follows...

    Create a new ArrayPractice project in Eclipse and a class named ArrayPractice.java. Put everything that follows into the main method except the method arrayAverage. Create a 5 element array of doubles called grades that contains the following numbers in this order: 81.2, 92.5, 48.9, 78.8, and 45.5. Create a 7 element array of ints called numbers that contains the following numbers in this order: 12, 42, 33, 67, 92, 58, and 33. Create a 9 element array of Strings called...

  • Programming in java In this part of this Lab exercise, you will need to create a...

    Programming in java In this part of this Lab exercise, you will need to create a new class named ArrayShiftMult. This class carries out simple manipulation of an array. Then you should add a main method to this class to check that your code does what it is supposed to do. 1. Create a BlueJ project named LAB06 as follows: a. Open the P drive and create a folder named Blue), if it does not exist already. Under this folder,...

  • USE JAVA: Given the Interface Code and the Interface Implementation Code; Write Junit Tests to test...

    USE JAVA: Given the Interface Code and the Interface Implementation Code; Write Junit Tests to test all fuctionality. ------------- Interface code: public interface CustomList { /** * This method should add a new item into the CustomList and should * return true if it was successfully able to insert an item. * @param item the item to be added to the CustomList * @return true if item was successfully added, false if the item was not successfully added (note: it...

  • JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a...

    JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...

  • HW58.1. Array Merge Sort You've done merge (on Lists), so now it's time to do merge...

    HW58.1. Array Merge Sort You've done merge (on Lists), so now it's time to do merge sort (on arrays). Create a public non-final class named Mergesort that extends Merge. Implement a public static method int[] mergesort(int[] values) that returns the input array of ints sorted in ascending order. You will want this method to be recursive, with the base case being an array with zero or one value. If the passed array is null you should return null. If the...

  • 3. Write Java methods to accomplish each of the following Write a method named simpleAry which...

    3. Write Java methods to accomplish each of the following Write a method named simpleAry which accepts and return nothing. It creates an array that can hold ten integers. Fill up each slot of the array with the number 113. Then, display the contents of the array on the screen. You must use a loop to put the values in the array and also to display them. Write a method named sury which accepts an array of type double with...

  • Java arrays Create method named repeatedN that takes two integer parameters named range and n and...

    Java arrays Create method named repeatedN that takes two integer parameters named range and n and returns an integer array Method should return an integer array that has numbers 0 - range repeated in order n times If range is less than or equal to 0; return an array that has 0 repeated n times Create a second method named printArray that takes an integer array as a parameter. The method should print out every element on the same line...

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