DOC PREVIEW
UNC-Chapel Hill COMP 14 - Lecture Notes

This preview shows page 1-2-17-18-19-35-36 out of 36 pages.

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

Unformatted text preview:

COMP 14 Introduction to Programming Miguel A Otaduy May 25 2004 Anatomy of a Class A class contains data declarations and method declarations int width int length Data declarations Method declarations operations Writing a class public class data private private Rectangle members int width int length methods public double computeArea public static void main Why Use Methods To divide complex programs into manageable pieces Abstraction provide an operation to be performed on an object ex computeArea Code Re use write a small piece of code that can be used called multiple times saves typing Methods Pre defined methods provided by Java standard library we ve used these before Math class Math pow Math sqrt Integer class Integer parseInt User defined methods you write these Method as a Black Box Input parameters METHOD Return value Internal data members of the class A method can use input parameters and internal data members of the class It may modify the value of internal data members It may also return a value Control Flow Program control flow execution always begins with the first statement in the method main other methods execute only when called Method control flow when a method is invoked the flow of control jumps to the method and the computer executes its code when complete the flow of control returns to the place where the method was called and the computer continues executing code Test this with the debugger Example Rectangle java Using Methods What You Need To Know 1 2 3 4 Name of the method Number of parameters Data type of each parameter Data type of value computed returned by the method 5 The code required to accomplish the task Method Declaration Specifies the code that will be executed when the method is invoked or called Located inside a class definition Contains method header method body Method Header A method declaration begins with a method header public static int countCharInWord char ch String word visibility modifiers return type method name formal parameter list The parameter list specifies the type and name of each parameter The name of a parameter in the method declaration is called a formal parameter Return Value Value returning methods The method returns the result of some operations Like mathematical functions Return one single value Void methods Perform operations but return no value Value Returning Methods Think mathematical function f x 2x 5 f x y 2x y f 1 7 f 2 9 f 3 11 f 1 5 7 f 2 3 7 f 3 1 7 Can have multiple arguments parameters Only one result of the function Value Returning Methods Uses Save the value for future calculation Use the value in a calculation Print the value x Math pow y 2 z a Math pow y 2 x System out println Math pow y 2 Return Type Indicates the type of value that the method evaluates to primitive data type class name void reserved word indicating that nothing is returned When a value is returned the method call is replaced with the returned value int number countCharInWord e Heels 2 The return Statement Tells the computer to return back to where the call was originally made Specifies the value that will be returned return expression The data type of expression must match the method s return type Methods with return types of void usually don t but can have a return statement Value returning methods must have at least one return statement Using return public static double larger double x double y double max if x y max x These are equivalent else methods max y return max public static double larger double x double y if x y return x else return y Example Rectangle java Add computePerimeter Void Methods Do not return a value Have a return type of void Similar in structure to valuereturning methods Call to method is always a standalone statement Can use return statement to exit method early Example Rectangle java Add printPerimeter The main Method The main method looks just like all other methods public static void main String args modifiers return method type name parameter list Method Body The method header is followed by the method body public static int countCharInWord char ch String word int count 0 for int i 0 i word length i if word charAt i ch count ch and word are local return count data The return expression must be consistent with the return type They are created each time the method is called and are destroyed when it finishes executing Example Largest java Read a sequence of numbers Print the max Parameters Each time a method is called the actual parameters in the call are copied into the formal parameters int num countCharInWord e Heels public static int countCharInWord char ch String word int count 0 for int i 0 i word length i if word charAt i ch count return count Parameters Formal parameters variable declarations in the method header automatic local variables for the method Actual parameters actual values that are passed to the method can be variables literals or expressions printStars 35 printStars 30 5 int num 35 printStars num Parameters Primitive Data Type Variables If a formal parameter is a variable of a primitive data type can it be modified inside a method The value from the actual parameter is copied There is no connection between variables inside the method and outside Conclusion it cannot be modified Example Largest java Modify the values inside the comparison method Parameters Reference Variables If a formal parameter is a reference variable can the object be modified inside a method The address from the actual parameter is copied The local reference variable points to the same object Conclusion it can be modified Primitive Data Types What if we want to modify a variable of a primitive data type inside a method Encapsulate it in a class Example IntClass ch 6 p 306 IntClass is a class suggested in the book but it is not a built in class in Java Data Scope The scope of data is the area in a program in which that data can be used referenced Data declared at the class level can be used by all methods in that class Data declared within a method can be used only in that method also called local data Key to determining scope is to look for blocks of code surrounded by variables declared inside cannot be used outside recall the problems some had with variables declared inside if statements Data Scope Example public class Rectangle variables declared here are class level available in all methods in Rectangle class public int computeArea variables declared here are method level only available in computeArea public


View Full Document

UNC-Chapel Hill COMP 14 - Lecture Notes

Download Lecture Notes
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 Lecture Notes 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 Lecture Notes 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?