Using Java Objects - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Using Java Objects

Description:

This assures the programmer can always convert whatever object they wish to a printable form ... The receiver of the message is always on the left ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 14
Provided by: calpoly
Category:
Tags: alwayson | java | objects | using

less

Transcript and Presenter's Notes

Title: Using Java Objects


1
Using Java Objects
  • Using Objects
  • Mixing objects and data types
  • Purpose of the toString method
  • Robert Stumpf, Ph.D.
  • CIS Department

2
Using Objects
  • Messages are usually sent to variables
  • Variables point to objects
  • Messages invoke a method in objects class
  • Messages may have arguments
  • Messages may also be sent to classes directly
  • Methods may return a response

3
Using Objects
  • Example messages without arguments sent to
    variables
  • With no response
  • window.show ( )
  • With a response.
  • inputLine fileIn.readLine ( )

4
Using Objects
  • Example messages with arguments sent to variables
  • With no response
  • window.reshape (0, 0, 600, 400)
  • With a response.
  • value dictionary.get (key)

5
Using Objects
  • Example messages without arguments sent to
    classes
  • With no response
  • no examples, why ?
  • With a response.
  • numberFormat NumberFormat.getCurrencyIns
    tance ( )

6
Using Objects
  • Example messages with arguments sent to classes
  • With no response
  • System.exit (0)
  • With a response
  • anInteger Integer.parseInt (aString)

7
Mixing Objects and Data Types
  • Message arguments may be objects or data types
  • Using an object anInteger Integer.parseInt
    (aString)
  • Using a data type window.reshape (0, 0, 600,
    400)

8
Mixing Objects and Data Types
  • Message responses may be objects or data types
  • Receiving an object todaysDate Date ( )
  • Receiving a data type anInteger
    Integer.parseInt (aString)

9
Mixing Objects and Data Types
  • Mathematical functions are available using the
    class methods from Math class
  • For example
  • Math.pow (1.06, 2.0) yields 1.1236
  • Math.sqrt (9.0) yields 3.0
  • Math.round (1.5) yields 2L

10
toString
  • All Java objects understand the message toString
    ( )
  • This assures the programmer can always convert
    whatever object they wish to a printable form
  • For example
  • String string
  • string temperature.toString ( )

11
toString
  • This means all programmers must construct their
    own classes with a toString method
  • If a programmer forgets to do this, their object
    will use the parents object (usually Object)
    toString method
  • This will usually lead yield a cryptic output
  • For an example of a cryptic output, see
    GregorianCalendar

12
Summary of Objects
  • The receiver of the message is always on the left
  • A dot is always is always used to separate a
    message from a class or object
  • The receiver is always a class or a variable
    pointing to an object
  • The message may have arguments
  • The method may return a response

13
Thank You
  • There are no speed limits
  • on the road to excellence.Thank you
  • Robert Stumpf
  • email rvstumpf_at_csupomona.edu
  • url http//www.csupomona.edu/rvstumpf
Write a Comment
User Comments (0)
About PowerShow.com