Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Calling up array methods from main

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Calling up array methods from main

    I'm having trouble just calling up the methods from main. I just need to test them to see if they work.
    public static void main(String[] args) {
            // TODO code application logic here
        }
     
        public static int randomInt(int low, int high) {
    	int difference = high - low;
    	return (int)(Math.random()*(difference + 1)) + low;
    	}
     
        public static int[] randomIntArray (int n, int low, int high) {
            int[] a = new int[n];
            for (int i = 0; i<a.length; i++) {
            a[i] = randomInt (low, high);
        }
        return a;
      }
     
        public static int indexOfMaxInRange(int[] arr, int lowIndex, int highIndex) {
    	int indexOfMax = lowIndex;
     
    	for (int i = lowIndex+1; i <= highIndex; i++) {
    		if (arr[i] > arr[indexOfMax]) {
    			indexOfMax = i;
    			}
    		}
    		return indexOfMax; 
    	}
     
        public static void swapElement(int[] arr, int index1, int index2) {
    	int temp = arr[index1];
    	arr[index1] = arr[index2];
    	arr[index2] = temp; 
    	}
     
        public static void sort(int[] arr) {
    	int length = arr.length;
    		//use length-1 because do not need to swap last element with itself
    	for (int i = 0; i < length-1; i++) {
    		int indexOfMax = indexOfMaxInRange(arr, i, length-1);
    		swapElement(arr, i, indexOfMax); 
    		}
    	}	
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Calling up array methods from main

    having trouble just calling up the methods
    Please explain what problems you are having.
    Copy the full text of any error messages and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    54
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default Re: Calling up array methods from main

    Since all the methods are static, you just need to call them without creating an instance of the class and make sure you assign a variable that is of the same data type as the return (for your non-void return methods only).

  4. #4
    Member
    Join Date
    Mar 2013
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Calling up array methods from main

    I still don't understand what you mean. Could you provide me with an example?

  5. #5
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Calling up array methods from main

    Could you provide me with an example?
    An example of calling a method?!

    I don't mean to mock you. But read your textbook, notes, or any other source of Java code you have available and give it a go. It doesn't matter at all if the compiler complains about what you try. That's where we all started! Post the code you are trying and the compiler messages as Norm suggests.

  6. #6
    Member
    Join Date
    Mar 2013
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default outputting

    I'm having problems outputting or testing the methods by main.
    public static void main(String[] args) {
     
        }
    public static int randomInt(int low, int high) {
    	int difference = high - low;
    	return (int)(Math.random()*(difference + 1)) + low;
    	}
     
        public static int[] randomIntArray (int n, int low, int high) {
            int[] a = new int[n];
            for (int i = 0; i<a.length; i++) {
            a[i] = randomInt (low, high);
        }
        return a;
      }
     
      public static int indexOfMaxInRange(int[] arr, int lowIndex, int highIndex) {
    	int indexOfMax = lowIndex;
     
    	for (int i = lowIndex+1; i <= highIndex; i++) {
    		if (arr[i] > arr[indexOfMax]) {
    			indexOfMax = i;
    			}
    		}
    		return indexOfMax; 
    	}
     
        public static void swapElement(int[] arr, int index1, int index2) {
    	int temp = arr[index1];
    	arr[index1] = arr[index2];
    	arr[index2] = temp; 
    	}
     
        public static void sort(int[] arr) {
    	int length = arr.length;
    	for (int i = 0; i < length-1; i++) {
    		int indexOfMax = indexOfMaxInRange(arr, i, length-1);
    		swapElement(arr, i, indexOfMax); 
    		}
    	}

  7. #7
    Member
    Join Date
    Apr 2012
    Posts
    42
    Thanks
    8
    Thanked 4 Times in 4 Posts

    Default Re: outputting

    Well, how do you call a method? From just a quick overview I can say those are some nice looking methods. All your methods have the parameters that are passed in already written in their headers, so in your main you have to call those methods and pass in the appropiate variables. Just do one at a time and make sure each one works before going to the next one.

  8. #8
    Member
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    54
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default Re: Calling up array methods from main

    Quote Originally Posted by my21 View Post
    I still don't understand what you mean. Could you provide me with an example?
    No offence but, how is it you can write your own methods, use static, return statements and single-dimension arrays, yet don't know how to call them? Use whatever source(s) you're studying or learning from to see examples.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Member
    Join Date
    Mar 2013
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Calling up array methods from main

    I got it! Thanks for your help!

  11. #11
    Junior Member
    Join Date
    Oct 2013
    Posts
    11
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Calling up array methods from main

    Re: Calling up array methods from main
    Try this with some of your own effort...
    public static void main(String[] args) {
    int rndInt = randomInt(int low, int high);
    int arr[]= randomIntArray (int n, int low, int high);
    int MaxRange = indexOfMaxInRange(randomIntArray (int n, int low, int high), int lowIndex, int highIndex);
    System.out.println( sort(randomIntArray (int n, int low, int high)) );
    }

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Calling up array methods from main

    @arnavkumartechno
    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    BTW This thread is over 6 months old
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Oct 2013
    Posts
    11
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Calling up array methods from main

    Thank you norm, i am new with the forums. Thank you i will take care of it in future.

Similar Threads

  1. Problem with calling objects from same-class methods in main
    By thekbon in forum Object Oriented Programming
    Replies: 12
    Last Post: December 18th, 2012, 01:10 AM
  2. Calling upon methods
    By jonathanfox in forum Java Theory & Questions
    Replies: 4
    Last Post: August 3rd, 2012, 11:58 AM
  3. Calling methods in other files
    By zdavos in forum Object Oriented Programming
    Replies: 4
    Last Post: March 2nd, 2012, 07:57 AM
  4. [SOLVED] Calling methods from other classes
    By knightknight in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 7th, 2011, 06:16 PM
  5. Calling for methods
    By soccer_kid_6 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 1st, 2010, 12:13 AM