settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: What are methods in Java

+11 votes

How to declare methods in Java and how to accept values as arguments (+ execute the code based on those arguments)?

Thanks

asked in Java category by user hues
edited by user golearnweb

2 Answers

+3 votes

Here's an example of Java method's usage:

  • The method here is called loopIt and is from row 11 to row 22.
  • There is also a String argument called label which can be changed as a string.
  • On rows 6,7 and 8 the method is called BUT with different label arguments printed each time.
public class MethodsExample {

    static String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

    public static void main(String[] args) {
        loopIt("Months of the year: ");
        loopIt("Second Run: ");
        loopIt("Third Run: ");
    }

    static void loopIt(String label) {//IN METHODS (IN THIS CASE THE METHOD IS: loopIt) YOU CAN PUT ARGUMENTS BETWEEN THE BRACKETS

        System.out.println(label);
        for (int i = 0; i < label.length(); i++) {
            System.out.print("*");
        }
        System.out.println();

        for (int i = 0; i < months.length; i++) {
            System.out.println(months[i]);
        }
    }
}

 

answered by user richard8502
edited by user golearnweb
+2 votes

Here's my code with 2 methods (besides the main one), BUT with different arguments inside the methods. Also it is an example of overloading methods with different signatures. Read the comments to clear the things out;

import java.util.Scanner;

public class UC_Methods {
    public static void main(String[] args) {//THE MAIN METHOD CALLS THE OTHER 2 METHODS: getInput AND addValues
        String s1 = getInput("Enter value 1: ");
        String s2 = getInput("Enter value 2: ");
        String s3 = getInput("Enter value 3: ");

        double result = addValues(s1, s2);
        System.out.println("The answer is: " + result);

        double result2 = addValues(s1, s2, s3);
        System.out.println("The answer for 3 values is: " + result2);

        double result3 = addValues(s1, s2, s3, s1, s2, s3, s1, s2, s3);
        System.out.println("The answer for ANY values is: " + result3);
    }

    static String getInput(String prompt) {//METHOD 1 FOR GETTING USER'S INPUT: getInput WITH STRING ARGUMENT CALLED prompt
        System.out.print(prompt);
        Scanner sc = new Scanner(System.in);
        return sc.nextLine();
    }

    static double addValues(String s1, String s2) {//METHOD 2 FOR GETTING USER'S 2 VALUES + RESULT: addValues
        double d1 = Double.parseDouble(s1);
        double d2 = Double.parseDouble(s2);
        double result = d1 + d2;
        return result;
    }

    static double addValues(String s1, String s2, String s3) {//METHOD 2 FOR GETTING USER'S 3 VALUES + RESULT: addValues (WITH DIFFERENT ARGUMENTS)
        double d1 = Double.parseDouble(s1);
        double d2 = Double.parseDouble(s2);
        double d3 = Double.parseDouble(s3);
        double result = d1 + d2 + d3;
        return result;
    }

    static double addValues(String... values) {//METHOD 2 FOR GETTING USER'S ANY AMOUNT OF VALUES + RESULT: addValues (WITH DIFFERENT ARGUMENTS)
        double result = 0;
        for (String value : values) {
            double d = Double.parseDouble(value);
            result += d;
        }
        return result;
    }
}

and you should get this when you use 2,3 and 4:

Enter value 1: 2
Enter value 2: 3
Enter value 3: 4

The answer is: 5.0
The answer for 3 values is: 9.0
The answer for ANY values is: 27.0

answered by user sam
edited by user golearnweb
...