Methods - PowerPoint PPT Presentation

About This Presentation
Title:

Methods

Description:

We have seen some assignment statements where we do the computation in the program ... Proceedural Abstraction. We define the input and output behavior. Contract ... – PowerPoint PPT presentation

Number of Views:11
Avg rating:3.0/5.0
Slides: 20
Provided by: thaddeusf
Category:

less

Transcript and Presenter's Notes

Title: Methods


1
Methods
  • CSC 171 FALL 2001
  • LECTURE 3

2
History
  • The abacus

3
HistoryThe Father of Computing
  • 1822 - Charles Babbage
  • The Difference Engine
  • First Government grant
  • Babbages ideas were not widley accepted because
    of poor documentation

4
Methods
  • Variables describe static aspects
  • nouns
  • Methods describe dynamic aspects
  • verbs, behaviors
  • Methods do things

5
Methods
  • We have seen some assignment statements where we
    do the computation in the program
  • int x a 5
  • String b Hello World\n

6
Methods
  • We have also had some occasion to use methods
    that do computation elsewhere
  • double d1 Math.sqrt(50.9)

7
Methods
  • Think of a method as a black box
  • We put something in
  • We get something out
  • Putting something in
  • parameters
  • Getting something out
  • Return values

8
Black Box
Math
9.0
sqrt()
3.0
double x Math.sqrt(9.0)
9
A Computer Program
  • public class myFirstProg
  • public static void main(String args)
  • System.out.println(Hello, CSC171)

10
Black Box
String args
MyFirstProg
main()
void
side effects
11
A Computer Program
  • public class mySecondProg
  • public static void main(String args)
  • int x 5, y 8
  • int product mymult(x,y)
  • System.out.println( x y is
    product)
  • public static int mymult(int n1, int n2)
  • return n1 n2

12
Black Box
String args
MyFirstProg
main()
void
mymult()
13
Sequence of events
main()
mymult()
5,8
System.out.println()
40
5 8 is 40
void
14
Proceedural Abstraction
  • We define the input and output behavior
  • Contract
  • We embed some complex behavior in a method.
  • We refer to the method by a name
  • We can write debug the method once then use
    it whenever we want.

15
  • public class myThirdProg
  • public static void main(String args)
  • int x 5, y 8
  • int product mymult(x,y)
  • System.out.println( x 3 y is
  • product)
  • public static int mymult(int n1, int n2)
  • return mycube(n1) n2
  • public static int mycube(int x) return x x
    x

16
Sequence of events
mycube()
main()
5
mymult()
5,8
125
1000
System.out.println()
53 8 is 1000
void
17
Interfaces needed for project
  • Interfaces are ways of writing contracts
  • Client communicates the input output
    requirements
  • Method names
  • Parameters for each method
  • Return types for each method

18
Interface definition
  • public interface myMinMax
  • public int myMin(int x, int y) // no body
  • public int myMax(int x, int y) // no body

19
Interface use
  • public class myProject
  • implements myMinMax
  • public int myMin(int x, int y)
  • if (x lt y) return x else return y
  • public int myMax(int x, int y)
  • if (xgty) return x else return y
Write a Comment
User Comments (0)
About PowerShow.com