2.3 Creating and Storing Objects
Unit 2 – Using Objects
AP Computer Science A
1
2.3 Creating and Storing Objects
Learning Objectives
You will be able to…
2
2.3 Creating and Storing Objects
Activity - Java Zoo
3
2.3 Creating and Storing Objects
Class Diagram
A class diagram is an organized template that allows you to preview the contents of class, such as the attributes and methods.
4
Mr. Potato Head Blueprint
Characteristics:
Number of eyes
Color of eyes
Color of Hat
Number of Arms
Facial hair
Behaviors:
Talk
Move
Yell
Change color of nose
2.3 Creating and Storing Objects
Class Diagrams
5
Mr. Potato Head Blueprint
Characteristics:
Number of eyes
Color of eyes
Color of Hat
Number of Arms
Facial hair
Behaviors:
Talk
Move
Yell
Change color of nose
iPod blueprint
attributes:� current song� volume� battery life
behavior:� power on/off� change station/song� change volume� choose random song
2.3 Creating and Storing Objects
Creating a New Object
Turtle mertle = new Turtle(habitat)
<Class> <variable name> = new <Class>(argument)
6
2.3 Creating and Storing Objects
Constructors
A constructor is a special method (function) used to create an instance of a class
7
2.3 Creating and Storing Objects
Constructors
8
2.3 Creating and Storing Objects
Arguments
An argument is a value that is passed into a method or constructor.
9
Animal tiger = new Animal(“orange”);
//create animal with orange fur
2.3 Creating and Storing Objects
Arguments
10
creates a World object with length 100 and height 300.
creates a Turtle object on the world called w with a default location (required).
creates a Turtle object on the world called w with a location (200,100).
2.3 Creating and Storing Objects
Overloaded Constructors
A constructor is said to be overloaded if there are multiple constructors with the same name but different arguments can be placed in the parenthesis.
11
2.3 Creating and Storing Objects
Activity Cont. - Java Zoo
12
2.3 Creating and Storing Objects
Class Diagram
13
Turtle |
Turtle(World w) Turtle(World w, int xpos, int ypos) |
|
|
Class Name
Constructors
Attributes
Methods
2.3 Creating and Storing Objects