Results 1 to 16 of 16
Thread: Help on Static Methods
- 04-16-2012, 09:42 PM #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");
}
- 04-16-2012, 09:59 PM #2
Senior Member
- Join Date
- Apr 2012
- Posts
- 211
- 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.
- 04-16-2012, 10:02 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: Help on Static Methods
There's nothing advanced about this thread and its questions; moved to the New To Java section.
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 04-16-2012, 11:54 PM #4
Member
- Join Date
- Apr 2012
- Posts
- 15
- Rep Power
- 0
- 04-17-2012, 12:12 AM #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.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 04-17-2012, 12:15 AM #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?!
- 04-17-2012, 12:19 AM #7
Member
- Join Date
- Apr 2012
- Posts
- 15
- Rep Power
- 0
- 04-17-2012, 12:29 AM #8
Senior Member
- Join Date
- Apr 2012
- Posts
- 211
- 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.
- 04-17-2012, 01:53 AM #9
Member
- Join Date
- Apr 2012
- Posts
- 15
- Rep Power
- 0
Re: Help on Static Methods
how would the 'for' loop look?
- 04-17-2012, 02:11 AM #10
Senior Member
- Join Date
- Apr 2012
- Posts
- 211
- 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)).
- 04-17-2012, 02:19 AM #11
Senior Member
- Join Date
- Apr 2012
- Posts
- 211
- 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.
- 04-17-2012, 02:29 AM #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;
- 04-17-2012, 02:49 AM #13
Senior Member
- Join Date
- Apr 2012
- Posts
- 211
- 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; }
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, 02:51 AM #14
Senior Member
- Join Date
- Apr 2012
- Posts
- 211
- Rep Power
- 0
Re: Help on Static Methods
Read http://www.java-forums.org/forum-gui...w-members.html to learn about code tags.
- 04-17-2012, 11:49 PM #15
Member
- Join Date
- Apr 2012
- Posts
- 15
- Rep Power
- 0
- 04-18-2012, 12:36 AM #16
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 18
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; }
Similar Threads
-
Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04-17-2012, 12:00 AM -
difference between static methods and instantce methods?
By venkatch in forum New To JavaReplies: 1Last Post: 10-23-2011, 01:37 PM -
Static and non static class methods question
By silverglade in forum New To JavaReplies: 2Last Post: 05-14-2011, 11:10 PM -
Recursion with static and non static methods
By sh4dyPT in forum New To JavaReplies: 14Last Post: 03-27-2009, 07:56 AM
Bookmarks