Lab 2 Arrays & JUnit Testing Programs that hold values of the same type benefit from arrays. Once placed in an array, values can be accessed using an index, which is a value from 0 up to the number of values in the array: where the first value is at position 0, the second at position 1, and so on, until the last element, which is located at index position “length – 1” (where length is the number of values in the array). As shown in the code-snippet below, array values are accessed using a square-brackets notation. In this example, we defined “anArray” as an array of 5 integers (initialized as zeroes) from where we get the value at the fourth element (at index 3) and assign a value of 9 to the first element. int[] anArray = new int [ 5 ]; int aValue = anArray[ 3 ]; anArray[ 0 ] = 9; Although arrays with one index (above, also known as one-dimensional arrays) are the most common, there are times in which is convenient to define arrays with multiple dimensions. That is the case of 2- dimensional arrays, which are useful to represent matrices and board games such as checkers or chess (higher-dimension arrays can also be defined but they are unusual). The example code below shows how to define a 2-by-3 (rows-by-columns) 2-dimensional array of booleans, and access its values. boolean[][] aMatrix = new boolean [ 2 ][ 3 ]; boolean aValue = aMatrix[ 1 ][ 2 ]; aMatrix[ 0 ][ 0 ] = false; You have to be mindful of array indices since using them for values that are outside boundaries is the main error programmers do when using arrays. For example, the only valid indices in an array of 5 values are from 0 (first value) to 4 (last value). Any other indices (for example, 5 or -1) will make the program fail. In general, programmer errors are known as bugs and they cause program failures. Testing is what you do when you run a program with sample data, and it is the first step towards knowing whether the program works or not. In most cases, testing lets you find the source of a failure; at other times, it only shows that there is a bug but not where in your code. If that happens, we must debug the program using tools such as (go figure) a debugger.
11
Embed
Lab 2 Arrays & JUnit Testing · Lab 2 Arrays & JUnit Testing Programs that hold values of the same type benefit from arrays. Once placed in an array, values can be accessed using
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Lab 2 Arrays & JUnit Testing
Programs that hold values of the same type benefit from arrays. Once placed in an array, values can be
accessed using an index, which is a value from 0 up to the number of values in the array: where the first
value is at position 0, the second at position 1, and so on, until the last element, which is located at
index position “length – 1” (where length is the number of values in the array).
As shown in the code-snippet below, array values are accessed using a square-brackets notation. In this
example, we defined “anArray” as an array of 5 integers (initialized as zeroes) from where we get the
value at the fourth element (at index 3) and assign a value of 9 to the first element.
int[] anArray = new int [ 5 ];
int aValue = anArray[ 3 ];
anArray[ 0 ] = 9;
Although arrays with one index (above, also known as one-dimensional arrays) are the most common,
there are times in which is convenient to define arrays with multiple dimensions. That is the case of 2-
dimensional arrays, which are useful to represent matrices and board games such as checkers or chess
(higher-dimension arrays can also be defined but they are unusual). The example code below shows
how to define a 2-by-3 (rows-by-columns) 2-dimensional array of booleans, and access its values.
boolean[][] aMatrix = new boolean [ 2 ][ 3 ];
boolean aValue = aMatrix[ 1 ][ 2 ];
aMatrix[ 0 ][ 0 ] = false;
You have to be mindful of array indices since using them for values that are outside boundaries is the
main error programmers do when using arrays. For example, the only valid indices in an array of 5
values are from 0 (first value) to 4 (last value). Any other indices (for example, 5 or -1) will make the
program fail.
In general, programmer errors are known as bugs and they cause program failures. Testing is what you
do when you run a program with sample data, and it is the first step towards knowing whether the
program works or not. In most cases, testing lets you find the source of a failure; at other times, it only
shows that there is a bug but not where in your code. If that happens, we must debug the program
using tools such as (go figure) a debugger.
This lab has exercises with both 1- and 2-dimensional arrays. In addition, we will write methods, and
practice testing them using JUnit and Eclipse’s debugger. Be aware that this lab may require you to look
for information from different sources, such as your text book, the Java API, or Google.
1. One-Dimensional Arrays In this section we will practice writing methods that use integer arrays of one dimension.
Exercise 1
Create a new Eclipse project named Lab02 with a Lab02One class in it.
In this class you will write methods to make a string with the values in a 1-dimensional array of integers,
and to calculate the sum, and average of the values in the array. The names of the methods you will
create are listed below. You need to use these exact definitions (later it will become clear why):
public static String arrayToString( int[] anArray )
public static int arraySum( int[] anArray )
public static double arrayAverage( int[] anArray )
Follow the steps below to implement these methods:
1. Write the implementation of the method arrayToString.
This method receives an array of integers and returns a string with all values in the array enclosed in
square brackets and separated by a comma and a blank space. For example, the method returns the
string “[4, 2]” when receiving an array with values {4,2}. Note that the method returns a string
rather than printing a string to the screen.
2. Write the implementation of the method arraySum.
This method receives an array of integers and returns an integer with the sum of the array’s values
(or 0 if the array is empty).
3. Write the implementation of the method arrayAverage.
This method receives an array of integer and returns a double value with the average of the values
in the array (or 0 if the array is empty).
Lastly, write a main method with the code shown below.
int arrayInt[] = new int[]{80,100,80,92,95,87,82,76,45,76,80,70};