Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

CSCI 281 INTRODUCTION TO COMPUTER SCIENCE II

Lab Manual
Module 3:
Working with arrays

DR. CATHERINE ANDERSON MCNEESE STATE UNIVERSITY


C HAPTER 1

Lab 5
Initializing and
analyzing arrays

Initializing an array with random arrays


and printing out the content.
S ECTION 1 The static format method from the String class:

Aligning columns of num- Another very useful static method from the String class is the
method format. The format indicator technique that origi-
ber nated in the C programming language and similar to the for-
mat method in the python language can be used here.

Below is the official documentation of the of the method for


the Java api.

COLUMNS OF NUMBER

1. integers
2. floating point number

2
To give you an example: you have the
four integers: 55, 2619, 451 and 15451,
and you want to from a display as sown
to the right. In order to do so, you would
have to form a formatting statement with If the column in to small, as shown below, the excess digits of
format indicators and then give the state- the number extend to the right of the column, as shown be-
ment a value(s) to work on. low.

The format indicator starts with the % and the number indi-
cated the width of the column. Then every number is aligned
from the right edge of the column. The printout to the right of
the green square is the actual display as formed by the pro-
gram.
With java, it is possible to replace the number between the %
and d with an integer variable. This would allow the width of
the column to be determined automatically by the program.
An example is shown below.

3
Formatting floating point numbers: The example below show one more decimal place added to
the display. This would require that each number be in-
A similar format indicator is provided for the floating point
creased, as shown below.
numbers. This starts with the %. The main difference is that
there are two numbers separated by a decimal point. The first
number is the total number of character in the display includ-
ing the decimal point. Then there is a decimal point and a
second number. This second number indicates the number of
decimal places you would like to show. Immediately below is
a sample of code that shows four doubles initialized and then
printed out using this form.

You should noticed that the value to the right of the decimal
point is truly rounded, not just truncated.

4
S ECTION 2 Processing arrays

Analyzing arrays In this lab, you will declare and initialize two arrays of ran-
dom numbers. There is a code-along video that will get you
started on writing methods that pass and return arrays. You
will be shown how to write a method that will initialize an ar-
ray and print out an array. From there, you will be responsi-
ble to modify the code to create a solution to the problem de-
scribed below. This means adding a second array as argu-
ment to the printArrays method.
P ROCESSING AN ARRAY
Problem requirements;
1. Initializing two arrays with random number
• You must implement a method called printTwoArrays
2. Printing out two array
that will take two arrays and print them to console, side by
3. Comparing the elements of two arrays side as shown in the image to the right.

• You must implement a method call compareArrays that


will compare two arrays to determine which array had the
most “position by position” values that were greater than the
other. You should also provide for ties. For example for the
array content example below:

content of array 1: 5, 8, 13, 24, 11, 56, 13


content of array 2: 7, 4, 3, 6, 33, 15, 5
array 1 has 5 positions with higher values then array 2, while
array 2 has only 2 higher values then array 1. There are no
ties.

5
If the size of the arrays is 20 and the range is between 1 and range should be defined by the variables low_limit and
50, the output below is a an appropriate output. Please note, high_limit. These two variables should be passed t0 the
you MUST right align all integers. methods to initialize the array.

- Implement a new method that takes two arrays as argu-


ments print them out as shown to the left, with all integers
right justified so that units place of all numbers in a column
are aligned. The method must be call printTwoArrays(...)

- Implement a new method that will count the number of


times each array has the higher number, when an element by
element comparison is done. It will then print out the re-
sults of this comparison. The method should be called com-
pareArray(...) and it should take two arrays of equal
length and return nothing.

The example below shows the required format of the output


given the arguments specified.

Example output for elements <-- 8,


low_limit <-- 40 and high_limit <-- 90

Required program flow for this lab

- Declare two arrays of int data type. The size should be de-
clared using a variable called elements. The program
should run with this variable set to any value.

- Use the methods implemented in the code along to initialize


each array. The arguments for the limits for the random

6
You are to run the test cases outlined below, take screen shots
of the results (printout of the array content and the compari-
son results, just as shown here in the manual) and paste the
images into your lab sheet. You should keep running your pro-
gram until you get at least one tie.

Test case 1 - Array size - 25


Low Limit - 110
High Limit - 130

Test case 2 - Array size - 10


Low Limit - 10
High Limit - 15

You lab will be evaluated on the criteria shown in the tables


provided at the submission site on Moodle.

You might also like