# Help on Static Methods

• 04-16-2012, 08:42 PM
tdub
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");
}
• 04-16-2012, 08:59 PM
shall
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.
• 04-16-2012, 09:02 PM
JosAH
Re: Help on Static Methods

kind regards,

Jos
• 04-16-2012, 10:54 PM
tdub
Re: Help on Static Methods
Quote:

Originally Posted by JosAH

kind regards,

Jos

thanks for the help
• 04-16-2012, 11:12 PM
DarrylBurke
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
• 04-16-2012, 11:15 PM
tdub
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?!
• 04-16-2012, 11:19 PM
tdub
Re: Help on Static Methods
Quote:

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

• 04-16-2012, 11:29 PM
shall
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.
• 04-17-2012, 12:53 AM
tdub
Re: Help on Static Methods
how would the 'for' loop look?
• 04-17-2012, 01:11 AM
shall
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)).
• 04-17-2012, 01:19 AM
shall
Re: Help on Static Methods
Sorry, I should have said length property. If an array is a[], then its length would be a.length.
• 04-17-2012, 01:29 AM
tdub
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;
• 04-17-2012, 01:49 AM
shall
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.

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)
• 04-17-2012, 01:51 AM
shall
Re: Help on Static Methods
• 04-17-2012, 10:49 PM
tdub
Re: Help on Static Methods
Quote:

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.

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.
• 04-17-2012, 11:36 PM
pbrockway2
Re: Help on Static Methods
Quote:

a static method that returns the first location of x in numbers. If x is not an element, return -1.
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.