# Thread: Help on Static Methods

1. Member
Join Date
Apr 2012
Posts
15
Rep Power
0

## Help on Static Methods

This is what I need to do.

The methods to be implemented are listed below:
1. boolean find(int x, int[] numbers): If x is an element of numbers, then return true, otherwise return false.
2. int indexOf(int x, int[] numbers): Returns the first location of x in numbers. If x is not an element, return -1.
3. int lastIndexOf(int x, int[] numbers): Returns the last location of x in numbers. If x is not an element, return -1.
4. int elementsGreater(int x, int[] numbers): Returns the number of elements that are strictly greater than x in the numbers array. Returns 0 if no element is greater than x.
5. int elementsSmaller(int x, int[] numbers): Returns the number of elements that are strictly smaller than x in the numbers array. Returns 0 if no element is smaller than x.
6. int elementsEqual(int x, int[] numbers): Returns the number of times x occurs in the numbers array. Returns 0 if x does not occur in numbers.
7. int[] indexOfElementsEqual(int x, int[] numbers): Returns an array containing the indices of the numbers array, which have the value x. For example, if the array is {2, 4, 2, 3, 4, 2}, and x=2, the array returned is {0, 2, 5}. If x is not present in numbers, then null is returned.

Then we must test the program. Here is the code I have come up with to do that

import java.util.Arrays

public static void main(String[] args) {

int[] testArray = {6, 1, 4, 9, 2, 4, 2, 3, 2, 1};

// Find an element that does exist
System.out.print("Test 1...find...");
if(find(2, testArray))
System.out.println("passed");
else
System.out.println("failed");

// Find an element that does not exist
System.out.print("Test 2...find...");
if(find(10, testArray))
System.out.println("failed");
else
System.out.println("passed");

// Last index of an element that exists multiple times
System.out.print("Test 3...lastIndexOf...");
if(lastIndexOf(1, testArray)==9)
System.out.println("passed");
else
System.out.println("failed");

// Last index of an element that does not exist
System.out.print("Test 4...lastIndexOf...");
if(lastIndexOf(10, testArray)==-1)
System.out.println("passed");
else
System.out.println("failed");

// Array containing indexes of an element that exists multiple times
System.out.print("Test 5...indexOfElementsEqual...");
int[] expectedArray = {1, 9};
int[] resultArray = indexOfElementsEqual(1, testArray);
if(Arrays.equals(expectedArray, resultArray))
System.out.println("passed");
else
System.out.println("failed");
}

2. Senior Member
Join Date
Apr 2012
Posts
199
Rep Power
0

## Re: Help on Static Methods

I'm not sure exactly what you want, but I'll try to give you some direction:

I think you will want two classes: a test class that holds the main function shown above and another class which contains all the method described above. The test class should create an object from the other class. Therefore, you'll need to create an object in the main function (shown above) and use that object to call it methods.

3. ## Re: Help on Static Methods

kind regards,

Jos

4. Member
Join Date
Apr 2012
Posts
15
Rep Power
0

## Re: Help on Static Methods

Originally Posted by JosAH

kind regards,

Jos
thanks for the help

5. ## Re: Help on Static Methods

Quit multiposting. I've closed the ancient thread you hijacked and another new thread you started with a part of the question posted here.

db

6. Member
Join Date
Apr 2012
Posts
15
Rep Power
0

## Re: Help on Static Methods

I'm not sure of the formatting that I should be using when writting the static method. I think with the 1st problem the code would look like this:
public static boolean find(int x, int[] numbers){
if (int x/1==x)
return true;
return false;

Does this look right?!

7. Member
Join Date
Apr 2012
Posts
15
Rep Power
0

## Re: Help on Static Methods

Originally Posted by DarrylBurke
Quit multiposting. I've closed the ancient thread you hijacked and another new thread you started with a part of the question posted here.

db
thanks for the advice dbag

8. Senior Member
Join Date
Apr 2012
Posts
199
Rep Power
0

## Re: Help on Static Methods

No, it does not look right :).

numbers is an array of integer numbers, for example, numbers could equal {1,2,3,4,5} or { 8,10,30,40,80,100} or any array of integers.

The find function should return true when it finds the x in the numbers array. Using an "if" (i.e. if (x == numbers[i]) ) in a "for loop" will work.

9. Member
Join Date
Apr 2012
Posts
15
Rep Power
0

## Re: Help on Static Methods

how would the 'for' loop look?

10. Senior Member
Join Date
Apr 2012
Posts
199
Rep Power
0

## Re: Help on Static Methods

I think it would best to go through the Java language basics at The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics).

Also, you will need the size of the array which can be found by using the arrays length member function (Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)).

11. Senior Member
Join Date
Apr 2012
Posts
199
Rep Power
0

## Re: Help on Static Methods

Sorry, I should have said length property. If an array is a[], then its length would be a.length.

12. Member
Join Date
Apr 2012
Posts
15
Rep Power
0

## Re: Help on Static Methods

why isnt this working?

public static boolean main(String[] args) {

public static boolean find (int x, int[] numbers){
int i = 0;

for(i=0; i<numbers.length; i++)
if (x==numbers[i])
return true;
return false;

13. Senior Member
Join Date
Apr 2012
Posts
199
Rep Power
0

## Re: Help on Static Methods

Post your code with code tags.

If you put a bracket on the next line after "return false;" you will be including the boolean function within the main function, which is incorrect. Its also incorrect without the braces.

Also, is that all of the code? Where is it located? Did you you put it in a separate class and a separate file? See Post #2.

Java Code:
```public static boolean find (int x, int[] numbers)
{
for(int i=0; i<numbers.length; i++)
{
if (x==numbers[i])
{
return true;
}
}

return false;
}```
As shown in the code surround the contents of the if and for contents with braces.

What IDE are you using. I suggest using the Netbeans IDE located at http://netbeans.org/downloads/ (select the one from the "all" column)

14. Senior Member
Join Date
Apr 2012
Posts
199
Rep Power
0

## Re: Help on Static Methods

Read Guide For New Members to learn about code tags.

15. Member
Join Date
Apr 2012
Posts
15
Rep Power
0

## Re: Help on Static Methods

Originally Posted by shall
Post your code with code tags.

If you put a bracket on the next line after "return false;" you will be including the boolean function within the main function, which is incorrect. Its also incorrect without the braces.

Also, is that all of the code? Where is it located? Did you you put it in a separate class and a separate file? See Post #2.

Java Code:
```public static boolean find (int x, int[] numbers)
{
for(int i=0; i<numbers.length; i++)
{
if (x==numbers[i])
{
return true;
}
}

return false;
}```
As shown in the code surround the contents of the if and for contents with braces.

What IDE are you using. I suggest using the Netbeans IDE located at http://netbeans.org/downloads/ (select the one from the "all" column)
I was wondering how to to the second part. a static method that returns the first location of x in numbers. If x is not an element, return -1.

16. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12

## Re: Help on Static Methods

a static method that returns the first location of x in numbers. If x is not an element, return -1.
Java Code:
```{
for(int i=0; i<numbers.length; i++)
{
if (x==numbers[i])
{
// do you mean return this i....
return true;
}
}

// ...or return -1?
return false;
}```
Honestly, though, you should post what you've tried. This shows that you have tried (but we all assume that!) and it will also let everyone know in what directions your thoughts are running.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•