Download presentation
Presentation is loading. Please wait.
Published bySophie Hoover Modified over 8 years ago
1
1 Functions/Methods
2
2 Function / Method zDefinition Õcollection of statements that may be invoked by name (from another function or method) zFunctions/methods that we have invoked ÕInput.readInt(), setLayout(), Math.sqrt() zFunctions/methods that we have defined Õmain(), init(), action() * the term “method” will be used from now on
3
3 Defining methods that the program will invoke zWhy? Õcode inside main() (or init() or action()) may be lengthy Õsometimes, the task requires something redundant (e.g., print two blocks of asterisks, one of size 5, the other of size 10) Õwe want to define our own math function zMethods allow us to organize our code into logical units
4
4 Defining Methods zSyntax (for applications) public static void methodName() { // statements } zFor applets, omit the static keyword zMethods are defined at the level of the class (same level as main() or init())
5
5 Parameters and Return Values zIt is possible to pass data to a method Õthis allows for the possibility of a different effect based on the value of the parameter zIt is possible to return data to the caller Õlike Input.readInt() or Math.sqrt(), the method can return a value zExample that uses both features Õint factorial(int num) { … }
6
6 Invoking Methods zInvocation ÕmethodName(expression-list) zFormal versus actual parameters Õformal parameter: the variable used to represent the argument within the method Õthe expression used during invocation that is evaluated and then passed as a value to the method
7
7 Object-Oriented Programming: Classes in Java
8
8 Object Definition: a thing that has identity, state, and behavior Õidentity: a distinguished instance of a class Õstate: collection of values for its variables Õbehavior: capability to execute methods * variables and methods are defined in a class
9
9 Class Definition: a collection of data (variables) and methods that operate on that data Õdata/methods define the contents/capabilities of the instances (objects) of the class Õobject creation occurs with the statement variable = new class(parameters); Õclasses can be viewed as factories for objects
10
10 Class zA class is a template for an object zAn object is an instance of a class
11
11 Class zAll data and variables defined within a class are called instance variables Õbecause each instance of that class (each object of the class) contains a copy of that variable zMethods and variables defined within a class are called members of that class
12
12 Two Kinds of Variables in Java zVariables of a primitive type e.g., int x; char c; zVariables of a reference type (class) e.g., Button b; String s; zConventions ÕPrimitive types are reserved words in Java and are indicated in all-lower-case letters ÕClass names: first letter usually capitalized
13
13 Variables and Values zPrimitive type variables int x; … x = 5; 5 X X
14
14 Variables and References zReference type variables Button x; … x = new Button(“click”); X X “click” Button Object
15
15 The new Keyword znew Button(“click”) creates a Button object and returns a reference (an address) to that object that a Button variable could hold “click” Button Object 1023: 1023 is some address in memory 1023 X
16
16 The Dot (“.”) Operator zAllows access to variables (primitive and reference types) and methods of reference type variables. ÕEx. TextField t = new TextField(10); t.setText(“hi”); *accessing a method using the dot operator
17
17 Method Invocation zSyntax for method invocation object.methodName(arguments) zMethod may return a value or simply produce an effect on the object zTo find out what methods are available for a given class Õjavap package.name.NameOfClass Õex. Javap java.awt.Button
18
18 Strings Revisited zStrings are objects as well zString is an existing Java class Õs = “Hello” is just a shorthand for s= new String(“Hello”); zString methods Õint length() ÕString substring(int x,int y); Õno “manipulating” methods
19
19 Variables and Objects Let Circle be a class with: variable r that indicates its radius method area() that computes its area ÕDeclaration:Circle c; ÕInstantiation:c = new Circle(); ÕUsage: c.r = 5.5; System.out.println(c.area());
20
20 The complete Circle class public class Circle { public double x,y; // center coordinates public double r; // radius // the methods public double circumference() { return 2*3.14*r; } public double area() { return 3.14*r*r; } }
21
21 Using the Circle class public class TestCircle { public static void main(String args[]) { Circle c; c = new Circle(); c.x = 2.0; c.y = 2.0; c.r = 5.5; System.out.println(c.area()); }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.