Introduction to Objects - PowerPoint PPT Presentation

About This Presentation
Title:

Introduction to Objects

Description:

Using the Math class differs from previous classes because we pass the class ... for JOptionPane and Math, we didn't need an object, why? ... – PowerPoint PPT presentation

Number of Views:73
Avg rating:3.0/5.0
Slides: 13
Provided by: foxr
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Objects


1
Introduction to Objects
  • Objects are the main structure constructed from
    Java programs
  • An object represents some real-world physical or
    abstract object
  • In Java, you write a class
  • And instantiate copies of the class as objects
  • That is, an object is an instance of a class
  • The object contains all of the class components
    (data, code) but has its own unique values for
    the data unlike any other object

2
Creating an Object
  • Assume you have a pre-defined class (e.g.,
    String, Random)
  • If the class is not automatically loaded, you
    must import it
  • import java.util.Random
  • You must declare a variable to be of that type
  • Random generator
  • You must instantiate the object
  • generator new Random( )
  • You interact with the object via message passing
  • generator.nextInt( ) // produces a random int
    value

3
Message Passing
Object Data instances d1 d2 d3 Methods m1
m2 m3 m4
Program that uses object object.m1( ) o
bject.m2( )

The object is comprised of methods (chunks of
code activated by messages) and internal data (d
ata instances) that describes that object but are
inaccessible outside of the object
A program creates an instance of the object and
then passes that object message (the name of a
method) the method manipulates the objects data
instances
4
What Does new Do?
  • The reserved word new plays the role of
    instantiating an object
  • This causes the operating system to provide the
    memory space needed for the object
  • And initializes the object as needed
  • Many classes require parameters to initialize
    their object, we will see some examples as we go
    through this section
  • Once done, you can now interact with the object
    by passing the object messages
  • However, if you try to pass a message to an
    object that is declared but not instantiated, you
    will receive a NullPointerException

5
Example Random
  • The Random class is used to generate random
    numbers
  • It is part of the java.util package
  • No parameters are passed when you instantiate the
    object (generator new Random( ) )
  • Messages
  • nextInt( )
  • nextDouble( )
  • nextFloat( )
  • Most commonly, you will generate an int value but
    nextInt( ) will generate any int (from 2 billion
    to 2 billion)
  • So, we will want to limit the range as follows
  • generator.nextInt( ) range
  • this creates a value between (range-1) and
    (range-1)
  • Or Math.abs(generator.nextInt( ) range) for a
    value between 0 and range-1
  • Or Math.abs(generator.nextInt( ) range) 1

6
Example Date
  • The Date class contains the current date, day and
    time
  • It is a simple class which will mostly be used to
    output the current date/day/time and we will not
    interact with it otherwise
  • import java.util.Date
  • Date today new Date( )
  • System.out.println(today)
  • Date has messages we could use such as getTime( )
    which returns the time as the number of seconds
    since midnight on 1/1/70, but mostly we will just
    output it

7
Example DecimalFormat
  • One unfortunate aspect of Java is if you output a
    float or double
  • The entire value is output
  • For instance, if you have computed a square root
    to be 3.18371615913, then this whole number is
    output!
  • We can restrict the output using the
    DecimalFormat class
  • import java.text.DecimalFormat
  • DecimalFormat df new DecimalFormat(0.00)
  • double x Math.sqrt(y)
  • And now
  • System.out.println(df.format(x))
  • Or
  • String temp df.format(x)
  • System.out.println(temp)

8
DecimalFormat Pattern
  • Notice, unlike when we instantiated a Random
    object, for a DecimalFormat object we included
    something in the ( )
  • (0.00)
  • This initializes the DecimalFormats internal
    data, which stores this as a pattern to use when
    performing output
  • The 0.00 means output at least 1 digit to the
    left of the decimal point, even if it is a 0, and
    output exactly 2 decimal points of values after
    the decimal, even if they are 0s
  • Instead of 0, you can use which means only
    output a digit in this position if it is a
    non-zero

9
The Math Class
  • Like String, Math is a class that is
    automatically loaded (you dont have to import
    it)
  • Math has methods
  • abs (absolute value)
  • sin, cos, tan, acos, asin, atan (trigonometry
    functions)
  • sqrt (square root)
  • pow (raise to the power of) as in Math.pow(x, 3)
    is equal to x3
  • round round off to nearest long (not int)
  • random generates a random double between 0.0
    and 1.0 (but not including 1.0)
  • Using the Math class differs from previous
    classes because we pass the class messages
    directly as in Math.abs(x) or Math.random( )

10
When Do You Need an Object?
  • We have now used several classes
  • Random, DecimalFormat, Date, String, JOptionPane,
    Math
  • Things seem confusing though
  • for Random, DecimalFormat and Date, we had to
    create an object
  • for String we used a variable
  • for JOptionPane and Math, we didnt need an
    object, why?
  • For String, the variable was actually an object,
    we were just permitted to skip the instantiation
    step because Java offers this as a shortcut (but
    remember, we still passed our String variables
    messages)
  • For most classes, we need an object that is
    declared and instantiated
  • but a few classes (like JOptionPane and Math) do
    not require this, so instead we pass our messages
    directly to the class

11
Another Handy Class Timer
  • We will use the Timer class later this week
  • when we start combining graphics and other Java
    features
  • A Timer object counts milliseconds and when a
    certain number elapse, it generates an event
  • If you want to handle the event, then you can
    make something happen
  • for instance, in a game, a spaceship that you are
    shooting at might move slightly each time an
    event is generated
  • The Timer class has messages to start and stop
  • We will examine Timer in more detail on Friday

12
Creating Our Own Objects
  • It is important to understand what objects are at
    this point of our camp because you need to use
    them
  • However, we havent learned enough to create our
    own objects
  • Once we do, we will start creating objects and
    using them in other objects we are creating, thus
    building up our own programs and the language
    itself (once written, anyone could use an object
    that you create!)
Write a Comment
User Comments (0)
About PowerShow.com