DOC PREVIEW
USF CS 112 - Intro to Programming II Scope and Parameters

This preview shows page 1 out of 3 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

{small lecturenumber - hepage :} Scope{small lecturenumber - hepage :} Local scope{small lecturenumber - hepage :} Object scope{small lecturenumber - hepage :} Class scope{small lecturenumber - hepage :} Class scope{small lecturenumber - hepage :} Scope{small lecturenumber - hepage :} Parameters{small lecturenumber - hepage :} Example{small lecturenumber - hepage :} Method signature{small lecturenumber - hepage :} Example{small lecturenumber - hepage :} Questions{small lecturenumber - hepage :} Questions{small lecturenumber - hepage :} Questions{small lecturenumber - hepage :} Specifying scopeIntro to Programming IIScope and ParametersChris BrooksDepartment of Computer ScienceUniversity of San FranciscoDepartment of Computer Science — University of San Francisco – p. 1/??3-2: Scope•Scope refers to the area of a program where a variable can beaccessed.•Java has three types of scope:◦Local scope - the variable exists only within a method◦Object scope - the variable can be accessed from anymethod belonging to an object.◦Class scope - the variable can be accessed by all instancesof a class.Department of Computer Science — University of San Francisco – p. 2/??3-3: Local scope•Exists only when a method is executing•The garbage collector reclaims a local variable when themethod ends.•Local variables are useful for temporary variables and counters./*raise x to the yth power*/public int exponentiate(int x, int y) {int total;int i;for (i = 0; i < y; i++) {total = total*x;}return total;}Department of Computer Science — University of San Francisco3-4: Object scope•Variables are available anywhere within an object.•This is useful for data associated with an object that will beused by multiple methods.•This is also called instance data.public class circle {public int radius;public static final double pi = 3.14;public double getArea() {return pi*radius*radius;}}Department of Computer Science — University of San Francisco – p. 4/??3-5: Class scope•Class variables are available to all members of a class.•These are declared as static•This means that one copy of the variable is shared by allobjects.•Useful for defining constants.Department of Computer Science — University of San Francisco – p. 5/??3-6: Class scopepublic class circle {public static final double pi = 3.14;public int radius;public double getArea() {return pi*radius*radius;}public static void main(String args[]) {circle c1 = new circle();circle c2 = new circle();c1.radius = 5;c2.radius = 6;System.out.println("c1’s area is: " + c1.getArea());System.out.println("c2’s area is: " + c2.getArea());}}Department of Computer Science — University of San Francisco3-7: Scope•In the previous example, each circle had it’s own copy of radius.•They all shared a copy of pi.•radius has object scope, whereas pi has class scope.◦Identify variables in Bank account program.Department of Computer Science — University of San Francisco – p. 7/??3-8: Parameters•Parameters are the variables passed into a method.•We can talk about:◦Formal parameters - these are the variables named in themethod definition.◦Actual parameters - these are the variables in the methodinvocation.Department of Computer Science — University of San Francisco – p. 8/??3-9: Example/**This is a method definition*/public double depositFunds(double amt) {balance = balance + amt;return balance;}...bankacct b = new bankacct();paycheck = 100.0/*this is a method invocation*/b.depositFunds(paycheck);Department of Computer Science — University of San Francisco3-10: Method signature•Specification of all data types coming in and out of a method.◦Type and order of input parameters◦Type of return variable•A method signature allows the compiler to uniquely identify amethod.Department of Computer Science — University of San Francisco – p. 10/??3-11: Example•Consider the following method declaration:•double calculate(double a1, double a2, double a3);•Which of the following are valid calls to this method?◦calculate(3, 52.0, -5.1);◦double y = calculate(0, 1.1, 2.2);◦calculate(1.1, 2.3);◦calculate(“Hello”, 4.4, 2);◦calculate();◦calculate(3.3);Department of Computer Science — University of San Francisco – p. 11/??3-12: Questions•What happens in this case?public class circle {public int radius = 5;public void printArea() {int radius = 4;System.out.println(‘‘Area is ’’ + (radius*radius*3.14));}}Department of Computer Science — University of San Francisco3-13: Questions•What happens in this case?public class bankacct {public double balance;public void updateBalance(double newAmount) {double newAmount = 12.0;balance += newAmount;}}Department of Computer Science — University of San Francisco – p. 13/??3-14: Questions•What happens in this case?public class circle {public int radius = 5;public void printArea(int radius) {System.out.println(‘‘Area is ’’ + (radius*radius*3.14));}...circle c = new circle()c.getArea(3);}Department of Computer Science — University of San Francisco – p. 14/??3-15: Specifying scope•In general, it’s a bad idea to give local variables the same nameas an instance or class variable.◦Confusing, leads to bugs.•If it can’t be avoided, you can use “this” to indicate the instancevariable should be used rather than the local varaible.•You can use the class name (e.g. circle.pi) to indicate that aclass variable should be used.Department of Computer Science — University of San


View Full Document

USF CS 112 - Intro to Programming II Scope and Parameters

Documents in this Course
Structs

Structs

4 pages

Trees

Trees

25 pages

Strings

Strings

27 pages

Queues

Queues

3 pages

Trees

Trees

24 pages

Arrays

Arrays

5 pages

ArrayList

ArrayList

24 pages

Stacks

Stacks

2 pages

Stacks

Stacks

8 pages

Trees

Trees

24 pages

Stacks

Stacks

8 pages

Queues

Queues

16 pages

Queues

Queues

17 pages

Queues

Queues

17 pages

Load more
Download Intro to Programming II Scope and Parameters
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Intro to Programming II Scope and Parameters and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Intro to Programming II Scope and Parameters 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?