Presentation is loading. Please wait.

Presentation is loading. Please wait.

* * 0 Chapter 6 Java Methods. * * 0 Method Syntax [access specifier][qualifier] return type method name(argument list) Access specifier public – method.

Similar presentations


Presentation on theme: "* * 0 Chapter 6 Java Methods. * * 0 Method Syntax [access specifier][qualifier] return type method name(argument list) Access specifier public – method."— Presentation transcript:

1 * * 0 Chapter 6 Java Methods

2 * * 0 Method Syntax [access specifier][qualifier] return type method name(argument list) Access specifier public – method is accessible from anywhere private – restrict access to the class in which the method is declared protected – used with inheritance Default – (the access specifier is left off) accessible only from classes in the same package

3 * * 0 Method Syntax – continued Qualifier Static makes the method usable by any code that has access to the method and the class regardless of whether any objects of the class have been created Arguments C++ has two ways to pass arguments – call by value and call by reference All arguments in Java are call by value. – – methods ALWAYS receive a copy of the argument

4 * * 0 Method Syntax - continued Arguments ●Objects are not passed to methods; references to objects are passed. ●If the argument is an object reference, a copy of a reference is passed to a method. Therefore, the method can manipulate the object directly.

5 * * 0 3 ways to call a method ●Refer to the method name –int x = square (y); ●Call using an object –System.out.println(“Hello”); //System.out is the object and println is the method ●Call using the class name (means the qualifier is static) –Integer.parseInt(numberString);

6 * * 0 Scope of Declarations ●Scope of a parameter declaration is the body of the method in which the declaration appears. ●Scope of local variable is the block in which the local variable is declared. ●The scope of a local-variable declaration that appears in a for initialization is the body of the for

7 * * 0 Scope Declarations - continued ●The scope of a method or field of a class is the entire body of the class. (except for static methods)

8 * * 0 Scope Example public class Scope { int x=1; public void init() ………….. public void local1() { int x=25; } public void local2() { x*=10; }

9 * * 0 Method Overloading Method overloading – concept that more than 1 method in a class has the same name but different signatures. Signature – combination of method name and number and types of arguments Example of Method Overloading public double square (double value) public int square (int value)

10 * * 0 Method Overloading Syntax Error Example public double square (int value) { ………} public int square (int x) {………} These two methods have the same signature – results in a syntax error

11 * * 0 Recursive Methods Recursive method – a method which continues to call itself directly or through another method Concept: a recursive method is capable of solving only the simplest case of a problem (base case). If the problem is complex, the method divides the problem into two smaller pieces: a base case and a smaller problem

12 * * 0 Recursion Example long y = factorial(5); Base case :1! = 1 5! 5 * 4! 4 * 3! 3 * 2! 2 * 1! 1

13 * * 0 Recursion Example public long factorial (long number) { if (number <=1) return 1; else return (number * factorial (number – 1)); }

14 * * 0 Recursion ●Avoid recursion in situations requiring performance. Recursive calls take up processor time and memory space.

15 * * 0 Static Methods ●Also called class method –Method that doesn’t depend on the contents of an object ●Java’s Math class contains a collection of static methods that enable one to perform common mathematical calculations. ●When a method is static, it only can activate static fields and static methods.

16 * * 0 Methods is Math class ●abs(x) ●max(x,y) ●min(x,y) ●pow (x,y) x y ●sqrt (x) ●ceil (x) smallest integer not less than x –ceil (9.2) = 10 –ceil (-9.8) = -9 ●floor(x) largest integer not greater than x –floor (9.2) = 9 –floor (-9.8) = -10

17 * * 0 Methods in Math class ●Math.random() –Returns a double with a value from 0.0 to 1.0 –(not including 1.0) ●How to randomly pick an integer from 1 to 6 –(int)(Math.random() * 6) + 1

18 * * 0 Random-Number Generation ●Class Random (in java.util) –Can produce random boolean, byte, float, double, int, long and Gaussian values ●Example: –Random randomNumbers = new Random(); –int randomValue = randomNumbers.nextInt(2); –returns 0 or 1

19 * * 0 Fields in Math class ●PI (public final static) ●E (public final static) ●Static fields are called class variables

20 * * 0 Calling methods in Math int z = Math.max (x,y); float d = Math.PI * y;

21 * * 0 GUI - Colors and Filled Shapes ●g.setColor(Color.BLUE); ●public void fillRect (int x, int y, int width, int height) ●public void drawOval(int x, int y, int width, int height) ●public void fillOval(int x, int y, int width, int height)

22 * * 0 How to create GUI classes ●Classified into 3 groups –Container classes –Component classes –Helper classes ●Container classes –JFrame, JPanel, JApplet ●Component classes –JButton, JRadioButton, Jmenu, JTextField, etc ●Helper classes –Graphics, Color, Font, LayoutManager

23 * * 0 How to create GUI classes ●Classified into 3 groups –Container classes –Component classes –Helper classes ●Container classes –JFrame, JPanel, JApplet ●Component classes –JButton, JRadioButton, Jmenu, JTextField, etc ●Helper classes –Graphics, Color, Font, LayoutManager

24 * * 0 End Chapter 6 - Methods Presentation


Download ppt "* * 0 Chapter 6 Java Methods. * * 0 Method Syntax [access specifier][qualifier] return type method name(argument list) Access specifier public – method."

Similar presentations


Ads by Google