CIS 234: Java Methods Dr. Ralph D. Westfall April, 2010.

18
CIS 234: Java Methods Dr. Ralph D. Westfall April, 2010

Transcript of CIS 234: Java Methods Dr. Ralph D. Westfall April, 2010.

Page 1: CIS 234: Java Methods Dr. Ralph D. Westfall April, 2010.

CIS 234: Java Methods

Dr. Ralph D. Westfall April, 2010

Page 2: CIS 234: Java Methods Dr. Ralph D. Westfall April, 2010.

What is a Method? a block of code that does something

has a name and parentheses after name curly brackets enclose statement(s) in body usually has header declarations, and often

has arguments inside the parenthesespublic static void main (String [] args){

System.out.println("Hi!");}

Page 3: CIS 234: Java Methods Dr. Ralph D. Westfall April, 2010.

Declarations - type of access modifiers before method name

public – any other class can use method main method must be public

private – only methods in same class can use it

can leave out the access modifier defaults to friendly – can be used by

other classes in same "package" (a package is somewhat like a directory)

Page 4: CIS 234: Java Methods Dr. Ralph D. Westfall April, 2010.

Declarations - static static means unchanging

all objects created from this class will use this same method

there will not be additional copies of a static class method

if don't use word static, each object created from this class will have its own instance (copy) of method

Page 5: CIS 234: Java Methods Dr. Ralph D. Westfall April, 2010.

Declarations – void or [type] return value type

void means doesn't return any data if not void, need to identify the type

of data the method will returnpublic static long cubeIt(int x) // int arg{ // returns a long integer

return x * x * x;}

Page 6: CIS 234: Java Methods Dr. Ralph D. Westfall April, 2010.

Arguments (args) the parentheses often enclose

"argument(s)" (data) that the method uses, identified by data type

public void say(String what) // argument{

System.out.println(what); // used here} // from Hello, world! Person class code

Page 7: CIS 234: Java Methods Dr. Ralph D. Westfall April, 2010.

Arguments - 2 Java is "strongly typed," so the data

type of every variable must be declared somewhere in a Java class

argument data types are declared within method's parenthesespublic static double raised(int salary, double pct){

return (100 + pct) / 100.0) * salary;}

Page 8: CIS 234: Java Methods Dr. Ralph D. Westfall April, 2010.

Calling a Method from within its class

method name([arguments, if has any]) ;say("Hi!"); // literal argument

from another object or class [object].[method name]([arguments]) ;myObject.cube(num); // variable argumentmyObject.raised(empSalary, 5); //both typesMath.sqrt(4); // literal in a class method

Page 9: CIS 234: Java Methods Dr. Ralph D. Westfall April, 2010.

Calling a Method - 2 arguments must be in right order

static double raise(double salary, double pct)

......raise (wages, increase) ; // correct orderraise(7, 50000) ; // logical error

argument names don't need to matchmyObject.raise(pay, increase) ;

// declared in method as // (double salary, double pct)

Page 10: CIS 234: Java Methods Dr. Ralph D. Westfall April, 2010.

"Signature" a method's "signature" is a pattern:

number, order, and types of arguments can have 0 [called with [name]( );] to many

an "overloaded" method has more than one signature e.g., can be called with different

patterns of arguments: (); (int x); (int x, double y); (int x, char y);

Page 11: CIS 234: Java Methods Dr. Ralph D. Westfall April, 2010.

Types of Java Programs applications

class that can run without any other programs (often used with other classes)

class(es) that doesn't run by itself, but is used by another application class

applet class runs inside a web browser (the

browser actually is a program) // [PgDn]

Page 12: CIS 234: Java Methods Dr. Ralph D. Westfall April, 2010.

Types of Java Programs - 2 servlet

runs on a server and makes it possible to generate web pages with database content

programmer can create a Java Server Page (JSP), which then gets compiled into a servlet (Blackboard pages are servlets)

servlets are part of the Java Enterprise Edition (JDK + Java EE bundle)

Page 13: CIS 234: Java Methods Dr. Ralph D. Westfall April, 2010.

Main Method one class in a Java application

must have a main( ) method but a class used by an application

doesn't need a main( ) method main method must be declared as:

public static void

Page 14: CIS 234: Java Methods Dr. Ralph D. Westfall April, 2010.

Main Method - 2 main method runs first when

application starts it can call methods in its own class,

and/or use methods in other classes methods it calls in its own class must

be static variables that are not inside any

method in the class must also be static

Page 15: CIS 234: Java Methods Dr. Ralph D. Westfall April, 2010.

Main Method - 3 main method has String[ ]

argument(s) means that you can "pass" one or more

words as inputs into class when you run itpublic class MyProgram{

public static void main (String[ ] args)[in DOS after class has been compiled] C:\>[path] java MyProgram Le 19 // 2 args

Page 16: CIS 234: Java Methods Dr. Ralph D. Westfall April, 2010.

Using Extra Methods in a Class saves typing (or pasting)

can use a separate method, rather than keep repeating same block of code in main method

makes code easier to read and maintain smaller programs code is organized into blocks //Notes

Page

Page 17: CIS 234: Java Methods Dr. Ralph D. Westfall April, 2010.

Exercise following pattern in DemoMethods.java,

fill in the blanks in the handout to: declare 3 numbers and one String as

member variables (above main method), using meaningful variable names

assign values to three of these variables in the loadData method (leave one of the numeric variables without a value

Page 18: CIS 234: Java Methods Dr. Ralph D. Westfall April, 2010.

Exercise (part 2) following pattern in

DemoMethods.java, write the following 4 methods: print a literal value with say( method add 2 numbers together and store them in

a member variable (above main method) print this calculated member variable multiply two numbers and print result with

some identifying text