0% found this document useful (0 votes)
66 views42 pages

03S Objects Interaction II

The document discusses objects interaction in object-oriented programming. It covers constructors, their types (default and explicit), overloading and reusing constructors. It also discusses the classes needed for a tic-tac-toe game organized in three layers: data layer with Board and Player classes, business logic layer with GameEngine, TurnController and MovementHandler classes, and UI layer. The UML class diagram shows the relationships between these classes across the three layers.

Uploaded by

rosalba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views42 pages

03S Objects Interaction II

The document discusses objects interaction in object-oriented programming. It covers constructors, their types (default and explicit), overloading and reusing constructors. It also discusses the classes needed for a tic-tac-toe game organized in three layers: data layer with Board and Player classes, business logic layer with GameEngine, TurnController and MovementHandler classes, and UI layer. The UML class diagram shows the relationships between these classes across the three layers.

Uploaded by

rosalba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Objects Interaction II

Sebastian Alejandro Velasco


Object Oriented Programming

Objects Interaction
Agenda

Objects Interaction
Constructors

Objects Interaction
Do you remember how to instantiate?

Objects Interaction
Constructors

This is the Student


class constructor

Invoking a constructor
serves as a request to the
JVM to construct
(instantiate) a brand-new
object

Objects Interaction
Constructors

Constructors are special type of procedures which


are responsible to ask the JVM to inflate a new
helium balloon

JVM

Objects Interaction
Default constructor

Objects Interaction
Default constructor

Default constructors setting all attributes


to their zero-equivalent default values

Objects Interaction
Explicit constructors

We use explicit constructors if we wish to do


something more “interesting” to initialize an object
when it is first instantiated

Objects Interaction
Explicit constructors rules

Constructor’s name must be exactly the same as


the name of the class for which we’re writing the
constructor

Constructors works like another method, can


define a list of parameters

We cannot specify a return type for a constructor; by


definition, a constructor returns a reference to a newly
created object of the class type

Objects Interaction
Passing parameters to constructors

Test Class

Class definition

Objects Interaction
Be careful

If there is at least one constructor defined we


cannot use the default constructor

Objects Interaction
Replacing the Default Parameterless Constructor

Objects Interaction
Overloading Constructors

It is possible to overload Constructors like any other method

Constructor 1 signature

Student ( )

Constructor 2 signature

Student ( String )

Constructor 3 signature

Student ( String , int )

Objects Interaction
Constructors reuse

Code duplication

Objects Interaction
Constructors reuse

It is possible to reuse Constructors using the keyword this

Reusing constructors can avoid duplication of code

Objects Interaction

Review: Tic Tac Toe
Layers and packages
Constructors and access modificators
Relation between objects
Data Layer

Which classes
belong to the data
layer?

Objects Interaction
Data layer

Objects Interaction
Board class

Overriding default
constructor

Objects Interaction
Printing user defined objects method

Objects Interaction
toString method

toString method allow us override the default


way how the object is printed

Important, do not
forget it

The return type is


String

Objects Interaction
Overriding toString method

Objects Interaction
Overloading
constructors

Objects Interaction
Overriding toString method

String.valueOf( ) is used to cast


variables to String

Objects Interaction
Player class

Objects Interaction
Overriding toString method

Objects Interaction
How do you know if two user defined objects are
equal?

Objects Interaction
Overriding equals method

equals method allow us to override the default way how two user
defined objects are equals or not

Important, do not
forget it

If the two objects


have the same
player (Symbol)
are equals

Objects Interaction
Business logic Layer

Which classes
belong to the
business logic
layer?

Objects Interaction
Business logic layer

Objects Interaction
GameEngine Class

This is the game starting point

Methods can be
private

Objects Interaction
GameEngine Class

Class Method signature Function


startGame ( ) Initialize objects and variables
Call the method play
play ( ) Iterate until win or finish
GameEngine condition is reached
getCurrentPlayer ( Player ) Change the current player at
the end of each turn

Objects Interaction
TurnController Class

Objects Interaction
TurnController Class

Class Method signature Function


playTurn ( Board , Player ) Handle movement turn
Call movement validator
Call board modifier
markBoard ( Board , int , Modify board after a valid play
TurnController char )
existFreeSquares ( Board , Chek if there are available
Player , Player) squares to play
generateRobotMovement ( ) Generate random robot
movement

Objects Interaction
MovementHandler Class

Objects Interaction
MovementHandler Class

Class Method signature Function


isValid ( Board , int ) Check if a square selection is
available to be marked
MovementHandle isWinningMovement Check if the last movement
r ( Board , Player ) causes the player's victory

Objects Interaction
UML Class diagram

Objects Interaction
UML Class diagram

DATA
BUSINESS LAYER
LAYER

UI
LAYER

Objects Interaction
Each layers is represented as a package

• Three layers
– Data
– Business logic
– UI

Objects Interaction
Tic Tac Toe

Check the code here

Objects Interaction
References

• [Barker] J. Barker, Beginning Java Objects: From Concepts To Code,


Second Edition, Apress, 2005. 

Objects Interaction

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy