methods mysteries revealed
play

Methods Mysteries Revealed Wri2ng monolithic programs in - PDF document

Methods A method (a.k.a. func2on, procedure, subrou2ne) is a piece of code that performs Methods and Data a useful ac2on. (Savitch, Chapter 5) Up to this


  1. Methods ¡ • A ¡ method ¡(a.k.a. ¡func2on, ¡procedure, ¡ subrou2ne) ¡is ¡a ¡piece ¡of ¡code ¡that ¡performs ¡ Methods and Data a ¡useful ¡ac2on. ¡ (Savitch, Chapter 5) – Up ¡to ¡this ¡point ¡you ¡have ¡(for ¡the ¡most ¡part) ¡ put ¡the ¡en2re ¡Java ¡program ¡in ¡a ¡single ¡method ¡ TOPICS called ¡ main . ¡ • Invoking Methods – The ¡ main ¡method ¡is ¡special ¡only ¡in ¡that ¡it ¡is ¡the ¡ • Return Values entry ¡point ¡when ¡you ¡start ¡a ¡Java ¡program. ¡ • Local Variables – Wri2ng ¡large ¡programs ¡in ¡this ¡manner ¡is ¡ • Method Parameters imprac2cal ¡for ¡a ¡number ¡of ¡reasons. ¡ • Public versus Private CS 160, Fall Semester 2015 2 Methods ¡ Mysteries ¡Revealed ¡ • Wri2ng ¡monolithic ¡programs ¡in ¡the ¡ main ¡ public class Temperature { method ¡is ¡too ¡complex ¡and ¡hard ¡to ¡ public static void main(String[] args) { understand. ¡ // your code here } – Instead ¡programmers ¡split ¡programs ¡into ¡many ¡ } methods, ¡each ¡of ¡which ¡is ¡simple ¡to ¡understand. ¡ – A ¡common ¡guideline ¡is ¡that ¡the ¡lis2ng ¡of ¡a ¡ method ¡should ¡fit ¡(approximately) ¡on ¡one ¡page. ¡ You also define a method called In our recitations and – Each ¡method ¡has ¡its ¡own ¡set ¡of ¡ local ¡variables, ¡ main that takes an array of assignments, you define more ¡details ¡on ¡data ¡in ¡a ¡minute. ¡ Strings as its parameters classes (e.g. P1, R1). CS 160, Fall Semester 2015 3 CS 160, Fall Semester 2015 4 1

  2. Method Types Calling Methods • When you use a method you "invoke" or • Calling a method that returns an item "call" it from another method. – For void method, invoke using object name: • Two kinds of Java methods • object.method(); – When method returns a value: – Perform some action and return a single item • int value = object.method(); – Perform some and return nothing – Use the variable value anywhere any other • The method main is a void method literal or variable value can be used – Invoked by the system, not the application – Return values can be of any data type: – Does not return anything • Primitive types, objects, collections CS 160, Fall Semester 2015 5 CS 160, Fall Semester 2015 6 Defining void Methods Method Declarations • public methods can be called from • Consider method to print something, does inside or outside the class not require any data: • private methods can be called only public void printMethod() from inside the class System.out.println(“Student Name: “); • Data type specifies return type, void System.out.println(“Student EID: “); means no return value } • Heading includes parameters in ( ) • Method definitions reside in class definition • Body enclosed in braces { } – Can be called only on objects of that class CS 160, Fall Semester 2015 7 CS 160, Fall Semester 2015 8 2

  3. Return Values Local Variables • Consider method getMonthsInYear( ) • Variables declared inside a method are called local variables public int getMonthsinYear() – May be used only inside the method, i.e. they { are valid only in the scope of the method int monthsInYear = 12; – All variables declared in method main are return monthsInYear; local to main } • Heading declares type of return value • Local variables having the same name and declared in different methods are • Last statement executed is return actually different variables! • Parameter list is empty CS 160, Fall Semester 2015 9 CS 160, Fall Semester 2015 10 Local Example Blocks • Recall compound statements public double sin( double angle) { – Enclosed in braces { } double value = Math.sin(angle); • When you declare a variable within a return value; compound statement } – The compound statement is called a block – The scope of the variable is from its public double cos( double angle) { declaration to the end of the block double value = Math.cos(angle); • Variables declared outside the block are return value; usable outside and inside the block } CS 160, Fall Semester 2015 11 CS 160, Fall Semester 2015 12 3

  4. Passing Parameters Method Parameters • Method declaration must include a list of • Note the declaration parameters and their types public int computeInterest( double rate) public double sin( double angle) { – The formal parameter or argument is rate return Math.sin(angle); } • Calling the method • Empty list means no parameters double interest = obj.computeInterest(5.9); • Parameters are separated by commas – The actual parameter is 5.9 CS 160, Fall Semester 2015 13 CS 160, Fall Semester 2015 14 Primitive Parameters Method Examples • Parameter names are local to the method • public double sin( double angle) • When method invoked • public double cos( double angle) – Each parameter initialized to value in • public char charAt( int index) corresponding actual parameter • public int indexOf( char c) – Primitive actual parameter cannot be altered • public int minimum( int i, int j) by invocation of the method • public String toLower( String s) • Automatic type conversion performed • public int[] getArray() byte -> short -> int -> long -> float -> double CS 160, Fall Semester 2015 15 CS 160, Fall Semester 2015 16 4

  5. Pre-condition Comment public and private • public • Precondition comment – Can access the class, method, or data by – States conditions that must be true before method is invoked name outside defining class • Example • private double squareRoot( double value) { – Can access the class, method, or data by // Parameter value must be > 0 name only inside defining class assert (value >0); • Classes generally specified as public … • Instance variables usually are private } • Methods can be public or private CS 160, Fall Semester 2015 17 CS 160, Fall Semester 2015 18 Objects and classes Post-condition Comment • A class is a blueprint for objects: • Postcondition comment – String class provides an abstraction – Tells what will be true after method executed • Example – Objects of this class can be instantiated double squareRoot( double value) { • e.g. String firstName = new String(); – firstName is an object (instance) of class String root = … – firstName is used to invoke methods in class // Return value is the positive • You can define your own classes with // square root of the value assert(abs(root*root-value)<EPSILON); – Data (e.g., internal private data) } – Methods CS 160, Fall Semester 2015 19 CS 160, Fall Semester 2015 20 5

  6. Accessors and Mutators Methods Calling Methods • When instance variables are private, class • A method body may call any other method must provide methods to access them: • If the method is within the same class – Typically named get SomeValue – Need not use the class prefix of object that is – Referred to as an accessor (getter) method being called • Must also provide methods to change the – this keyword is assumed, which is values of the private instance variable shorthand for the class we’re currently in – Typically named set SomeValue – Referred to as a mutator (setter) method CS 160, Fall Semester 2015 21 CS 160, Fall Semester 2015 22 Instance ¡Data ¡ Instance ¡Data: ¡Example ¡ • Local ¡data ¡resides ¡in ¡a ¡method, ¡class ¡data ¡ public class P1 { resides ¡in ¡the ¡class ¡ public int int0 ; • Data ¡defined ¡in ¡the ¡class ¡can ¡be ¡of ¡two ¡types: ¡ ¡ private double real0; – Data ¡may ¡belong ¡to ¡the ¡class ¡(and ¡will ¡take ¡the ¡ } same ¡value ¡for ¡all ¡the ¡objects) ¡ – Data ¡may ¡belong ¡to ¡the ¡object ¡(and ¡can ¡take ¡ • int0 visible outside of class, different ¡values ¡for ¡each ¡instance) ¡ • real0 is not, it is private to to the class and can only be accessed from within the class CS 160, Fall Semester 2015 23 CS 160, Fall Semester 2015 24 6

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend