Objective: Using Arrays
The purpose of this project is to write some basic methods which work with arrays.
Copy the two classes below into BlueJ and complete the methods indicated.
/**
* Complete the Array methods below
* Note that all of the methods are static!
*/
public class Array {
/*** <<< THIS METHOD IS COMPLETE >>>
* prints out an array of any size
***/
public static void print(int[] a) {
int x = 0;
for (x = 0; x < a.length-1; x++)
System.out.print(a[x] + ", ");
System.out.println(a[x]);
}
/*** <<< CODE NOT COMPLETE >>>
* returns the largest integer in an array
***/
public static int max(int[] a) {
// <<< COMPLETE THE CODE >>>
}
/*** <<< CODE NOT COMPLETE >>>
* returns the smallest integer in an array
***/
public static int min(int[] a) {
// <<< COMPLETE THE CODE >>>
}
/*** <<< CODE NOT COMPLETE >>>
* reverses the order of the elemets in the array
***/
public static void reverse(int[] a) {
// <<< COMPLETE THE CODE >>>
}
/*** <<< CODE NOT COMPLETE >>>
* merges two sorted arrays into 1, maintains the sorted order
***/
public static int[] merge(int[] a, int[] b) {
// <<< COMPLETE THE CODE >>>
}
}
/***
* <<< This Code is Complete >>>
*
* The following is the output from this program:
*
* array example 1
* 4, 5, 9, 7, 3, 4, 8
* The largest value is: 9
* The smallest value is: 3
*
* array example 2
* 24, 87, 96, 102, 874, 941
* reverse order: 941, 874, 102, 96, 87, 24
*
* array example 3
* 2, 6, 8, 9
* 1, 3, 7, 9, 10
* merged array: 1, 2, 3, 6, 7, 8, 9, 9, 10
* ***/
public class ArrayTester {
public static void main(String[] args) {
System.out.println("\narray example 1");
int num1[] = {4,5,9,7,3,4,8};
Array.print(num1);
System.out.println("The largest value is: " + Array.max(num1));
System.out.println("The smallest value is: " + Array.min(num1));
System.out.println("\narray example 2");
int num2[] = {24,87,96,102,874,941};
Array.print(num2);
System.out.print("reverse order: ");
Array.reverse(num2);
Array.print(num2);
System.out.println("\narray example 3");
int num3[] = {2,6,8,9};
int num4[] = {1,3,7,9,10};
Array.print(num3);
Array.print(num4);
System.out.print("merged array: ");
int merged[] = Array.merge(num3,num4);
Array.print(merged);
}
}