Title: Introducing Objects and stuff
1Introducing Objects and stuff
2Definition
- Object a class that is the root of the
hierarchy tree for all classes in JAVA. - An object is defined by a class or
- Primitive Data Includes common values such as
numbers and characters. - Data Type Defines a set of values and
operations. - Operators A symbol that represents an operation
in a programming language, such as the addition
operator
3Continued
- Encapsulation the characteristic of an object
that limits access to its variables and methods.
All interaction with the object occurs through an
interface. - Inheritance The ability to create a new class
from an existing one. Inherited variables and
methods of the original class are available in
the new class if they were declared locally.
4Section 2.1 Using Objects
- Kevin Curtis
- Dennis Truong
- Period 2
5The Basics
- Objects
- The basic software part in an object-orientated
program. - Represents output device or file (i.e. monitor)
6The Basics
- For example
- System.out.println (Whatever you are, be a good
one.) - System.out represents the output device or
file - The objects name is out and stored in the
system - class
- The println method will print a string of
characters to - the screen.
- The parameter is the piece of data sent to a
method, or in this example, the string of
characters printed to the screen.
7Methods and Objects
- Receives information from the parameter
- System.out.println (test)
- Determines how the object functions for example
- print method prints information and stays on the
same line. - println method prints information and then skips
to the next line.
8Methods and Objects
Countdown
System.out
println
main
9Abstraction
- An abstraction means that the details of how it
works does not matter to the user. An object is
an example of an abstraction. - An abstraction hides or ignores certain details.
With a good abstraction hiding the right details
at the right time to manage the complexity. - Level of abstraction the amount of abstraction
that is used to hide the details
102.6 Creating Objects
11Introduction
- Variable can hold primitive value or reference
to object - The new operator returns a reference to a newly
created object - Creating object with new operator is called
instantiation - ex. String name new String (Mr.
Jacobson) - An object is called instance of particular class
- After the new operator creates the object, a
constructor is a string literal
12Dot Operator
- When object is instantiated, we use the dot
operator to get is methods - Dot operator is added right after object
reference and is followed by method being invoked
- ex. Count name.length()
13The String Class
- String in Java are object represented by the
string class - the type shown in front of method name is called
return type of the method - A return type void means that the method doesnt
return a value - Object immutable when value cant lengthened or
short nor can any of its character change - Index is a characters position in string
- The first character is zero
14Wrapper Class
- All primitive type in Java have wrapper class--
let you create objects representing primitive
data - Integer class represent int and double class
represent a double - ex. Integer number new Integer (45)
- The Integer and Double objects are immutable
-
15Chapter 4.0 Writing Classes
16Objects Revisited
- Example of an ObjectA ball has diameter, color
and elasticity. - The properties that describe an object are called
attributes - defined as the objects state of
being. - Behaviors What it does-ex. the ball can be
thrown, bounced, or rolled.
17- Each object has a state and a set of behaviors.
The values of an objects variables define its
state and the methods define its behaviors. - The action of an object will remain the same, but
it depends on the objects state. - The class of an object may contain a method to
add a new course. - Software objects can often represent physical
things, but they dont have to. For example an
error message can be an object, with its state
being the text of the message and behaviors,
including printing the error message. Theres no
limit for possibilities to just be physical
things.
18Classes
- An object is defined by a class
- A class can be described as a model, patter, or
blueprint of the object thats being created. It
has no memory space for data. Each object has
its own data space, thus its own state. - Blueprint - defines the important
characteristics. Ex. A houses walls, windows,
doors, electrical outlets, and so on.
19Chapter 4.1 Review
- Anatomy of a Class
- Darwin Arayata
- Stephen Le
- Jibram Martinez
20- Members of the class are classes containing the
declarations if the data that will be stored in
each instantiated object and the declarations of
the methods that can be invoked using an object. - Refer to the picture on pg. 193 listing 4.1
21Instance Data
- In the coin class, the constants HEADS and TAILS,
and the variable face are declared inside the
class, but not inside any methods. - Location at which variable declared defines its
scope - The coin object, for example, has its own face
variable with its own data space.
-Reference to Page 197 listing 4.3
22Instance Data continued..
- Attributes such as the variable face are also
called instance data because memory space is
created for each instance of the class that is
created. - Java automatically initializes any variable
declared at the class level.
23Encapsulations and Visibility Modifiers
- We can think about an object in one of two ways,
depending on what we are trying to do. - First when we are designing and implementing
objects - We have to define the variables that will be
held in the object and write the methods that
make the object useful - Objects should be encapsulations lated. The rest
of a program should work with an object only
through a well defined interface
24Encapsulations and Visibility modifiers
continued.
- When we are designing a solution we have to think
about the objects in the program work with each
other - In java we create object encapsulations using
Modifiers - A modifier is a java reserved word that names
special characteristics of a programming language
25Continued
- Some Java Modifiers are called visibility
modifiers - They control whether client code can see whats
inside an object
26Anatomy Of A Method
- By Jei Mercado, David Chap, and Kevin Lai
27Anatomy Of a Method
- A method declaration defines the code that is
executed when the method is invoked. - When it is done, control return to the location
where the call was made and execution continue. - Define the same class
- Define Programs
28Return Statement
- A return value must match the return type in the
method header. - Return type in the method header can be a
primitive type class name Reserved word void - Void is used as a return type
- Return is double
29Parameter
- Pass through when invoked
- List in the header of this method list the type
of value that are passed their name - Formal parameter
- Actual value pass into actual parameter
30Constructor
- Cannot have any Return type even void
- Is the same name as the class
- Each class have a different structure
31Local Data
- A variable declared in a method is local to that
method and cannot be used outside of it - Instruct data
32Lesson 4.3
33Uses
- Performing similar operations on different types
of data. - Using the same method name with different
parameter lists for several different methods.
34What is needed
- The compiler still needs to match up each
invocation with a specific method declaration. - It needs a signature, which is the methods
name, number, type, and order of its parameters.
35What happens
- First, the value in the variable is converted to
a string representation. - Next, the two strings are concatenated into one
longer string. - Last, the definition of println that accepts a
single string is called.
36Public class SnakeEyes public static void
main (String args) final
int ROLLS 500 int snakeEyes 0,
num1, num2 Die die1 new Die()
Die die2 new Die(20) for
(int roll 1 roll lt ROLLS roll)
num1 die1.roll()
num2 die2.roll()
if (num1 1 num2 1)
snakeEyes
System.out.println (Number of rolls
ROLLS) System.out.println (Number
of snake eyes snakeEyes)
System.out.println (Ratio
(double)snakeEyes/ROLLS)
37Output
- Number or rolls 500
- Number of snake eyes 6
- Ratio 0.012
38Chapter 4 - Section 4our
By Miguel, Marvin, and Eric
39What is method decomposition????
-a complicated method can be broken into several
simpler methods and helped by support methods.
-in an object-oriented design, breaking up
methods must happen after objects have been
broken.
40For example
-PigLatinTranslator.java (pg. 216-218)
-the translate method is broken up into simpler
methods and uses several support methods.
-translateWord is a support method.
-beginsWithVowel, beginsWithBlend, startsWith are
simpler methods that help the translate method do
its job.
41THE END
Or is it?
42Object Relationship
43Association
- Use Relationship- when two classes are aware of
each other and may use each other. - Ex. An artist object draws a picture object
44Association of objectsof the same class
- A method invoked through one object may take as a
parameter another object of the same class
Woman object taking parameters from Man object
45Aggregation
- An aggregate object is made up, in part, of other
objects, forming a has-a relationship - Ex. A car has a tire
46Great Job!
- I will send a copy to each computer
- This is a good summary of some of the object
oriented programming ideas