Lab: 4 - Working with Arrays in Java

Assigned
Friday, 1 September 2023
Summary
You will submit this lab for grading (4 points). In this lab, you will practice the use of Arrays in Java.

OBJECTIVE

The objective of this lab is to get experience working with the Arrays.

(1/2 point) Start this lab session by sharing something (most interesting or helpful and Java methods) you learned from the readings or on your own (about Java).
Add the answers to your lab report. At least three ideas per student. 

Part 1: Arrays in Java

We should also explore some of the more significant, heavyweight features of Java and how they improve over what C provides. One example of this is the array which is a data structure that holds a homogeneous (i.e., same type), fixed-size collection of values. When working with a new type of data, you should always ask yourself the following two questions:

  1. How do I create values of this type?
  2. How do I use or consume values of this type?

Luckily, the array syntax is largely identical to C:

  • The type of an array that holds values of type T in Java is T[], e.g., int[] for an array of integers.
  • To create a new array, we use either an array literal or a new expression passing in the size of the array.

For example, the following code snippet creates an array of 5 elements. The first initializes the array:

int[] arr1 = { 0, 1, 2, 3, 4 };     // An array literal     
int[] arr2 = new int[5];            // A "new" expression; int arr2[] = new int[5] works too.

int[] arr2 declares the array, new int[5]; creates or allocates memory to arr2. You must specify the type and number of elements to allocate.

Array indexing arr1[0] works identically to C, but Java arrays, unlike C arrays, know their own length:

System.out.println(arr2.length);    // Prints 5 

You could use jshell in the terminal or command window to test the snipped of code.

With all this in mind, try writing some code to answer the following questions regarding initialization of arrays in Java:

  1. Initialization. Perhaps the largest departure from C is that the following code snippet in C
    int arr[5];
    

    is how you declare an array with five elements in C. Note that there is no array literal or new expression present. What happen if you try this in Java?. i.e., declare a variable with an array type, do not use an array literal or the right-side expression of a “new” expression to initialize it, and then try to use that array.

  2. new Expressions. Note that the array literal allows you to specify the contents of the array (if you know them at compile time). What value(s) does the new expression use to initialize each element of an array? Test different types.

  3. Out-of-bounds. Recall that with C arrays, you are free to walk off the end of the array into arbitrary parts of memory (because an array is merely a pointer)! Can you do this in Java? If not, what error(s) do you get when you try to do this? Do the errors happen at compile time, or runtime?

(1/2 point) Write up the answers to these three questions and add them to your lab report.

Part 2: Arrays and Methods.

You can pass an array as a parameter to a method, and the method can use and change the contents of the array.

  • Take a look at the Java documentation - Class Arrays or Table 7.2 in BJP for most useful methods of the Arrays Class.

(3 points) Create a new program, ArrayProblems.java and write the following functions. Consider that you will work with an array of integers.

  1. min(array) returns the minimum value of the array.
  2. max(array) returns the minimum value of the array.
  3. range(array) returns the range of the given array of integers. The range of an array is the difference between its minimum and maximum value.
  4. printForward(array) prints the elements of the given array in forward order.
  5. printBackward(array) prints the elements of the given array in backward order.

Your main method should demonstrate that your functions works over the following array {3, 7, -10, 2, 9, 1}.

Note: Copy and paste the terminal output of your programs in a .txt file (lab report) as evidence of testing your methods with different inputs.

Lab Submission

You will submit your files via Gradescope by the end of the week. Submit the ArrayProblems.java and your lab report.

I strongly recommend that each student keep a copy of the lab. Therefore, don’t forget to share the files/folder with your lab partner!

Remember: Write your code anticipating errors and print user-friendly error messages, all your public methods should be well documented (use Javadoc comments). You should include comments or organize your code and report in a way that will help the grader to find the answer to the exercises or posted questions.

For those with more time

Exercise A

Write a function memofib(n) that takes an integer n and returns an integer array of size n where the i-th element of that array contains the i-th fibonacci number. For example, memofib(10) should return an integer array containing the elements {1, 1, 2, 3, 5, 8, 13, 21, 34, 55}. Use a loop to do this computation instead of recursion. Your main function should demonstrate the results of calling memofib(10). Why is a loop preferable to a recursive approach to computing fib?

Exercise B

Write a function longestIncreasingSubsequence(arr) that returns the size of the longest increasing subsequence found in the given array of integers. For example, if the input array is {3, 7, -10, 2, 8, 9, 5, 1}, then the function returns 4 corresponding to the sub-sequence {-10, 2, 8, 9}. Your main method should demonstrate that your function works on this sample array.