Objects, Methods - PowerPoint PPT Presentation

1 / 8
About This Presentation
Title:

Objects, Methods

Description:

The keyword static (class method) visibility static returnType name ... Must be executed on an object belonging to the class in which the method was defined ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 9
Provided by: jliu4
Category:
Tags: class | methods | objects

less

Transcript and Presenter's Notes

Title: Objects, Methods


1
Objects, Methods Multimedia
  • CS 026 Lab4

2
Java Method
  • A method is a named collection of statements that
    have a specific purpose
  • How to define a method (object method)
  • visibility returnType name (parameterList)
  • body of method (a collection of statements)
  • The keyword static (class method)
  • visibility static returnType name
    (parameterList)
  • body of method (a collection of statements)

3
Java Method (contd)
  • visibility public and private
  • returnType such as int, double, void
  • name the name of the method, starting with a
    lowercase word and uppercasing the first letter
    of each additional word
  • Parameter list specifies the values passed in to
    the method
  • public void makeSquare (int length)
  • this.turnRight()
  • this.forward(length)
  • this.turnRight()
  • this.forward(length)
  • this.turnRight()
  • this.forward(length)
  • this.turnRight()
  • this.forward(length)

4
Object Method (Instance Method)
  • Can be thought of as a message sent to an object,
    to do something
  • Must be executed on an object belonging to the
    class in which the method was defined
  • turtle1 is a object of Turtle class, forward() is
    a predefined in Turtle class
  • Invoke object method objName.methodName()
  • turtle1.forward(100)forward() is a object method

5
Class Method (Static Method)
  • Can be thought of as a message sent to a class,
    to do something
  • Does not need to be executed on an object
    belonging to the class
  • static double max (double a, double b) in class
    Math.
  • Invoke class method className.methodName()
  • Math.max(5.0, 15.5) max() is a class method in
    class Math

6
Digital Pictures
  • A collection of pixels
  • Each pixel has a red, green, and blue value (RGB
    value)
  • To manipulate a picture, we manipulate the pixels
    that make up the picture (such as change the
    value of pixel using setRed(), setGreen(),
    setBlue() )

For example Pixel 1 (0, 0) Pixel 2 (0,1) .
How about pixels? (0, 4), (1, 4), (2, 0) --out
of boundary!
7
Documentation(part of Java Language)
  • For all predefined classes in DrJava
  • \intro-prog-java\bookClasses\doc
  • For predefined Turtle class in DrJava
  • /intro-prog-java/bookClasses/doc/Turtle.html
  • For predefined Picture and Pixel classes in
    DrJava
  • /intro-prog-java/bookClasses/doc/Picture.html
  • /intro-prog-java/bookClasses/doc/Pixel.html
  • For predefined class in Java 5
  • http//java.sun.com/j2se/1.5.0/docs/api/

8
QUESTION?
Write a Comment
User Comments (0)
About PowerShow.com