How to solve java method: findLast, findMax, findIndexOfMax, Range....?
-
I'm totally new to java....and got stuck with this homework....(due for the next couple days): I have done 3 of them and stuck at question #4.... I don't expect anyone to do the work, just to help me out to get the idea of how to do it or something like that.... 1.Write and test a method count() that MUST USE a for_each loop to count and return the number of elements in an int array e.g. int a[] = {7,8,9,9,8,7}; System.out.println(count(a)); <- outputs 6. 2. Write and test a method sum() that calculates and returns the sum of the elements in an int array e.g. int a[] = {7,8,9,9,8,7}; System.out.println(sum(a)); <- outputs 48 3.Write and test a method average() that calculates and returns the average (in floating point) of the elements in an int array e.g. int a[] = {7,8,9,9,8,7}; System.out.println(average(a)); <- outputs 8.0 4. Write and test a method findLast() that searches an int array for the last occurrence of a value. Returns the index if found, -1 if not found e.g. int a[] = {7,8,9,9,8,7}; System.out.println(findLast(a, 8)); <- outputs 4 System.out.println(findLast(a, 2)); <- outputs -1
-
Answer:
you use a for loop... private int findLast( int[] arr, int value ) { int indexOf = -1; for( int i = 0; i< arr.length; i++) { if( arr[i] == value ) { indexOf = i; } } return indexOf; }
sore at Yahoo! Answers Visit the source
Related Q & A:
- How To Solve Informatics Olympiad?Best solution by sirupsen.com
- How To Solve Problems Creatively?Best solution by Quora
- how to solve functional equation?Best solution by Mathematics
- How to solve such an optimization problem efficiently??Best solution by Theoretical Computer Science
- How to Solve nonlinear system by newton method?Best solution by Mathematics
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.