need programme of following java 2 array,
April 22, 2009 at 10:04 PM
java 2 array Write an application to test utilize arrays in a variety of ways – • you will be calling all of the methods you write from the main, • so remember to make them static
Part 1 1A) In the main: Declare and create 2 arrays called nums1 and nums2 to store 15 integers 1B) Declare a third array called nums3 and assign to it 15 values of your choice (from 1 to 30 – BUT NOT in sorted order ), for example {25,12,6,2,30……}
2A.Write a method called initArray to initialize an array of int to values from 0 to 14 using a loop. 2B.Pass in the nums1 array as a parameter.
3. Write a method called displayArray to display all numbers in an array of int on one line in this format: 0 1 2 3 4 and so on. Pass in the nums1array as a parameter.
4. In the main: In the main method, call the initArray method passing in nums1(see//4A to the right). Print an appropriate message(//4B) before calling the display array(//4C) , so that you r results look like this: The nums1 array after initializing: 0 1 2 3 4 and so on The nums3 array after initializing: 25 12 6 2 30 and so on
5. Write a method called genRandom which takes the array nums2 as a parameter and fills it with random numbers between 1 and 100. in a ‘for’ loop, assign this stmt to each element in the array (int)(Math.random()*99)+1;
6. In the main: add a call to the genRandom methods passing in nums2 followed by an appropriate message explaining what the output will be and then call the displayArray method so your output looks like this: The nums2 array after initializing: 17 4 12 (random numbers – different each time) and so on
7. Write a method called sumNumbers which takes the nums3 array as a parameter. It sums all numbers in the array and returns the sum as the value of the method. (check your sum by adding all the numbers you put in nums3)
8. In the main, add a call to the sumNumbers method, then Print a message telling what the output is followed by the results . For example: The following is the sum of all numbers in the nums3 array (REMEMBER: when a method returns a value, you need to supply a variable to receive the answer in the calling statement. – Print the sum returned)
9. Write a method called underTen which takes the array nums1 as a parameter. It sums all numbers that have a value less than 10 in the array and returns the sum as the value of the method.
10. In the main: add a call to the underTen method, then Print a message telling what the output is followed by the results . For example: The following is the sum of all numbers in the nums1 array that are less than 10
11. Write a method called smallest which takes the array nums3 as a parameter. It finds the smallest of the number in the array and returns it as the value of the method.
12. In the main: first call the displayArray method so your output looks like this: The nums3 array after initializing: 10 5 15 (these should be the numbers you assigned in the create and initialize declaration) and so on
Now, add a call to the smallest method passing in nums3 as a parameter, then Print a message telling what the output is followed by the results . For example: The smallest number in the nums3 array is
13. Write a method called largestIndex which takes the array nums2 as a parameter. It finds the largest of the numbers in the array and returns it’s index as the value of the method.
14. In the main: call the largest method, then Print a message telling what the output is followed by the results . For example: The index of the largest number in the nums2 array is
15. Write a method called search which takes the array nums3 as a parameter and a random number you want to search for. (just pick one out of your array and pass it in as the second parameter) It finds the FIRST instance of that number and returns its index as the value of the method. If it does not find your random number, it returns a -1
16. In the main: call the search method, then Print a message telling what the output is followed by the results . Use a conditional to print either of these statements: For example: The random number 6 was found at location 2 OR The random number 6 was NOT found
17. Write a method called addToOverTwenty which takes the array nums3 as a parameter. It adds 1 to all numbers that have a value greater than 20 in the array . It has no return value.
18. In the main: add a call to the addToOverT wenty method, then Print a message telling what the output is followed by the results of calling the displayArray method. For example: The nums3 array after adding 1 to all numbers greater than 20 is 10 6 15 and so on (check with the values you assigned to nums3)
19. In the main: Use the Arrays class’s static methods sort and binarySearch to complete the following: a. Sort nums3 in ascending order b. Do a binary search on nums3 for a searchNumber you choose i. Output the number and its index which is returned if found ii. If not found, binarySearch returns a negative number so output a not found message c. The Arrays class is in the folder java.util.*; //so add the new import statement!