Methods - PowerPoint PPT Presentation

1 / 8
About This Presentation
Title:

Methods

Description:

reported answer is 1339571480) 5) 5. 6) Compiler Error. cannot find ... String str = 'The Detroit Tigers ' 'are the best team in ' 'all of baseball. ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 9
Provided by: jacobh
Category:

less

Transcript and Presenter's Notes

Title: Methods


1
Methods
  • Homework Solutions
  • 7
  • 2) 1
  • 3) 9
  • 4) Runtime Error infinite loop
  • (actually, int overflow reported answer is
    1339571480)
  • 5) 5
  • 6) Compiler Error
  • cannot find symbol
  • variable count.
  • 7) 0
  • 8) X final value 4
  • Y final value 7
  • Sum 13
  • Product 90

2
Method basics
  • Always
  • represents a series of commands.
  • ends with parentheses
  • begins with lowercase letters
  • Sometimes
  • returns a value
  • requires information

3
Examples
  • String str What is up?
  • str str.toUpperCase()
  • Person p new Person()
  • p.setPetsName(Apollo)
  • int dudesAge p.getAge()

returns a value becomes a new String object
No extra info required.
Extra info required in order for method to
execute.
returns a value becomes a new int primitive
Does not return a value doesnt turn into
anything just a command
4
Complex method calls
  • person1.setName(person2.getName())
  • String str The Detroit Tigers
  • are the best team in
  • all of baseball.
  • String sport str.substring(
  • str.indexOf(base),
  • str.indexOf(.))

returns a String object.
returns an int
returns an int
5
Two kinds of methods
  • Ordinary methods
  • operate on a particular object
  • p.setName(Freddy)
  • str.charAt(3)
  • acc.deposit(123.45)
  • b1.addActionListener(this)
  • rect.getArea()
  • static methods
  • operate from a class
  • context is separate from individual objects.
  • Math.sqrt(9.0)
  • Account.updateAllAccounts()
  • Person.numObjectsCreated()
  • Fish.pondTemperature()
  • Time.valueOf(123400)

6
The Math Class
  • The Math class API
  • All methods are static.
  • Basic methods
  • abs(double a)
  • abs(int a)
  • max(double a, double b)
  • max(int a, int b)
  • min(double a, double b)
  • min(int a, int b)
  • pow(double a, double b)
  • random()
  • squrt(double a)

7
Overloaded Methods
  • When a class uses the same method name more than
    once, the method is said to be overloaded.
  • Math.abs(2) //no confusion!
  • Math.abs(2.1) //no confusion!
  • Java knows which method to use from the parameter
    type.
  • Must use a new permutation of parameter types to
    reuse a method name.

8
Practice
  • Pretend you have a Person class with the
    following methods
  • getName() setName(String n)
  • getAge() setAge(int a)
  • Write a line of code that creates a Person
    object.
  • Write a line of code that gives this person a
    name.
  • Write a line of code that gives this person an
    age.
  • Write a line of code that creates a String object
    called xName and assigns to it the name of your
    person. (Pretend you have forgotten the name.)
  • Write a line of code that creates an int
    primitive variable called xAge and assigns to it
    the age of your person. (Again, pretend you have
    forgotten the age.)
Write a Comment
User Comments (0)
About PowerShow.com