1 / 11

Blue Box Diagram

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

Download Presentation

Blue Box Diagram

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Parameters and Arguments First Call x = triple(10); public int triple(int x) { return x*3; } 30 • 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 themethod more than once. • We might want to call a function using literals Second Call public int triple(int x) { return x*3; } x = triple(20); 60 Third Call int z = 20; x = triple(z); 60 public int triple(int x) { return x*3; } Fourth Call x = triple(triple(10)); public int triple(int x) { return x*3; } 90

  3. General Class Syntax <public, private, protected> class <className> { … Your instance variables here … <public, private, protected> <mods><type><methodName>(<parameters>) { …. Instructions here … } ∙ ∙ ∙ <public, private, protected> <mods><type><methodName>(<parameters>) { …. 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 Definition: Block of instructions called (executed) by name • 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 StringSystem.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/ 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 • Call: int x = 5; tryToChange(x); • Method: private void tryToChange(int x){ x = 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 • What is a method? • What is an instance variable? • What is the difference between an argument an a parameter? • What does it mean to limit scope? • What is an argument? • What is a side effect? • What does private and public mean? • What is the definition of protected? • How do you return a value from a method? • What does pass by value mean? • What is the difference between an object variable and primitive variable?

More Related