Docsity

Processing Arrays Using Loops: A Java Programming Lab Report, Study Guides, Projects, Research of C programming

Partial preview of the text

Download Processing Arrays Using Loops: A Java Programming Lab Report and more Study Guides, Projects, Research C programming in PDF only on Docsity!

LAB REPORT:

SUBJECT Basic arrays

SUBMITTED BY: SAMIA

REG. NO: 17JZBCS

DEPARTMENT: CS&IT

TITLE: Processing arrays using loops

Exercise 1:

- In BlueJ, create a new project called Lab - Create a class in that project called MyArrays - Write a method in that class called arrayPlay that takes no parameters and returns no value - In arrayPlay, create an array called myNums that can hold 20 integers. - What are the valid indexes for this array? SOLUTION: Figure I

Exercise 2:

- Create a Random object for generating random numbers. Remember to import the java.util.Random class. - Use a for loop to fill the myNums array with random integers in the range 100 to 200. - Make sure the program compiles, though running it at this point still won't produce any output. - Could you have filled the array with a for-each loop instead?

SOLUTION:

Figure 4

Exercise 4:

- Print a blank line to separate the initial values from the following output. - Use a regular for loop to print every third value in the array starting with the first one. - That is, print the first value, the fourth value, the seventh value, etc.

- Once again: could a for-each loop be used for this? SOLUTION: Figure 5

Exercise 5:

- Print another blank line. - Now print the values in the array backwards, starting with the last position and moving to the first. - Regular for loop or for-each loop?

SOLUTION:

Figure IV Figure V

Exercise 7:

- Print another blank line. - Now print the index AND the value for each value that's over 170 - For example: 4 184 12 172 14 190 18 179 - Run your program several times. Results will vary.

SOLUTION:

Figure VI Figure VII

Exercise 8:

- Now write separate method called countValues that accepts an integer array as a parameter. Call the parameter list. - The method should return the number of values in the array that are less than 150. - In the arrayPlay method, call the countValues method, passing in the myNums array, and print the result: The number of values less than 150 are xxx