0% found this document useful (0 votes)
1K views2 pages

BPJ Lesson 18 Exercise

The document provides code snippets and exercises related to arrays in Java. It includes code to: 1) Loop through an array and take the absolute value of each element. 2) Concatenate an array to a string. 3) Find the first negative number in an array. 4) Split a string into an array. 5) List the elements of given arrays. 6) Count occurrences of a substring in a given string.

Uploaded by

api-308024418
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views2 pages

BPJ Lesson 18 Exercise

The document provides code snippets and exercises related to arrays in Java. It includes code to: 1) Loop through an array and take the absolute value of each element. 2) Concatenate an array to a string. 3) Find the first negative number in an array. 4) Split a string into an array. 5) List the elements of given arrays. 6) Count occurrences of a substring in a given string.

Uploaded by

api-308024418
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Exercise on Lesson 18

double

sgt

for
double sgt[]
rtl

one

cr

cr.length
ref

double
heroWorship

dbx
vb

adc
adc[1]
adc

adc[3] + adc[4]

adc

adc[5]

18-7
13. Describe what the following code segment does:
for (int j = 0; j < b.length; j++)
b[j] = Math.abs( b[j] );
14. For the
array
, what would be the output of the following code?
String ss = >>>;
int len = ss.length( );
for (int j = 0; j < len; j++)
ss+= c[j];
System.out.println(ss);
15. Write a loop that locates the first occurrence of a negative integer in an array, .
When the loop is finished, the variable
should contain the index of the negative
number, or the length of the array if there were no negative numbers in the array.
16. String wc = Whooping crane;
String sp[] = wc.split(oo);
for(int j = 0; j < sp.length; j++)
{
System.out.println(sp[j]);
}
\\

17. List the elements of


18. List the elements of

method, write code that will count all of the occurrences of th


19. Using the
(without regard to upper or lower case) in The best THERE is is Barth.

This project called


method,
. The

will consist of just one class,


method will use two -loops:

, that in turn, has just one

The first loop will produce an integer count from 65 to 90 (notice these are the ASCII
codes for characters AZ) and initialize the elements of the character array
with
the characters corresponding to the ASCII codes being generated by the loop. This will
fill the
array as follows: ch[0] = A, ch[1] = B, , ch[25] = Z.
The second loop will print the 26 elements of the
array with one comma followed
by one space between adjacent characters as follows:
A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z

You might also like