Presentation is loading. Please wait.

Presentation is loading. Please wait.

Methods CSC 171 FALL 2004 LECTURE 3. Methods Variables describe static aspects – “nouns” Methods describe dynamic aspects – “verbs”, “behaviors” – Methods.

Similar presentations


Presentation on theme: "Methods CSC 171 FALL 2004 LECTURE 3. Methods Variables describe static aspects – “nouns” Methods describe dynamic aspects – “verbs”, “behaviors” – Methods."— Presentation transcript:

1 Methods CSC 171 FALL 2004 LECTURE 3

2 Methods Variables describe static aspects – “nouns” Methods describe dynamic aspects – “verbs”, “behaviors” – Methods “do things”

3 Methods We have occasion to use methods that do computation “elsewhere” – double d1 = Math.sqrt(50.9);

4 Methods Think of a method as a “black box” – We put something in – We get something out Putting something in – parameters Getting something out – Return values

5 Black Box Math sqrt() 3.0 9.0 double x = Math.sqrt(9.0); sqrt()

6 A Computer Program public class myFirstProg { public static void main(String args[]){ System.out.println(“Hello, CSC171”); }

7 Black Box MyFirstProg main() void String args[] “side effects”

8 A Computer Program public class mySecondProg { public static void main(String args[]){ int x = 5, y = 8; int product = mymult(x,y); System.out.println( x + “ * “ + y + “ is “ + product); } public static int mymult(int n1, int n2){ return n1 * n2; }

9 Black Box MyFirstProg main() void String args[] mymult()

10 Sequence of events main() mymult() System.out.println() 5,8 40 “5 * 8 is 40” void

11 Proceedural Abstraction We define the input and output behavior – Contract We embed some complex behavior in a method. We refer to the method by a name We can write & debug the method once & then use it whenever we want.

12 public class myThirdProg { public static void main(String args[]){ int x = 5, y = 8; int product = mymult(x,y); System.out.println( x + “^3 * “ + y + “ is “ + product); } public static int mymult(int n1, int n2){ return mycube(n1) * n2; } public static int mycube(int x){ return x * x * x; } }

13 Sequence of events main() mymult() System.out.println() 5,8 1000 “5^3 * 8 is 1000” void mycube() 5 125

14 Group Exercise Write a method – Called “volume of a box” – Takes width, height, depth as parameters – Returns the volume, the product

15 Solution public static int volume (int width, int height, int depth){ return width * height * depth; }

16 CONSTRUCTORS Constructors are special methods - used to build objects - you need constructors to build objects out of class definitions - however, if you don’t specify a constructor, you get one by default

17 Example class Instructor { String name; int age; }

18 Constructor for Initializations class Instructor { String name; int age; public Instructor (String instructorName) { name = instructorName; }// but you give up your default constructor; }

19 Accessor/Mutators Accessor methods “gets” and Mutator methods “sets” are preferable methodologies rather than direct modification

20 WHY GET/SET?

21 So Good: Instructor i1 = new Instructor(); i1.setName(“Ted”); Bad: Instructor i1 = new Instructor(); i1.name = “Ted”;

22 Get/Set In general: “gets” take no parameters and return something “sets” take parameters and return nothing


Download ppt "Methods CSC 171 FALL 2004 LECTURE 3. Methods Variables describe static aspects – “nouns” Methods describe dynamic aspects – “verbs”, “behaviors” – Methods."

Similar presentations


Ads by Google