Results 1 to 16 of 16
  1. #1
    tdub is offline Member
    Join Date
    Apr 2012
    Posts
    15
    Rep Power
    0

    Default 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. #2
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default 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. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default Re: Help on Static Methods

    There's nothing advanced about this thread and its questions; moved to the New To Java section.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    tdub is offline Member
    Join Date
    Apr 2012
    Posts
    15
    Rep Power
    0

    Default Re: Help on Static Methods

    Quote Originally Posted by JosAH View Post
    There's nothing advanced about this thread and its questions; moved to the New To Java section.

    kind regards,

    Jos
    thanks for the help

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default 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
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    tdub is offline Member
    Join Date
    Apr 2012
    Posts
    15
    Rep Power
    0

    Default 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. #7
    tdub is offline Member
    Join Date
    Apr 2012
    Posts
    15
    Rep Power
    0

    Default Re: Help on Static Methods

    Quote Originally Posted by DarrylBurke View Post
    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. #8
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default 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. #9
    tdub is offline Member
    Join Date
    Apr 2012
    Posts
    15
    Rep Power
    0

    Default Re: Help on Static Methods

    how would the 'for' loop look?

  10. #10
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default 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. #11
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default 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. #12
    tdub is offline Member
    Join Date
    Apr 2012
    Posts
    15
    Rep Power
    0

    Default 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. #13
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default 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. #14
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Help on Static Methods

    Read Guide For New Members to learn about code tags.

  15. #15
    tdub is offline Member
    Join Date
    Apr 2012
    Posts
    15
    Rep Power
    0

    Default Re: Help on Static Methods

    Quote Originally Posted by shall View Post
    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. #16
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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.

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. Replies: 1
    Last Post: 10-23-2011, 12:37 PM
  3. Static and non static class methods question
    By silverglade in forum New To Java
    Replies: 2
    Last Post: 05-14-2011, 10:10 PM
  4. Recursion with static and non static methods
    By sh4dyPT in forum New To Java
    Replies: 14
    Last Post: 03-27-2009, 06:56 AM

Tags for this Thread

Posting Permissions

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