I really need help understanding this, I have to write 200-300 words due this week though I can not seem to understand what little my book tells me about it. Sadly this is all i got from my book...Multiple parameters can be declared by specifying a comma-separated list.
Arguments passed in a method call must be consistent with the number, types and order of the parameters. Sometimes called formal parameters. I know there has to be more to it, can someone please explain in English or direct me to a site that can explain it in simple terms maybe I can grasp.

Recommended Answers

All 9 Replies

Does your book not have any examples at all? No code, just descriptive text?

public void oneParamMethod(int firstParam){
...
public void threeParamMethod(int firstParam, double secondParam, String thirdParam){
...

Correct me if I'm wrong but do you mean Varargs?

Example...

public class VarArgTest{

   public static void main(String[] args){
          String values1[] = {"Mom", "Dad", "Sis"};          

          VarArgTest.myVarArgMethod(values1);
          VarArgTest.myVarArgMethod(" ", " ");
          VarArgTest.myVarArgMethod("Yo", "This", "Rocks", "My", "Socks");

   }

   public static void myVarArgMethod(String... strings){

         for(String element : strings)
             System.out.println(element);

   }
}

Here is what my book has: Though I do not understand the example:
// Fig. 6.3: MaximumFinder.java
2 // Programmer-declared method maximum.
3 import java.util.Scanner;
45
public class MaximumFinder
6 {
7 // obtain three floating-point values and locate the maximum value
8 public void determineMaximum()
9 {
10 // create Scanner for input from command window
11 Scanner input = new Scanner( System.in );
12
13 // obtain user input
14 System.out.print(
15 "Enter three floating-point values separated by spaces: " );
16 double number1 = input.nextDouble(); // read first double
17 double number2 = input.nextDouble(); // read second double
18 double number3 = input.nextDouble(); // read third double
19
20 // determine the maximum value
21double result = maximum( number1, number2, number3 );

22
23 // display maximum value
24 System.out.println( );
25 } // end method determineMaximum
26
27 // returns the maximum of its three double parameters
28 public double maximum( double x, double y, double z )
29 {
30 double maximumValue = x; // assume x is the largest to start
31
32// determine whether y is greater than maximumValue
33if ( y > maximumValue )
34 maximumValue = y;
35
36 // determine whether z is greater than maximumValue
37 if ( z > maximumValue )
38 maximumValue = z;
39
40 return maximumValue;
41 } // end method maximum
42 } // end class MaximumFinder

Please use code tags!

And from the looks of it, the Book was introducing you to methods that can hold more than one argument.

It may have been something you didn't know (or may have) based on your reading. Here's the corrected code and I added some comments--

import java.util.Scanner;

public class MaximumFinder{

		public static void main(String[] args){

			MaximumFinder mf = new MaximumFinder();
			mf.determineMaximum();

		}

		// obtain three floating-point values and locate the maximum value
		public void determineMaximum(){

		// create Scanner for input from command window
		Scanner input = new Scanner( System.in );

		// obtain user input
		System.out.print("Enter three floating-point values separated by spaces: " );
		double number1 = input.nextDouble(); // read first double
		double number2 = input.nextDouble(); // read second double
		double number3 = input.nextDouble(); // read third double

		// determine the maximum value
		double result = maximum( number1, number2, number3 ); 
              // ^^->multiple arg method used

		// display maximum value
		System.out.println( result ); // ->book forgot to display result! lol
	} // end method determineMaximum

	// returns the maximum of its three double parameters
	public double maximum( double x, double y, double z ){ // ->the lesson is this i guess?
		double maximumValue = x; // assume x is the largest to start

		// determine whether y is greater than maximumValue
		if ( y > maximumValue )
			maximumValue = y;
		// determine whether z is greater than maximumValue
		if ( z > maximumValue )
			maximumValue = z;

		return maximumValue;
	} // end method maximum
} // end class MaximumFinder

Basically methods can have multiple arguments.... but I doubt that helps you with your assignment.

I could be way off, but that's what it really sounds like.

What exactly are you trying to accomplish?

I really need help understanding this, I have to write 200-300 words due this week though I can not seem to understand what little my book tells me about it. Sadly this is all i got from my book...Multiple parameters can be declared by specifying a comma-separated list.
Arguments passed in a method call must be consistent with the number, types and order of the parameters. Sometimes called formal parameters. I know there has to be more to it, can someone please explain in English or direct me to a site that can explain it in simple terms maybe I can grasp.

Sorry for quoting the title but I'll try to be more clear in answering your question--

Methods can be defined with multiple parameters and declared by fulfilling the requested parameters--

//defining a method--
public static void displaySomething(int x, String name, int z, char someChar){

    System.out.println("Congratulation you fulfilled the proper arguments!");
    System.out.println("Here is what you entered: ");
    System.out.print(x + " " + name + " " + z + " " + someChar);
}
//in same class as the defined method

 public static void main(String[] args){

      
//declaring the defined method, with valid arguments for the parameters of the method--
   displaySomething(3, "Hoot", 1, 'b');
}

Uhm, I'm sorry but you're getting your terms mixed up. What you refer to as declaring the method, is actually calling the method, and what you refer to as defining the method is both declaring and defining the method

public class A {
    public static void methodB (String c) { // method declaration
        System.out.println(c);  // method definition
    }

    public static void main (String[] args) {
        String c;    // reference variable declaration
        c = "Hello";    // reference variable definition
        methodB(c);  // a method call
    }

}
commented: Thanks for correcting me - I have no other excuse other than relativity over OOP +1

Uhm, I'm sorry but you're getting your terms mixed up. What you refer to as declaring the method, is actually calling the method, and what you refer to as defining the method is both declaring and defining the method

It's been a bad day.

You're right, I wasn't thinking in terms of code but on a less-technical level.

So when you define the method you are breaking down what you want such:
call getInfo (name, address, phone_number)
The call get info is the method and name, address, and phone number is the the parameter.

Correct, but you also need to declare the parameter types and what the method is to return. i.e.

public String getInfo(String name, String address, String phoneNumber) {
    return name + " " + address + " " + phoneNumber;
}

in the above public is the access modifier. public means everything can use it.

The first String means that the method will return a String. getInfo is the method name. name, address, and phoneNumber are the parameters that the calling class needs to provide, all of type String, and those are the "names" by which the method will refer to them. What they "are called" outside the method doesn't matter.

So that then it would be called as such:

String a = "George";
String b = "Jungle";  // George of the Jungle
String c = "(123) 456-7890"
String d = getInfo(a, b, c);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.