Methods in Java (9) - COP 3330: Object-Oriented Programming...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
COP 3330: Methods In Java Page 1 © Dr. Mark Llewellyn COP 3330: Object-Oriented Programming Summer 2011 Methods In Java – A Closer Look Department of Electrical Engineering and Computer Science Computer Science Division University of Central Florida Instructor : Dr. Mark Llewellyn markl@cs.ucf.edu HEC 236, 407-823-2790 http://www.cs.ucf.edu/courses/cop3330/sum2011
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COP 3330: Methods In Java Page 2 © Dr. Mark Llewellyn Methods In Java A method is a construct for grouping statements together to perform some function. By writing a method, you can write the code once for performing the function in a program and reuse it in many other programs. For example, when you need to find the maximum between two numbers. Whenever you need this function, you have to write the following code: By defining a method for this code, you do not have to repeatedly write the code. int num1, num2, result; if (num1 > num2) result = num1; else result = num2;
Background image of page 2
COP 3330: Methods In Java Page 3 © Dr. Mark Llewellyn Methods In Java We’ve already been using several methods in some of the sample programs we’ve seen. For example, we’ve used: System.out.print JOptionPane.showInputDialog System.out.println JOptionPane.showMessageDialog Double.parseDouble All of these methods are defined in various Java libraries. You’ve also been creating your own methods based on UML class diagrams and are now familiar with the differences between class and instance methods. We now want to consider more complex problems and learn the concept of method abstraction.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COP 3330: Methods In Java Page 4 © Dr. Mark Llewellyn Defining Methods In Java The syntax for defining a method in Java is: modifier returnValueType methodName (parameter list) { //method body }
Background image of page 4
COP 3330: Methods In Java Page 5 © Dr. Mark Llewellyn Defining Methods In Java The method header specifies the modifiers, return value, method name , and parameters of the method. A method may return a value. The returnValueType is the data type of the value the method returns. Some methods may perform their desired operations without returning a value. In this case, the returnValueType is the keyword void . A method that returns a value is called a value-returning method , and a method that does not return a value is called a void method .
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COP 3330: Methods In Java Page 6 © Dr. Mark Llewellyn Defining Methods In Java The variables defined in method header are known as formal parameters or simply parameters . A parameter is like a placeholder. When a method is invoked, a value is passed to the parameter. This value is referred to as an actual parameter or argument . The parameter list refers to the type, order, and number of the parameters of a method. The method name and the parameter list together constitute the method signature .
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 75

Methods in Java (9) - COP 3330: Object-Oriented Programming...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online