Title: CS2
1CS2
- Module 9
- Category Elements of Java
- Topic Methods
- Objectives
- Describe all that is important regarding methods
including main.
2CS 2
- Introduction to
- Object Oriented Programming
- Module 9
- Elements of Java
- Methods
3MethodMadness
4Caution
Before we can talk about methods in Java, we need
to look at programming languages in general,
from a very high level. Don't be alarmed by some
of the syntax on the following slides. The big
picture is what's important, not the coding
details....
5Programming Paradigms
Procedural Programming 'Imperative'
assignment used to create state, and procedures
manipulate that state. E.g., C, Fortran, Pascal
int y int x 3 y
manipulateData(x) Functional Programming
Functions (procedures that do not depend on
outside data) are used to provided data. E.g.,
Lisp, Scheme. (defun check-member (input-item
input-list) (cond ((null input-list)
nil) ((equal input-item
(first input-list)) T) (T
(check-member input-item (rest input-list)))))
6Programming Paradigms
Object-Oriented Programming All data exists
within objects interaction occurs only between
objects. Even numbers are objects that know how
to add themselves. E.g., SmallTalk array
array Array new 5. rect 0_at_0 corner
8_at_9. 1 to array size do item rect
origin item_at_item. array at item put rect
copy . Java Object-oriented, but not 100
OO, since it contains primitives.
There are other paradigms, but these three help
explain where Java comes from
7Method
- Recall that the OO paradigm calls for us to
encapsulate data and behavior inside of the
object (defined by the class). - The method is the tool to accomplish the behavior
part. - Methods are equivalent to procedures and
functions in other langauges - They only occur inside of a class
- They effectively allow us to send an object a
message telling it to do something
8Java Methods
- There exists in Java a single construct, the
method, to express both "procedures" and
"functions" - When a procedure (like a command) is called for,
specify the return type "void" before method
name - public void printHelloWorld( )
-
- System.out.println("Hello World!")
- // of printHelloWorld
- Note All methods must have parentheses for the
parameter list . . .even if there are no
parameters! It's how Java knows it's a method!
Note the comment
9Java Methods
- Single construct for both procedures and
functions - When a function is called for, specify the
- appropriate return type before method name
- public float average (float num1, float num2,
float num3) -
- float returnVal
- returnVal (num1 num2 num3)/ 3
- return (returnVal)
- // of average
- Recall that a function resolves to a value. The
return type (float in this case) specifies the
type of the value. Java will insist that you
actually return something of that type
10Don't be alarmed
For now make methods public later we'll explain
other options
Use this with a "main" method for sure. Details
forthcoming
Method name
- public static float square(float x)
-
- float retval
- retval x x
- return retval
Return Value Mandatory for all methods even if
it's "void"
Parameter list
11Method Signatures
"The signature of a method consists of the name
of the method and the number and types of formal
parameters to the method. A class may not declare
two methods with the same signature, or a compile
time error occurs." --Java Language
Specification s.8.4.2 Method overloading occurs
where identically named methods have subtle
variations in the method parameters. public int
getCube(int side) return side side
side public int getCube(float side)
return (int)(side side side)
public int getCube(double side) return
(int)(side side side)
CS1 associated two similar methods by calling
one a "helper" method. Java lets you overload
the name instead.
12Warning!
- Make sure that right now you get a clear
understanding of what is meant by method
overloading.
13Methods Common Mistakes
public float average (float num1, float num2,
float num3) float returnVal
returnVal (num1 num2 num3)/ 3
return returnVal // of average
- Note the mistakenly placed ending semicolon
- Compiler wrongly views this now as an "abstract"
method (more on abstract methods later) - Results in unhelpful error message about abstract
methods - You are likely to make this mistake!
14Where do Methods Go?
- Any method we create must appear in a class, as
either - a class member (will be marked static) or
- an instance member.
- More on creating classes and objects shortly . .
. - For now, just know that all methods belong in
some class.
15OO Programming Note
Already, we've covered one of the most important
aspects of Object Oriented (OO) programming the
notion that data and methods belong together in a
class. Right now, it's sufficient that you
merely know that variables and methods belong in
classes. Later, we'll see how this enables us to
encapsulate state (the variables) with behavior
(the methods). So, remember this day. You've
started to learn THE cornerstone of OO
programming.
16Writing Methods A Larger Look
- A Java requirement
- All methods belong to an object (or class).
- Name of object (or class) must be unambiguous
when method is called. - To run a program, there must be a class (whose
name is the name-of-the-program), containing
a special method called main
17main Method
18The main method
Array of strings thatcan accept command line
parameters
visible to all
nothingreturned
public static void main (String args)
a class method,not an instance method(the
method is ready to runwhen loaded)
Method name
19Why?
- Look ahead We're going to write one or more
classes. - Classes will be used to produce objects
- But how does it all start up???
- We need at least one method that is part of a
class (and not an object) that will start
everything up. - The "static" is what tells Java the method that
it belongs to the class and not any particular
object - The name "main" is the Java standard for the
first method to be run in the first class to be
loaded
20class A public static void main(...
Each class can have it's own main method. You
indicate the one to run when invoking the
JVM. This fact becomes critical when we learn
to write debug test mains.
class B public static void main(...
class C public static void main(...
21Questions?
22(No Transcript)