Presentation is loading. Please wait.

Presentation is loading. Please wait.

Blue Box Diagram Method contains a block of instructions We pass (give) The method information (expressions) as arguments to use Processing is done in.

Similar presentations


Presentation on theme: "Blue Box Diagram Method contains a block of instructions We pass (give) The method information (expressions) as arguments to use Processing is done in."— Presentation transcript:

1 Blue Box Diagram Method contains a block of instructions We pass (give) The method information (expressions) as arguments to use Processing is done in the blue box Out can come a value that is returned Instance Variables (side effects) Definition: A method is a block of instructions in a class or object that is called by name

2 Parameters and Arguments Parameter: name a function gives an expression used to call a method Argument: expression used to call a method Why the difference? – We might want to use the method more than once. – We might want to call a function using literals 30 public int triple(int x) { return x*3; } x = triple(10); First Call 60 public int triple(int x) { return x*3; } x = triple(20); Second Call 60 public int triple(int x) { return x*3; } int z = 20; x = triple(z); Third Call 90 public int triple(int x) { return x*3; } x = triple(triple(10)); Fourth Call

3 General Class Syntax class {… Your instance variables here … ( ) { …. Instructions here …} ∙ ( ) { …. Instructions here …} } Example that we did in labs public class Grader { public final static int LABMAX = 90; public static void main(String[] args) { … instructions …} } Note: protected is something that can be inherited by a child class (This is a CS 257 topic – memorize the definition for now.

4 Methods Static method – It is using the class name – Call round() method in Math class : System.out.println(Math.round(4.5) ; Non static method – It is called using an instantiated object – String str = “abc”; – Call the length() method in str, an instantiated object of type String System.out.println(str.length(); Java has a huge class library. – We can use this library to access methods that do all sorts of useful things. – See: http://java.sun.com/j2se/1.5.0/docs/api/http://java.sun.com/j2se/1.5.0/docs/api/ Definition: Block of instructions called (executed) by name Dot notation: The syntax to access methods inside classes and objects

5 More on static and non-static public class MyClass {public static void main(String[] args) {MyClass data = new MyClass(); // Make instance of MyClass(). String s1 = data.name(); // The name method executes. String s2 = name(); // Error – name() is not static. String s3 = name2(); // OK – name2() is a static method. String s4 = MyClass.name2(); // OK – name2() is a static method. System.out.println(s1); } public String name(){ return “Dan ” + doIt(); } public String doIt() { return “Harvey”; } public static String name2() { return “Static Dan Harvey”; } } Note: when we call methods in the same class, dot notation is not needed.

6 A method returning a value public class ReturnValue {private int y = 10; public static void main(String[] args) {ReturnValue myObject = new ReturnValue(); int x = myObject.doReturn(); System.out.println(x); } private int doReturn() {int x = 33 + y; return x; } What prints? Where is x visible? Where is y visible? Is x in doReturn the same variable as in main? Which are the instance variables?

7 A method with parameters public class LotsOfParams { protected static int x = 3; public static void main(String[] args) {LotsOfParams params = new LotsOfParams(); int y = x + 4; int x = params.doSomething (1,2,x + y -3,4,params.doSomething(x, y, 3, 4, 5)); } private int doSomething(int a, int b, int c, int d, int e) {System.out.println(“” + c + b + d + a + e ); return a + b + c + d + e; } What prints? Why private? Which are the arguments? Which are the parameters (formal arguments)? Which are instance variables? What are the methods?

8 Pass by value A copy of arguments gets passed to a method, not the variable itself Example 1.Call: int x = 5; tryToChange(x); 2.Method: private void tryToChange(int x) { x = 3; } 3.Result: x in the call does not change! The x in the method only lives between the method’s braces. Question: Why doesn’t x change?

9 Method to search an array public class FindMe {String[] list = {“hello”, “this”, “is”, “a”, “good”, “day”}; public static void main(String[] args) {FindMe find = new FindMe(); System.out.println(find.isItThere(“is”)); System.out.println(find.isItThere(“cat”)); } private int isItThere(String which) {for (int i=0; i<list.length; i++) { if (list[i].equals(which)) return i; } return -1; } Question: Why do we need find in find.isItThere(“is”));

10 Method to remove and add public class RemoveMe {String[] list = {“hello”, “this”, “is”, “a”, “good”, “day”}; int howMany = list.length; public static void main(String[] args) {removeIt(3); addIt(“greetings”); } private static void removeIt(int which) {for (int i=which; i<howMany-1; i++) { list[i] = list[i+1]; } howMany--;} public static boolean addIt(String newOne) {if (howMany == list.length) return false; list[howMany++] = newOne;}

11 Review 1.What is a method? 2.What is an instance variable? 3.What is the difference between an argument an a parameter? 4.What does it mean to limit scope? 5.What is an argument? 6.What is a side effect? 7.What does private and public mean? 8.What is the definition of protected? 9.How do you return a value from a method? 10.What does pass by value mean? 11.What is the difference between an object variable and primitive variable?


Download ppt "Blue Box Diagram Method contains a block of instructions We pass (give) The method information (expressions) as arguments to use Processing is done in."

Similar presentations


Ads by Google