COMP 14 Introduction to Programming Adrian Ilie July 8 2005 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Anatomy of a Class A class contains data declarations and method declarations int width int length Data declarations Method declarations operations 2 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Writing a class public class data private private Rectangle members int width int length methods public double computeArea public static void main 3 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL 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 4 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL 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 5 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL 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 6 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL 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 7 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Example Rectangle java 8 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL 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 9 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL 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 10 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL 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 11 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL 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 12 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL 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 13 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL 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 14 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL 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 15 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL 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 16 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Using return public static double larger double x double y double max if x y These are equivalent max x methods else max y return max public static double larger double x double y if x y return x else return y 17 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Example Rectangle java Add computePerimeter 18 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL 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 stand alone statement Can use return statement to exit method early 19 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Example Rectangle java Add printPerimeter 20 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL The main Method The main method looks just like all other methods public static void main String args modifiers 21 Adrian Ilie return method type name parameter list The UNIVERSITY of NORTH CAROLINA at CHAPEL 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 22 Adrian Ilie They are created each time the method is called and are destroyed when it finishes executing The UNIVERSITY of NORTH CAROLINA at CHAPEL Example Largest java Read a sequence of numbers Print the max 23 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL 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 24 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL 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 25 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Parameters Primitive Data Type Variables If a formal parameter is a variable of a
View Full Document