Univeristy of Nizwa



| | | |

|Univeristy of Nizwa |[pic] |2011/2012 |

|College of Economics, Management and | |Dr. Tarek Guesmi |

|Information Systems | | |

| |

|Computer Systems Concepts |

|LAB5 : Arrays |

Objectives:

Upon completion of this lab, you should be able to:

● Declare, create, and manipulate one-dimensional primitive arrays

● Use an array to represent a one-to-many object relationship

Online resources:

Eduwave: Chapter5-presentation



Exercise 1

Tasks I

• Create the TestArrays class with the following characteristics:

Class Name: TestArrays

Project: ArrayProject

Location: Source Packages

Package: default package

• Add a main method. In the main method, declare two variables called array1 and array2. They should be of type int[] (array of int).

• Using the curly-brace notation, {}, initialize array1 to the first eight prime numbers: 2, 3, 5, 7, 11, 13, 17, and 19.

• Display the contents of array1. You might want to use the printArray method (see ‘‘Hint’’) to display these

integer arrays.

• Assign the array2 variable equal to the array1. Modify the even indexed element in array2 to be equal to the index value (for example, array2[0] = 0; and array2[2] = 2; and so on). Print out array1.

|Hint |

|The printArray support method might help you to solve this exercise, asfollows: |

|public static void printArray(int[] array) { |

|System.out.print(’’); |

|} |

| |

|Note – You can also use the java.util.Arrays.toString method to |

|generate a string representation of an array. For example: |

| |

|System.out.println(Arrays.toString(array1)); |

Tasks II

• Make an array of 5 integers.

• Use a for loop to set the values of the array to the index plus 10.

• Use a for loop to print out the values in the array.

• Make an array of strings initialized to Frank, Bob, and Jim using the variable initializer syntax.

• Use a for loop to print out the strings in the array.

• Set the last element of the array to Mike.

• Print out the last element in the array.

Exercise 2

Write a program that will read a sequence of positive real numbers entered by the user and will print the same numbers in sorted order from smallest to largest. The user will input a zero to mark the end of the input. Assume that at most 100 positive numbers will be entered.

Exercise 3

Write a java class UtilArr with the following static methods:

• Sum that provides the total of the cells values.

• Incre which increments all the values ​​in an array by a given value.

Exercise 4

Implement a Java function that calculates the sum of digits for a given char array consisting of the digits '0' to '9'. The function should return the digit sum as a long value.

Exercise 5

Implement a Java function that finds two neighbouring numbers in an array with the smallest distance to each other. The function should return the index of the first number. In the sequence 4 8 6 1 2 9 4 the minimum distance is 1 (between 1 and 2). The function should return the index 3 (of number 1).

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download