/* 10/7/2019 * Parameters are used to pass information from a method call * to the actual method. * In the method header, you declare the parameter with * a datatype and identifier, similar to a variable declaration. * You can have a whole list (comma separated list) of parameters * but each parameter must have a datatype. * * THE IDENTIFIERS OF PARAMETERS AND THEIR ARGUMENTS DO NOT * HAVE TO MATCH * The datatypes of the arguments must match those of the * declared parameters in the method. If they don't match * up, a compiler error results. * * local variable -- variable declared in a method * * pass-by-value * when a parameter is passed into a method, a copy is made * of the variable's value. There is no connection between * the variables other than the initialization of the parameter * to the value of the argument. * Any modifications to values of parameters will NOT modify * the variables in the calling method. * * next time: composition (and value methods) * * Advantages of using methods: * Modular programming -- makes coding much easier, because * we deal with small manageable problems rather that 1 large * problem. * * fewer bugs * easier to maintain * reusability * more readable */ import java.util.Scanner; public class MethodParams { /* this version is modified * to read in name and print hello name */ public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("What is your name? "); String name = sc.next(); printHello(name); // call sum2nums passing 2 integers sum2nums(34, 987); // literals int x=43, y=765; // double y=89; // this would cause a compiler error sum2nums(x, y); // variables System.out.println("back in main, x="+x+" y="+y); System.out.println("Enter 2 integers: "); x = sc.nextInt(); y = sc.nextInt(); sum2nums(x, y); // variables that were read in sum2nums(x+y, y-x); // expresssion is evaluated and the answer is passed in sum2nums(3.5, 4.5); System.out.println("main is returning"); } /** method printHello displays Hello World! to the screen * it has no parameters and returns void */ public static void printHello(String nm) { System.out.println("Hello " + nm); return; } /** sum2nums adds 2 integers and prints the sum * @param num1 first number passed in * @param num2 second number passed in */ public static void sum2nums(int num1, int num2) { int sum = num1+num2; System.out.println(num1+"+"+num2+"="+sum); num1++; num2++; System.out.println("in method, after incrementing, num1="+num1+" num2="+num2); } /** method sum2nums is OVERLOADED to accept 2 doubles */ // note that there must be a difference in the parameter // list when methods are overloaded. // names of these parameters are not related to names // of parameters in other methods public static void sum2nums(double num1, double num2) { double sum = num1+num2; System.out.println(num1+"+"+num2+"="+sum); } } /* Lab 10: Void Methods Write a program with 2 methods: main, average Method average will accept an integer parameter numElts, which represents the number of data elements to be read in. The method should read in (from the keyboard) numElts numbers, and print their average. */ if (option==1) billOption1(); else if (option==2) billOption2();