Dana Vrajitoru
C101/I210 Computer Programming

C101/I210 Homework 11

Due date: Wednesday, April 22, 2020.

Objective: To work with arrays in Java.

In this homework you will continue adding code to the program that manipulates arrays in Java that we started in the lab. Make sure to do Lab 11 first.

Ex. 1. More Array Methods

Reopen (if you closed it) the project that you created for lab 11. Add the following features to it.

a. Maximum

Write a static method that takes one parameter which is an array of integers and returns the largest value in the array. The header of the method should be

static int findMax(int[] a)

Add an option for it in the menu (method displayMenu in the demo) with the letter "X" and add a case for it in the switch statement and add call to it inside the case.

b. Average

Write a static method that takes in an array of integer and output their average. If the array has a length of 0, the function should return 0. Otherwise it should add all the elements of the array and then divide the result by their number, which is the length of the array, and return the result. The header of the function should be

static float average(int[] a)

In the main, add an option for it in the menu with the letter V, and the appropriate cases in the switch statement, in which you call the method and output the result.

c. Search

Write a method that takes in an array an integer and one specific value, called target. The method should return true if the target is in the array, and false if it isn't. The header of the method should be

static boolean find(int[] a, int target)

The method should contain a loop that goes through the entire array with an index i, just like in the method sumArray. In the loop, compare a[i] with the target, and if they are equal, you can return true. On the else, don't do anything in the loop (don't add an else to this). After the loop, return false. That way, if the search reaches the end of the array and it doesn't return true from anywhere, then the target is not in the array and you can return false.

As before, add an option for this in the main on the letter "F". Also ask the user for the value they want to search for and input it in a local variable (you'll need to declare it).

d. Reverse

Add the following method that swaps two elements of an array:

static void swap(int[] a, int i, int j) {
  int temp = a[i];
  a[i] = a[j];
  a[j] = temp;
}

Write a method that reverses the values of an array. The method should use a loop with i going from 0 to the length of the array divided by 2. For each i, it should call the swap with indexes i and n-i-1. The method doesn't need to output anything, because we'll assume that the user will ask to output the array from the menu after it reverses it. The header of the function should be

static void reverse(int[] a)

Add an option in the menu with the letter "R" and add the appropriate case and call.

Test the program to make sure all of the options work properly.

Turn in:

Upload the lab and homework .java file (should be the same): ArrayMethods.java, to Canvas in Assignments - Homework 11.