1 of 13

2.3 Creating and Storing Objects

Unit 2 – Using Objects

AP Computer Science A

1

2.3 Creating and Storing Objects

2 of 13

Learning Objectives

You will be able to…

  • create class diagrams to represent the attributes and behavior of a class.
  • use a constructor to create an instance of a class.

2

2.3 Creating and Storing Objects

3 of 13

Activity - Java Zoo

  • You are going to create an Animal class..
    • choose the animal for this class
    • must have at least 4 attributes
      • your attributes need to have a data type
        • string, boolean, int, double...
    • must have at least 4 behaviors
      • name them using camelCasing

3

2.3 Creating and Storing Objects

4 of 13

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

5 of 13

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

6 of 13

Creating a New Object

Turtle mertle = new Turtle(habitat)

<Class> <variable name> = new <Class>(argument)

  • This is what is referred to as a constructor - a statement that constructs the object from the class.

6

2.3 Creating and Storing Objects

7 of 13

Constructors

A constructor is a special method (function) used to create an instance of a class

7

  • The constructor of a class is a method that allows us to initialize the attributes(variables) of an object when it is first created.
  • Constructors always have the same name as the class and are used with the keyword new.
  • An object variable is created using the keyword new followed by a call to a constructor.

2.3 Creating and Storing Objects

8 of 13

Constructors

8

  • The constructor with nothing placed in the parenthesis may set certain attributes to a default value.

  • creates a world with a default size
  • this size is 640 by 480
  • the Turtle class needs something in the parenthesis!
  • will not work otherwise….

2.3 Creating and Storing Objects

9 of 13

Arguments

An argument is a value that is passed into a method or constructor.

9

  • An argument passed into a constructor allows us to choose the values of particular attributes for that object...

  • This can always be changed later...

Animal tiger = new Animal(“orange”);

//create animal with orange fur

2.3 Creating and Storing Objects

10 of 13

Arguments

10

  • A constructor with values placed make it easier to construct objects with particular attributes.

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

11 of 13

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

12 of 13

Activity Cont. - Java Zoo

  • Create three different constructors for your animal that you made earlier
    • label what should go in the parenthesis with the variables you used for your attributes

12

2.3 Creating and Storing Objects

13 of 13

Class Diagram

  • Constructors also have a place in the class diagram.

13

Turtle

Turtle(World w)

Turtle(World w, int xpos, int ypos)

  • String name
  • Color bodyColor
  • int width
  • int height
  • int xpos
  • int ypos
  • forward()
  • backward()
  • turnLeft()
  • turnRight()
  • penUp()
  • penDown()

Class Name

Constructors

Attributes

Methods

2.3 Creating and Storing Objects