Introduction to Methods Shirley Moore CS 1401 Spring 2013 cs1401spring2013.pbworks.com April 1,...

17
Introduction to Methods Shirley Moore CS 1401 Spring 2013 cs1401spring2013.pbworks.com April 1, 2013

Transcript of Introduction to Methods Shirley Moore CS 1401 Spring 2013 cs1401spring2013.pbworks.com April 1,...

Page 1: Introduction to Methods Shirley Moore CS 1401 Spring 2013 cs1401spring2013.pbworks.com April 1, 2013.

Introduction to Methods

Shirley MooreCS 1401 Spring 2013

cs1401spring2013.pbworks.comApril 1, 2013

Page 2: Introduction to Methods Shirley Moore CS 1401 Spring 2013 cs1401spring2013.pbworks.com April 1, 2013.

Agenda

• Announcements• Exam 2 Recap• Introduction to Methods• Assignments for next class• Lab 6 intro

Page 3: Introduction to Methods Shirley Moore CS 1401 Spring 2013 cs1401spring2013.pbworks.com April 1, 2013.

Announcements

• CS Seminar• Homework deadlines will be strictly enforced

from now on (ask for help early, in lab or by email to instructor or TA)

• Guest lecturer on Thursday, April 4 (graduate student Sonish Shrestha)

• Quiz 5 on Thursday

Page 4: Introduction to Methods Shirley Moore CS 1401 Spring 2013 cs1401spring2013.pbworks.com April 1, 2013.

Exam 2 Results

Page 5: Introduction to Methods Shirley Moore CS 1401 Spring 2013 cs1401spring2013.pbworks.com April 1, 2013.

Learning Outcomes

• After completing this lesson, students will be able to– explain the concepts of a method, parameters,

arguments, and return values– define and invoke methods with and without

parameters and with and without return values– develop modular code that is easy to read and

maintain

Page 6: Introduction to Methods Shirley Moore CS 1401 Spring 2013 cs1401spring2013.pbworks.com April 1, 2013.

Method

• Groups statements that perform a specific function together

• Enables reuse• Organizes and simplifies program structure• You have already been using methods!

– System.out.println()– Math.random()– Math.pow()

Page 7: Introduction to Methods Shirley Moore CS 1401 Spring 2013 cs1401spring2013.pbworks.com April 1, 2013.

Defining a Method• Syntax: <modifiers> <returnValueType> <methodName> (<list of parameters>) { <methodBody> }

• Example: (label method header, method body, modifiers, return value type, method name, parameter list (formal parameters), method signature)

public static int maxInt (int num1, int num2) { if (num1 < num2) return(num1); else return(num2); }

Page 8: Introduction to Methods Shirley Moore CS 1401 Spring 2013 cs1401spring2013.pbworks.com April 1, 2013.

Invoking (Calling) a Method

• Examples: int larger = maxInt(1, 4); System.out.println(maxInt(5, 3)); int a = 5; int b = 3; System.out.println(maxInt(a, b));• Label actual parameters, also called

arguments.

Page 9: Introduction to Methods Shirley Moore CS 1401 Spring 2013 cs1401spring2013.pbworks.com April 1, 2013.

Main Method Modifiers

• The mystery of what this means: public static void main(String[] args) {• Modifiers

– public means that the method is visible and can be called from other objects of other types. Other alternatives are private, protected, package and package-private.

– static means that the method is associated with the class, not a specific instance (object) of that class. This means that you can call a static method without creating an object of the class.

– void means that the method has no return value. If the method returned an int you would write int instead of void

Page 10: Introduction to Methods Shirley Moore CS 1401 Spring 2013 cs1401spring2013.pbworks.com April 1, 2013.

Class Activity

• Modify the solution to Exam 2 Problem 6 to use a maxInt and a minInt method.– Download Grades.java (no methods) and

Grades2.java (minInt implemented) files from class website

• Print out your code and label all the parts of your method definitions and invocations.

Page 11: Introduction to Methods Shirley Moore CS 1401 Spring 2013 cs1401spring2013.pbworks.com April 1, 2013.

Tracing a Program with Methods

• When a method is invoked, the runtime system creates an activation record and places it on the call stack.– The activation record contains the parameter

values and any local variables of the method– Activation records are stored on the stack in a last-

in first-out (LIFO) manner• Class activity: Trace the Grades2 program for

the input: 3 80 100 40

Page 12: Introduction to Methods Shirley Moore CS 1401 Spring 2013 cs1401spring2013.pbworks.com April 1, 2013.

Void Method

• A void method does not return a value.• Class Activity

– Download the LetterGrade.java program from the class website.

– Print out and label the parts of the method definition and invocations.

– Modify the program so that the submethod returns the grade as a character value and the main method prints the grade.

– Which version do you like better? Why?

Page 13: Introduction to Methods Shirley Moore CS 1401 Spring 2013 cs1401spring2013.pbworks.com April 1, 2013.

More about Parameters

• Parameters must be passed in the correct order.– Class activity: Download the program

WelcomeToJava.java from the class website.• Why doesn’t it compile?• Fix it and then run it and trace it.

• Parameters are passed by value.– Class activity: Download the program PrintMax.java

from the class website.• Compile and run it and observe the results.• Why doesn’t it work?

Page 14: Introduction to Methods Shirley Moore CS 1401 Spring 2013 cs1401spring2013.pbworks.com April 1, 2013.

Modular Program Design

• Use methods to– make program easier to read and maintain– enable code reuse– enable plug-and-play (i.e., substitute a different

method implementation with the same functionality)

– allow multiple people to work on the same program

Page 15: Introduction to Methods Shirley Moore CS 1401 Spring 2013 cs1401spring2013.pbworks.com April 1, 2013.

Greatest Common Divisor

• Class activity: Download GreatestCommonDivisor.java from the class website.– Compile, run, and trace the code with sample

inputs.– Extra credit: Replace the gcd method with a more

efficient implementation. To get the extra credit, you must comment your code and be able to explain your method.

Page 16: Introduction to Methods Shirley Moore CS 1401 Spring 2013 cs1401spring2013.pbworks.com April 1, 2013.

Assignments for Thursday, April 4

• Study for Quiz 5 over sections 5.1-5.6• Read sections 5.8 and 5.9 and do CheckPoint questions• Turn in Programming Exercises 5.1, 5.5, 5.8

– Turn in .java files.– Don’t need to turn in algorithm, just program, but program

must be commented with algorithmic steps– May modify posted solution to SortThree on class website

for problem 5.5– Grading (10pts for each problem): correct operation (5

points), good programming style (2 points), explanatory comments (3 points)

Page 17: Introduction to Methods Shirley Moore CS 1401 Spring 2013 cs1401spring2013.pbworks.com April 1, 2013.

Lab 6

• Due by beginning of lab on Tuesday, April 9.– No extensions will be given (Get help early!)

• Monte Carlo simulation of definite integrals– You can do this even if you don’t know calculus!