Presentation is loading. Please wait.

Presentation is loading. Please wait.

Methods. Methods are groups of statements placed together under a single name. All Java applications have a class which includes a main method class MyClass.

Similar presentations


Presentation on theme: "Methods. Methods are groups of statements placed together under a single name. All Java applications have a class which includes a main method class MyClass."— Presentation transcript:

1 Methods

2 Methods are groups of statements placed together under a single name. All Java applications have a class which includes a main method class MyClass { public static void main(String[] args) {... statements }

3 Methods All Java applications have a main method. Many of the statements in our main methods have invoked, or called, other library methods such as println, parseInt and showInputDialog. It is often convenient to write and call our own methods, to simplify the code in the main method (and elsewhere).

4 Car repair bill with methods 01 import javax.swing.*; 02 class CarRepair 03 { 04 public static void main(String[] args) 05 { 06 String partsStr = read("What is the parts cost"); 07 String hoursStr = read("How many hours"); 08 double parts = Double.parseDouble(partsStr); 09 double hours = Double.parseDouble(hoursStr); 10 // calculate bill before VAT 11 double bill = parts + hours * 20; 12 // add VAT 13 bill *= 1.175; // same as bill = bill * 1.175; 14 display("Your bill is £" + bill); 15 }

5 Car repair bill with methods 17 private static void display(String s) 18 { 19 JOptionPane.showMessageDialog(null, s); 20 } 21 22 private static String read(String prompt) 23 { 24 return JOptionPane.showInputDialog(prompt); 25 } 26 27 }

6 Car repair bill with methods In this version we have defined two private methods read and display. These make use of JOptionPane methods. The main method calls the read method at lines 06 and 07, and the display method at line 14. The method definitions at lines 17 – 20 and 22 – 25 can be thought of as recipes or methods for displaying an output string and reading an input string with a given prompt. When we write the main method we can forget about the fiddly JOptionPane methods and use the simpler display and read methods instead.

7 Parameters and arguments When we write methods we may specify one or more parameters in the method header between ( and ). The display method has a parameter String s and the read method has a parameter String prompt When we use a method we pass arguments across to these parameters. For instance in the main program we pass the argument "How many hours" for the call of read at line 07 across to the parameter String s at line 17.

8 Car repair bill with methods – design diagram The third box documents the methods used. – means private

9 Subroutines and functions Subroutine methods do something useful without returning a value. Examples of these are the main method of an application and the library methods println and showMessageDialog. These use the keyword void in their header as in private static void display(String s)... Function methods return a value. Examples of these are the library methods showInputDialog (which returns a String value), parseInt (which returns an int value) and parseDouble (which returns a double value). These use a type (primitive or class) in their header as in private static String read(String prompt)...

10 Subroutines and functions Function methods must include at least one return statement to return the required value as in return JOptionPane.showInputDialog(prompt); and the value returned must match the type specified in their header (here a String is returned). Subroutine methods don’t need return statements but may include statements of the form return; in which case the rest of the method is ignored and control passed back to the calling method.

11 Modifiers The keywords public, private and static are method modifiers. public methods may be called from outside the classes in which they are defined. private methods may only be called from other methods defined in the same class. Their job is just to simplify the code in other methods and make the code easier to understand. static methods belong to the class itself, and not to the objects created by the class. static methods may be public or private. Non- static (or dynamic) methods have access to the inner state of class objects. (This will become clearer in the next section).

12 Library methods The Java class libraries contain thousands of methods, both static and dynamic. static methods include –JOptionPane.showInputDialog(...) –JOptionPane.showMessageDialog(...) –Integer.parseInt(...) –Double.parseDouble(...)

13 Library methods The Java class libraries contain thousands of methods, both static and dynamic. Dynamic methods include –System.out.println(...); –Date now = new Date(); int thisYear = now.getYear() + 1900;

14 Library methods When calling a static method we always use ClassName.methodName(arguments) unless the calling and called methods are in the same class.

15 Library methods When calling a dynamic method we always use object.methodName(arguments) unless the calling and called methods are in the same class.

16

17 Car repair revisited We could add an extra private method calculateBill(...) to carry out the actual calculation

18 Car repair revisited 01 import javax.swing.*; 02 class CarRepair2 03 { 04 public static void main(String[] args) 05 { 06 String partsStr = read("What is the parts cost"); 07 String hoursStr = read("How many hours"); 08 double parts = Double.parseDouble(partsStr); 09 double hours = Double.parseDouble(hoursStr); 10 double bill = calculateBill(parts, hours); 11 display("Your bill is £" + bill); 12 }

19 Car repair revisited 14 private static void display(String s) 15 { 16 JOptionPane.showMessageDialog(null, s); 17 } 18 19 private static String read(String prompt) 20 { 21 return JOptionPane.showInputDialog(prompt); 22 } 23 24 private static double calculateBill(double p, double h) 25 { 26 double b = p + h * 20; 27 return b * 1.175; 28 } 29 30 }

20 Scope of variables So far we have used local variables. The scope of a local variable is the code where it may be referred to, and is simply the method in which it is defined. (This applies to the method’s parameters too). For instance the scope of the variable parts declared at line 08 in CarRepair2 is the main method lines 04 – 12. Any reference to parts outside this scope would be an error in this program (but see below).

21 Scope of variables Within a particular method we have complete freedom in naming the variables and parameters. calculateBill could have been defined like this: 24private static double calculateBill(double parts, double hours) 25 { 26 double bill = parts + hours * 20; 27 return bill * 1.175; 28 } with no difference to the compiled program. There are now two variables named parts - one in the main program declared at line 08 and one which is a parameter of the calculateBill method at line 24. The Java compiler decides which of these two is intended; the references at lines 8 and 10 are to the variable in the main program, the reference at line 26 is to the parameter of calculateBill.

22


Download ppt "Methods. Methods are groups of statements placed together under a single name. All Java applications have a class which includes a main method class MyClass."

Similar presentations


Ads by Google