/* This program demonstrates the use of methods in Java. * Methods are like mini programs that can be "called" by another program, or another part of the same program * Methods are built for specific purposes. */ import java.util.Scanner; class Methods { /* This method is called assThreeNumbers. It accepts 3 integer paramters and returns and integer as well. * We have declared this method static. That means we can have only one instance of this method per class. * For now, all the methods we write will be static. * This method adds 3 integers and returns their sum. */ static int addThreeNumbers (int num1, int num2, int num3) { //The parameters num1, num2, and num3 are local variables. They can be used as regular variables. // And they are destroyed when control leaves the method. int sum; sum = num1 + num2 + num3; return sum; // The return statement returns the value to the place of call, where it can be received in a // variable, printed, used as a parameter to another method, etc. } /* This method is called starbucks. It takes a String parameter and returns a String value. * This method returns a catchphrase associated with a certain name. * If there is no catchphrase for the name, it returns "No Cathcphrase". */ static String starbucks ( String name) { String catchphrase = "No Catchphrase";//default value if (name.equalsIgnoreCase("John Cena")) catchphrase = "Ba Da Da Daaa"; // or is is "You can't see me"? else if (name.equalsIgnoreCase("Donald Trump")) catchphrase = "China"; else if (name.equalsIgnoreCase("Tommy Wiseau")) catchphrase = "Oh hi Mark!"; return catchphrase; } public static void main(String [] args) { Scanner input = new Scanner (System.in); int a,b,c; System.out.println("Enter 3 mumbers:"); a=input.nextInt(); b=input.nextInt(); c=input.nextInt(); //Over here, the 3 numbers we just read in are passed to the addThreeNumbers method. // The returned result is stored in the variable called result. int result = addThreeNumbers(a,b,c); System.out.println("The sum is : "+result); //We can pass literals as parameters. We can also directly print the returned value. System.out.println("The sum of 2345, -974 and 1048 is "+ addThreeNumbers(2345,-974,1048)); String answer; System.out.println("Enter your name"); String junk = input.nextLine(); // to remove the preceding newline answer = input.nextLine(); //This method is used in a similar vein to the previous method System.out.println(starbucks(answer)); System.out.println("Tommy Wiseau's catchphrase is "+starbucks("Tommy Wiseau")); } }