/* Methods that Return Values You should recall the difference between the following lines: JOptionPane.showMessageDialog(null,"hello"); s = JOptionPane.showInputDialog(null,"prompt: "); // s a String variable The first line invokes the "showMessageDialog" method of a server called JOptionPane. null and "hello" are "parameters" which I'll explain later. The difference to note here is that showMessageDialog is method that can stand alone as a statement, whereas showInputDialog can not. showMessageDialog is a function that returns no value (like the functions of our previous example). showInputDialog returns a value - a String value. And thus calls to showInputDialog are expressions, not statements (until you do something else with it, like assigning the String returned to a variable). We can write functions that return values by replacing "void" with the type of value it will return. To return a value from inside a function, we introduce the special statement return x; Which will cause the function executing this code to return the value x, whatever it might be. For example, we can define a method (inside some class) with public int f() { return 3; } or pubic String g() { return "hello"; } Now when we call these functions, the functions calls must be treated as expressions as opposed to stand-alone values! That is, you can nolonger just say "myobject.f();" You must do something with the integer value returned by f, for example: int x; x = myobject.f(); IMPORTANT ENOUGH TO EMPHASIZE: If a function is declared void, that means it can't return a value so don't use "return" anywhere. If a function is declared to return a value, such as of type int, then you must execute a return statement before exiting a function. For example, if you have an if-else statement as the last statement of your function, then both the if and else components must end by a return statement. Don't get fixated with the syntax - THINK! That is, think about whether you really want your function to return something or not before puting return statements all over the place. Most mistakes made by beginners are caused by them not thinking clearly first before they start to write code. In our next major example, we rewrite the waiter class to illustrate functions that return values. In addition to a monolithic menu() function, I (the client) may want to know the price of individual items. Furthermore, I don't necessarily just want these values printed. I may decide to do some calculation with it, or I may want to display it on some fancy graphical interface. It's a general principle of computer science to only do I/O when you have to. */ /* Class with functions that take no parameters but return values*/ class waiter { // services are defined in the form of methods (functions): public void water() { System.out.println("We don't have water!"); } public void check() { System.out.println("Here's your check. Pay up!"); } // ... other methods such as menu() omitted for brevity public double priceofchicken() { return 5.95; } public double priceofburger() { return 4.95; } public double priceoffries() { return 1.95; } } // end of class waiter public class objects1 { public static void main(String[] args) { waiter ourwaiter; // declares a waiter variable ourwaiter = new waiter(); // assigns variable a waiter object double x; // variable to hold price of burger double y; // variable to hold price of chicken x = ourwaiter.priceofburger(); // assign x and y to values returned y = ourwaiter.priceofchicken(); // by function calls. if (x