Calling Methods In Java

Man of Honour
Joined
15 Mar 2004
Posts
28,140
Location
Liverpool
Hi, I was wondering how to call certain methods in Java?

I have a class file which contains different methods, and I'm supposed to create a main.java file such that the results of these is called and I can then display it. I'm not sure how to do this though.

One of the methods is:

Code:
static int[ ] Method1(int[ ] L, int n)

and to call it we've been told that:

Given a n-element array, L, whose value are distinct integers, the statement
Code:
P = Sorting.Method1(L,n);
will return an n + 1 element integer array in
which {P[1], P[2], . . . , P[n]} hold all of the elements in L sorted into ascending order, i.e. for each element L some position j in P will have P[j] = L; furthermore for each k with 1 ≤ k < n, the value P[k] will be less than the value P[k + 1].

Any help recieved will be gratefully receieved. Quite stuck on this :(

Edit - letters have been changed because otherwise it's confusing..
 
Last edited:
You need to make an object called Sorting out of the class containing the method Method1. You then need to implement the logic you stated at the end of your post in Method1 which returns an int array based on 2 input arguments. By saying P = something, P must be the correct data structure to take the return value of Method1, in this case an int array. I suggest working out the logic on paper before implementing it in the method. Let me know if you need further explanation.

Hope this helps.
 
By the look of what you have posted then there would be no need to instantiate the Sorting object (as Ladforce suggested) as you are calling a static method. Static methods belong to the class and not an object so they are availiable where no instance of the objecte exists which means you no dont have to construct one. So instead of this:

Code:
Sorting sort = new Sorting();
sort.Method();

You can just do this:

Code:
Sorting.Method();

As Ladforce mentioned you do need to work out the method logic though. A method takes a certain number of inputs (called parameters) and then does some sort of operations with them which is then returned as an output (called a return value) to the calling context.

In your case the method you have described takes two parameters, an array of integers and a single integer, it then performs some sort of operation with these (sorting by the description you gave) and returns an array of integers.

You need to work out what the method is going to do, get some paper and then write out a call to the method with some sample data, you should then be able to work out what the output should be before writing any code, once your happy with what it needs to do you can sort out how it will do it. If you have no idea how to get from the input to the output then you will have no luck writing any code so sort this out first.

Edit: All those letters confuse me in your post, I think I understand most of it but I can't figure out what the 'n' parameter does.
 
Last edited:
Grrr, god damnit, I've done the bulk of the programming - but it's giving me two errors that I for the life of me can't figure out. Can anybody help I'm going potty over it.

Code:
import java.util.*;
import java.util.Random;

public class Main {
    
private int [] PermTwo;
private Random RandomSequence;
private int size = 100;              
private int counter2;
private int[] SortA, SortB, SortC, SortD;
private int[] Permutation;


public Main() {
    
Permutation = new int[size];        

SortA = new int[size+1];
SortB = new int[size+1];
SortC = new int[size+1];
SortD = new int[size+1];

RandomSequence = new Random();
}

	public void MethodOne()	{
	SortA = new int[size+1];
	// So we can use the sorting class
	//Sorting Sort[COLOR="Yellow"][B]*[/B][/COLOR] = new Sorting(); 
        SortA = Sorting.SortMethodA(Permutation,size);//Sorting?
	System.out.println("The run-time of method A is; " + SortA[0]);
        }
        //

        public void MethodTwo()	{
	SortB = new int[size+1];
	// So we can use the sorting class
	//Sorting Sort[COLOR="Yellow"][B]*[/B][/COLOR] = new Sorting(); 
        SortB = Sorting.SortMethodB(Permutation,size);//Sorting?
	System.out.println("The run-time of method B is; " + SortB[0]);
        }
               
        public void MethodThree()	{
	SortC = new int[size+1];
	// So we can use the sorting class
	//Sorting Sort[COLOR="Yellow"][B]*[/B][/COLOR] = new Sorting(); 
        SortC = Sorting.SortMethodC(Permutation,size);//Sorting?
	System.out.println("The run-time of method C is; " + SortC[0]);
        }
        
        public void MethodFour()	{
	SortD = new int[size+1];
	// So we can use the sorting class
	//Sorting Sort[COLOR="Yellow"][B]*[/B][/COLOR] = new Sorting(); 
        SortD = Sorting.SortMethodD(Permutation,size);//Sorting?
	System.out.println("The run-time of method D is; " + SortD[0]);
        }  
        
// Random Method
	
public void RandomPermTwo()
    {
    int k;
    int NumberLeft;
    int temp;
    NumberLeft = size;
    while (NumberLeft>0)
      {
      counter2++;
      k = RandomSequence.nextInt(NumberLeft);
      temp = PermTwo[NumberLeft-1]; PermTwo[NumberLeft-1] = PermTwo[k]; PermTwo[k] = temp;
      NumberLeft--;
      };
    };
    
  public int[] GetPermTwo()
    {
    return PermTwo;
    };    
        
    public static void main(String[] args) {
        // TODO code application logic here
        }
    }

* = Cannot find symbol? :confused:
 
Last edited:
SortMethodA is defined in the sort.class file which is something that is already provided. And the class is saved in the same folder as Main.java. Netbeans acknowledges its existance...

I have tried loading it from command prompt too, but I get the same error :(
 
//Sorting Sort = new Sorting();
SortA = Sort*.SortMethodA(Permutation,size);

As you've commented out the "Sorting Sort = new Sorting()" lines, then shouldn't the next line read:

SortA = Sorting.SortMethodA(Permutation,size);
 
Thanks for the suggestion - but I'm getting the same errors again :(.

I think I have to slightly alter the properties of the files themselves. Though right clicking the class and java files in Netbeans doesn't allow me to do this (options are all greyed out) :/
 
Do you have a class called Sort with a method called SortMethodB(int[], int) in the same package as the class you are trying to use it in and does it compile without errors?

A "Cannot find symbol" error means that Java doesn't have any idea where the bit of code you are trying to call is, it is a rather generic error and doesn't give you any help identifying the problem. You could try an create a new class that makes a call to that method with some same data to see what it does.
 
I have just tried to create another class and copied the code over from the class file ;).

Sorting.SortMethodA(int,int); oddly enough appears to work...but obviously I'm not allowed to do this if you know what I mean..
 
I don't think we can help you much without having all of the code as the problem could be anywhere, if you posted all of the classes used (including provided ones) then we might be able to track down a bug (or it may be a configuration error if it compiles for other people as is often the case with Java).
 
Would it be ok to add you onto msn please? I appreciate if you're too busy but they're not huge classes or anything it's probably something I've just overlooked after staring at a computer screen for too long. My n1 ck is my username uncapitalised at hotmaledotcom. :)
 
Back
Top Bottom