Professional Documents
Culture Documents
CSCI 281 Lab5
CSCI 281 Lab5
Lab Manual
Module 3:
Working with arrays
Lab 5
Initializing and
analyzing arrays
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.
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.
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.
- 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.
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.