Methods In this section we will learn how about methods in Java: How to Draw a Triangle Methods A Triangle Method Declaring a Method Calling a Method Passing Parameters Local Variables Results from a method PHY281 Scientific Java Programming Methods Slide 1 Drawing a Triangle First choose how we wish to describe a triangle e.g. the coordinates of the 3 corners. If we assume a flat based isosceles we can choose: • The coordinates of the bottom left hand corner. • The length of the base. • The vertical height. height (bottomX, bottomY) base PHY281 Scientific Java Programming Methods Slide 2 Drawing a Triangle For example if: • The coordinates of the bottom left hand corner • The length of the base • The vertical height g.drawLine(80, 200, 80+100/2, 200-110); = (80, 200) = 100 = 110 g.drawLine(80+100, 200, 80+100/2, 200-110); (80+100/2, 200-110) minus sign because y increases down the screen 110 (80, 200) 100 (80+100, 200) g.drawLine(80, 200, 80+100, 200); PHY281 Scientific Java Programming Methods Slide 3 Code to Draw a Triangle Use variables to make it more general import java.awt.*; import java.applet.Applet; public class TriangleTry extends Applet { public void paint(Graphics g) { int bottomX = 80; int bottomY = 200; int base = 100; int height = 110; g.drawLine(bottomX, bottomY, bottomX+base, bottomY); g.drawLine(bottomX+base, bottomY, bottomX+base/2, bottomY-height); g.drawLine(bottomX+base/2, bottomY-height, bottomX, bottomY); } } These statements continue on the next line (they don't have to). PHY281 Scientific Java Programming Methods Slide 4 Methods If we want to draw two or more triangles we would have to duplicate all this code as many times as we wanted triangles. The program would be very tedious to write and impossible to maintain (if you wanted to change anything you would have to remember to change all the copies). Remember that our program is an Object and Objects have : • Things that describe the object called attributes - colour, size etc • Things the object can do to its data or to something else called methods. Methods are known as subroutines or functions in other computer languages. In order to make our program do something - draw triangles we add a drawTriangle method to our class. PHY281 Scientific Java Programming Methods Slide 5 A Triangle Method import java.awt.*; import java.applet.Applet; public class TriangleMethod extends Applet { public void paint(Graphics g) { drawTriangle(g, 80, 200, 100, 110); drawTriangle(g, 125, 220, 60, 70); } private void drawTriangle(Graphics g, int bottomX, int bottomY, int base, int height) { g.drawLine(bottomX, bottomY, bottomX+base, bottomY); g.drawLine(bottomX+base, bottomY, bottomX+base/2, bottomY-height); g.drawLine(bottomX+base/2, bottomY-height, bottomX, bottomY); } } PHY281 Scientific Java Programming Methods Slide 6 Declaring a Method The following method header declares (introduces) the method. The parameters of the method are enclosed in ( ) and the body of the method is enclosed in { }. Name of method Parameters private void drawTriangle(Graphics g, int bottomX, int bottomY, int base, int height) { ... } Body Access control modifier Return type The private is an access control modifier that says that the method can only be used within the class where it is declared. (paint is public because it is called from outside the class by the browser or appletviewer). The void is the return type that says what type of variable or object the method produces that can be used by the program that calls it. In this case nothing is produced so the return type is void. (It could have calculated the area and returned it as an int for instance). PHY281 Scientific Java Programming Methods Slide 7 Calling a Method You call or invoke a method by stating its name together with a list of arguments in ( ) that match the parameters in the declaration in both order and variable type. Name of method Arguments drawTriangle(g, 80, 200, 100, 110); Method Calling program 1 private void drawTriangle(...) { ... 3 7 ... drawTriangle(g,80,200,100,110); 6 5 4 ... } drawTriangle(g,125,220,60,70); 2 8 9 The program executes in the calling program (1) till it gets to the call to the method when it skips to the method (2). It then executes the statements in the method (3) and returns to the calling program (4). It then executes any more statements there (5) till it reaches the next call to the method and repeats the process (6, 7, 8) before finally continuing (9). PHY281 Scientific Java Programming Methods Slide 8 Passing Parameters The values you pass as arguments must match the type of the parameters declared in the definition of the method. A copy of the value of the variable is used in the method itself (this is known as pass-by-value) rather than the variable itself. b = 100; drawTriangle(g, 80, 200, b, 110); Arguments The values (100 and 110) are used for base and height private void drawTriangle (Graphics g, int bottomX, int bottomY, int base, int height) { ... } If you change base in the method it doesn't affect b above (because its value was copied so only the copy changes). Parameters PHY281 Scientific Java Programming Slide 9 Methods Local Variables The calculations can be simplified by introducing local variables into the method: private void drawTriangle(Graphics g, int bottomX, int bottomY, int base, int height) { int rightX = bottomX + base; int topX = bottomX + base/2; int topY = bottomY - height; g.drawLine(bottomX, bottomY, rightX, bottomY); g.drawLine(rightX, bottomY, topX, topY); g.drawLine(topX, topY, bottomX, bottomY); } Local variables These variables are temporary - they are created each time the method is called and destroyed when it ends. They are not related to any variables with the same names elsewhere in the program. Technically this means the variables have limited scope. When you define local variables you don't have to worry if some other method is using the same variable names (probably it is). PHY281 Scientific Java Programming Methods Slide 10 Results from a Method Add a new method areaTriangle that calculates the area of the triangle: private void drawTriangle(Graphics g, int bottomX, int bottomY, int base, int height) { int rightX = bottomX + base; int topX = bottomX + base/2; int topY = bottomY - height; g.drawLine(bottomX, bottomY, rightX, bottomY); g.drawLine(rightX, bottomY, topX, topY); g.drawLine(topX, topY, bottomX, bottomY); int answer = areaTriangle(base, height); g.drawString("Area = " + answer, topX+10, topY); } private int areaTriangle(int base, int height) { int area = height*base/2; return area; } } PHY281 Scientific Java Programming Methods Slide 11 Results from a Method int answer = areaTriangle(base, height); g.drawString("Area = " + answer, topX+10, topY); private int areaTriangle(int base, int height) { int area = height*base/2; return area; } •We call the method areaTriangle with the base and height •areaTriangle calculates the area •The value of the area is returned to the calling program •The variable answer is set equal to the returned value. PHY281 Scientific Java Programming Methods Slide 12