Lab 4: Calling User Defined Functions Introduction: In Java we can create "user defined functions" in our programs by typing them in above the main function. The syntax for these functions is shown below: static return_type function_name ( type name, type name, … ) { statements return value; } The return_type can be any Java data type (int, float, etc.) or the special type "void" which means the function does not return any data. The function_name can be any name following the rules for identifier names. Finally, function parameters are optional and can be used to specify what variables are sent into the function. Parameters are defined like variables with a data type followed by parameter name, separated by commas. In this lab, we will learn how to define and use functions in a program. Instructions: Consider the following Java program. It has two user defined functions called "GetConcert" and "GetNumTickets". If you look at the code in these functions, you will see that they print some messages, read some input from the user, and return the value of a variable. import java.util.Scanner; public class Main { static char GetConcert() { // Print menu System.out.println("The following concerts are available:"); System.out.println(" B for Beyonce"); System.out.println(" L for Lady Gaga"); System.out.println(" T for Taylor Swift"); System.out.println("Enter the concert you want:"); // Read user input Scanner scnr = new Scanner(System.in); char Concert = scnr.nextLine().charAt(0); return Concert; } static int GetNumTickets() { // Read user input System.out.println("Enter number of tickets:"); Scanner scnr = new Scanner(System.in); int NumTickets = scnr.nextInt(); // Loop until user input is valid while ((NumTickets < 0) || (NumTickets > 10)) { if (NumTickets < 0) System.out.println("Error: number must be > 0"); else if (NumTickets > 10) System.out.println("Error: number must be <= 10"); System.out.println("Enter number of tickets:"); NumTickets = scnr.nextInt(); } return NumTickets; } static float GetCost() { float Cost = 0; // Beyonce tickets are $35 each // Lady Gaga tickets are $45 each // Taylor Swift tickets are $40 each // Cost is price * NumTickets // Give 10% discount on 5 or more tickets return Cost; } public static void main(String[] args) { // Call function to find out the concert they want to attend // Call function to find out how many tickets they want // Call function to calculate total cost // Print out the information you have collected. System.out.println("The customer wants the following:"); System.out.println("Concert: " + Concert); System.out.println("Number of Tickets: " + NumTickets); System.out.println("Total Cost: " + Cost); } } Step 1: Copy this program into your Java program editor, and compile it. You should see three error messages saying "cannot find symbol". This is because we are using the variables "Concert", "NumTickets" and “Cost” inside main, but they are declared in another function. This is not allowed in Java. Variables can only be accessed in the function they are defined in. Step 2: Declare three new variables called "Concert", "NumTickets" and “Cost” in main, and recompile the program. You will see that the error messages have gone away. Java does allow us to reuse variable names in different functions, which is a good thing because we would run out of good names otherwise. If you run your program, you will see that it simply prints out your initial values for these variables. Step 3: Modify your program again to call the "GetConcert" function and save the return value in "Concert". Now when you run your program you should see a message asking the user for input, and your program should output the value they enter. Step 4: Modify your program again to call the "GetNumTickets" function and save the return value in "NumTickets". Now when you run your program you should see two messages asking the user for input, and your program should output the value they enter. Step 5: As you can see, the function "GetConcert" is supposed to return 'B', 'L', or 'T' to indicate the concert choice. Unfortunately, if the user enters 'X' the function will just return this character. Modify the GetConcert function to perform error checking to ensure that the correct values are returned. If the user enters an incorrect value, they should be prompted to try again. Step 6: The “GetCost” function is currently a “skeleton” that contains nothing but comments. To implement this function, you need to add three if-statements of the form: if (Concert == ‘B’) Cost = 35 * NumTickets; If you do this, you will get error messages saying “cannot find symbol”. This is because we have not told the function what values to use for “Concert” or “NumTickets”. To fix this, edit the first line of the “GetCost” function to add these two parameters using appropriate data types. Once you have this working, you can implement the 10% discount for purchasing 5 or more tickets. Step 7: Finally, modify your program one more time to call the "GetCost" function with two parameters “Concert” and “NumTickets” and save the return value in "Cost". Compile and run your program one last time. Test your program with a variety of input values to verify that both functions are working properly. Step 8: Once you think your program is working correctly, upload your final program and a copy of your program output into Blackboard for grading.