Intro to Parameters

Hello,

This is my first post.

I need some help with parameters. I am new to java and am having a little difficultly understanding how exacly they work.

I have searched various forums including this one and could not get a simple description of how they work.

I am a first year student and have understood subprograms and global variables ok, and that parameters should be used where possible rather than global variables.

IIf anyone can help, break it down for me or direct me where I can find out about them it would be much appreciated.

Thanks in advance.

[602 byte] By [Rory_Ta] at [2007-11-26 18:55:57]
# 1
Here you go: http://java.sun.com/docs/books/tutorial/java/javaOO/arguments.html
zadoka at 2007-7-9 20:34:34 > top of Java-index,Java Essentials,New To Java...
# 2

Thanks for the fast reply.

just to clarify. in the main program, (public static void main (String[]args), do I declare these parameters again eg. double loanAmt etc before I call the method below.

Or do I just call the method below as in: computePayment ( loanAmt, rate etc)?

Thanks in advance.

public double computePayment(double loanAmt,

double rate,

double futureValue,

int numPeriods) {

double interest = rate / 100.0;

double partial1 = Math.pow((1 + interest), -numPeriods);

double denominator = (1 - partial1) / interest;

double answer = (-loanAmt / denominator)

- ((futureValue * partial1) / denominator);

return answer;

}

Rory_Ta at 2007-7-9 20:34:34 > top of Java-index,Java Essentials,New To Java...
# 3

> just to clarify. in the main program, (public static

> void main (String[]args), do I declare these

> parameters again eg. double loanAmt etc before I call

> the method below.

>

> Or do I just call the method below as in:

> computePayment ( loanAmt, rate etc)?

>

You can declare them in the main method or just call it directly. If you aren't using them somewhere else calling it directly might be preferred.

zadoka at 2007-7-9 20:34:34 > top of Java-index,Java Essentials,New To Java...
# 4
Thanks again.
Rory_Ta at 2007-7-9 20:34:34 > top of Java-index,Java Essentials,New To Java...
# 5

here is an example for you.

my method to calculate the largest element in an array.

class Tester1 {

public int getMax( int[] A) {

int largest =0;

for(int i=0; i<A.length; i++)

if(A[i] > largest)

largest = A[i];

return largest;

}

and now i have another class which contains a main method. you need to create an objects of the class so then you can call the method. here is my main method.

class ArrayEg1 {

public static void main(String[] args) throws IOException {

int[] myArray= new int[5];

Scanner s = new Scanner(System.in);

Tester1 test = new Tester1(); //create an object based on my Tester1 class

System.out.println("Enter any five numbers ");

for(int i=0; i<myArray.length; i++) {

myArray[i] = s.nextInt();

}

System.out.println(" The max is " +test.getMax(myArray)); //invoking my getMax method.

}

}

hope this helps.

its good to have methods define outside the main method.

Message was edited by:

fastmike>

fastmikea at 2007-7-9 20:34:34 > top of Java-index,Java Essentials,New To Java...