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.
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:
Luckily, the array syntax is largely identical to C:
T
in Java is T[]
, e.g., int[]
for an array of integers.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:
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.
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.
(1/2 point) Write up the answers to these three questions and add them to your lab report.
You can pass an array as a parameter to a method, and the method can use and change the contents of the array.
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.
min(array)
returns the minimum value of the array.max(array)
returns the minimum value of the array.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.printForward(array)
prints the elements of the given array in forward order.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.
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.
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?
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.