Question: In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the
In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester.
Util is a utility class. It has no instance variables and should not have a constructor. Its purpose is to house some useful static methods:
public static double average(int[] numbers) - gets the average of the values values in the array or 0 if the array has no contents. Use the enhanced for loop.
public static double average(ArrayList
public static boolean containsThrice(int[] list, int target) - determines if the target is in the array exactly three times. Returns true if it is, otherwise returns false.
public static boolean containsThrice(ArrayList
Notice that there are two methods called average and two methods called containsThrice. These are examples of overloading - methods with the same name but different number and type of parameters. The compiler tells them apart because in each case, one takes an array and one an ArrayList as a parameter.
When using the enhanced for loop, the implementation of the pairs of methods will be very similar. The exact same loop works for both.
Provide Javadoc. Look at the documentation produced for Util. The Javadoc utility works on static methods, too.
The tester class:
import java.util.ArrayList; /** * Tester for the static methods of Util * * @author Kathleen O'Brien */ public class UtilTester { public static void main(String[] args) { //array average int[] numbers = {5, 6, 9, 8, 6, 7, 6}; System.out.println("average array: "+ Util.average(numbers)); System.out.println("Expected: 6.714285714285714"); int[] numbers2 = {-5, -9, -8, -6, -7, -10, -5, 3}; System.out.println("average array: "+ Util.average(numbers2)); System.out.println("Expected: -5.875"); int[] numbers3 = {}; System.out.println("average array: "+ Util.average(numbers3)); System.out.println("Expected: 0.0"); //array containsThrice System.out.println("Array contains Thrice: " + Util.containsThrice(numbers, 6)); System.out.println("Expected: true"); numbers[0] = 6; System.out.println("Array contains Thrice: " + Util.containsThrice(numbers, 6)); System.out.println("Expected: false"); System.out.println("Array contains Thrice: " + Util.containsThrice(numbers2, -5)); System.out.println("Expected: false"); System.out.println("Array contains Thrice: " + Util.containsThrice(numbers3, 6)); System.out.println("Expected: false"); //arraylist average ArrayList list = new ArrayList<>(); list.add(5); list.add(6); list.add(9); list.add(8); list.add(6); list.add(7); list.add(6); System.out.println("average ArrayList: " + Util.average(list)); System.out.println("Expected: 6.714285714285714"); ArrayList list2 = new ArrayList<>(); list2.add(-5); list2.add(-9); list2.add(-8); list2.add(-6); list2.add(-7); list2.add(-10); list2.add(-5); list2.add(3); System.out.println("average ArrayList: " + Util.average(list2)); System.out.println("Expected: -5.875"); ArrayList list3 = new ArrayList<>(); System.out.println("average ArrayList: " + Util.average(list3)); System.out.println("Expected: 0.0"); //ArrayList containsThrice System.out.println("ArrayList contains Thrice: " + Util.containsThrice(list, 6)); System.out.println("Expected: true"); list.set(0, 6); System.out.println("ArrayList contains Thrice: " + Util.containsThrice(list, 6)); System.out.println("Expected: false"); System.out.println("ArrayList contains Thrice: " + Util.containsThrice(list2, -5)); System.out.println("Expected: false"); System.out.println("ArrayList contains Thrice: " + Util.containsThrice(list3, -5)); System.out.println("Expected: false"); } } To check your work you can try this codecheck link:
http://www.codecheck.it/files/18032401453uj2im9sgn9pkrphvhqy4u1yb
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
