Gol en Eclipse

download Gol en Eclipse

of 28

Transcript of Gol en Eclipse

  • 7/31/2019 Gol en Eclipse

    1/28

    EclipseUML by examples 1

    EclipseUML by examplesEclipseUML by examplesUniversit degli Studi di Bologna

    Facolt di Scienze MM. FF. NN.

    Corso di Laurea Triennale in Scienze di Internet

    Anno Accademico 2004-2005

    Laboratorio di Sistemi e Processi Organizzativi

  • 7/31/2019 Gol en Eclipse

    2/28

    EclipseUML by examples 2

    Module Road MapModule Road Map

    1. How to Install

    2. Class Diagram Example

    3. The Game of Life

  • 7/31/2019 Gol en Eclipse

    3/28

    EclipseUML by examples 3

    Module Road MapModule Road Map

    1. Overview and how to install2. Class Diagram Example

    3. The Game of Life

  • 7/31/2019 Gol en Eclipse

    4/28

    EclipseUML by examples 4

    What is Omondo EclipseUML?What is Omondo EclipseUML?

    Omondo EclipseUML is a visual modeling toolnatively integrated with Eclipse

    http://www.eclipseuml.com

    one of the most successful Eclipse plug-ins with more than 250,000downloads in the last 12 months

    the leading UML Eclipse plugin

    Omondo offers two different tools:

    EclipseUML Free Edition

    which is a UML specialized tool;EclipseUML Enterprise Edition

    which provides data modeling, UML, J2ee, and business processmodeling,

    time limited trial

  • 7/31/2019 Gol en Eclipse

    5/28

    EclipseUML by examples 5

    How to install EclipseHow to install Eclipse

    http://www.eclipseuml.com/download/free/index.jspdownload Eclipse 2.1.1 in accordance with your OS

    eclipse-SDK-2.1.1-win32.zip or

    eclipse-SDK-2.1.1-linux-gtk.zip

    download EclipseUML latest version

    eclipseuml-installer_1.2.1.20031103.jar

    according to your OS, unzip eclipse-SDK into

    C:\eclipse

    \usr\local\eclipse

    run

    java -jar eclipseuml-installer_1.2.1.20031103.jar

    specifying the right installation directory

  • 7/31/2019 Gol en Eclipse

    6/28

    EclipseUML by examples 6

    Module Road MapModule Road Map

    1. Overview and how to install

    2. Class Diagram Example3. The Game of Life

  • 7/31/2019 Gol en Eclipse

    7/28

    EclipseUML by examples 7

    Class Diagram ExampleClass Diagram Example

    The following path from the eclipse directoryeclipse\plugins\com.omondo.uml.doc.user_1.2.1\doc\diagrams\classDia

    gram\ClassDiagramExample\ClassDiagramExample.html

    shows a class diagram creation.

    This is a good point to start.

  • 7/31/2019 Gol en Eclipse

    8/28

    EclipseUML by examples 8

    Class Diagram ExampleClass Diagram Example

  • 7/31/2019 Gol en Eclipse

    9/28

    EclipseUML by examples 9

    Module Road MapModule Road Map

    1. Overview and how to install2. Class Diagram Example

    3. The Game of Life

  • 7/31/2019 Gol en Eclipse

    10/28

    EclipseUML by examples 10

    ReferencesReferences

    The following slides are extracted from acourseware which is part of the Open Source

    ECESIS project

    The courseware is produced by, and is copyrighted byEspirity, Inc. and CMA

    To get the whole course material go to

    http://www.eclipse.org/ecesis/

  • 7/31/2019 Gol en Eclipse

    11/28

    EclipseUML by examples 11

    Rules of the Game ofRules of the Game of LifeLife

    Life is played on a grid of square cells (like a chess board)but extending infinitely in every direction.

    A cell can be liveor dead.

    A live cell is shown by putting a marker on its square.

    A dead cell is shown by leaving the square empty.

    Each cell in the grid has a neighborhood consisting of the

    eight cells in every direction including diagonals.

    To apply one step of the rules, we count the number of live

    neighbors for each cell. What happens next depends on this

    number.

  • 7/31/2019 Gol en Eclipse

    12/28

    EclipseUML by examples 12

    Rules of the Game of LifeRules of the Game of Life

    A dead cell with exactly three live neighbors becomes a live

    cell (birth).

    A live cell with two or three live neighbors stays alive

    (survival).

    In all other cases, a cell dies or remains dead (death).

  • 7/31/2019 Gol en Eclipse

    13/28

    EclipseUML by examples 13

    What objects can we identify?What objects can we identify?

    The basic classes in the system may beBoard

    Game

    RuleCreate 3 classes

  • 7/31/2019 Gol en Eclipse

    14/28

    EclipseUML by examples 14

    Board.javaBoard.java

    Edit Board.java constructorsand toString:

    public Board() {

    this.cells = newint[10][10];

    }

    public Board(int[][] cells) {

    this.cells = cells;

    }

    public String toString() {

    return"Board for game of life";

    }

  • 7/31/2019 Gol en Eclipse

    15/28

    EclipseUML by examples 15

    Game.java and Rule.javaGame.java and Rule.java

    Edit Game.java construtorspublic Game(Board aBoard) {

    this.board = aBoard;

    this.generation = 1;

    }

    public Game() {

    setBoard(new Board());

    }

    Edit Rule.java construtorpublic Rule(String name) {

    this.name = name;

    }

  • 7/31/2019 Gol en Eclipse

    16/28

    EclipseUML by examples 16

    Use scrapbook pagesUse scrapbook pages

    Inspect with scrapbook the following code:

    int[][] boardCells = {

    {0, 0, 0, 0, 0, 0, 0 , 0, 0, 1},

    {0, 0, 0, 0, 0, 0, 0 , 0, 0, 1},

    {0, 0, 0, 0, 0, 0, 0 , 0, 0, 1},

    {0, 0, 0, 0, 0, 0, 0 , 0, 0, 1},

    {0, 0, 0, 0, 0, 0, 0 , 0, 0, 1},

    {0, 0, 0, 0, 0, 0, 0 , 0, 0, 1},

    {0, 0, 0, 0, 0, 0, 0 , 0, 0, 1},

    {0, 0, 0, 0, 0, 0, 0 , 0, 0, 1},

    {0, 0, 0, 0, 0, 0, 0 , 0, 0, 1},{0, 0, 0, 0, 0, 0, 0 , 0, 0, 1}};

    Board board = new Board(boardCells);

    Game game = new Game(board);

    game

  • 7/31/2019 Gol en Eclipse

    17/28

    EclipseUML by examples 17

    Singleton patternSingleton pattern

    How many instances of the Game class will we need at thesame time in one single application?

    Just one.

    Define a private static field called theInstance in the Game

    class.

    Lazy initialization:

    publicstatic Game getIstance() {

    if(theInstance == null)

    theInstance = new Game();

    return theInstance;

    }

  • 7/31/2019 Gol en Eclipse

    18/28

    EclipseUML by examples 18

    Edit toStringEdit toString

    Edit toString method in Board class:public String toString(){

    StringBuffer buffer = new StringBuffer();

    for (int i = 0; i < getCells().length; i++){

    buffer.append("\n");

    for (int j = 0; j < getCells()[0].length; j++){

    if (getCells()[i][j] == 0)

    buffer.append(0");

    else

    buffer.append(1");

    }

    }

    return buffer.toString();

    }

  • 7/31/2019 Gol en Eclipse

    19/28

    EclipseUML by examples 19

    Use scrapbook pagesUse scrapbook pages

    Execute with scrapbook the following code:int[][] boardCells = {

    {0, 0, 0, 1, 0, 0, 0 , 0, 0, 1},

    {0, 0, 0, 1, 0, 0, 0 , 0, 0, 1},

    {0, 0, 1, 1, 1, 0, 0 , 0, 0, 1},

    {0, 0, 1, 1, 1, 0, 0 , 0, 0, 1},{0, 0, 1, 1, 1, 0, 0 , 0, 0, 1},

    {0, 0, 0, 1, 0, 0, 0 , 0, 0, 1},

    {0, 0, 0, 0, 0, 0, 0 , 0, 0, 1},

    {0, 0, 0, 1, 0, 0, 0 , 0, 0, 1},

    {0, 0, 1, 1, 1, 0, 0 , 0, 0, 1},

    {0, 0, 1, 1, 1, 0, 0 , 0, 0, 1}};

    Board board = new Board(boardCells);

    System.out.println(board)

  • 7/31/2019 Gol en Eclipse

    20/28

    EclipseUML by examples 20

    Board class RefactoringBoard class Refactoring

    Change the Board class name toTwoDimensionalBoard

    Refactor->Rename

    Create an abstract class Board withabstract evolve methods

    publicabstractvoid evolve

    (Game);

    publicabstractvoid evolve

    (Game, int);

  • 7/31/2019 Gol en Eclipse

    21/28

    EclipseUML by examples 21

    Rule class RefactoringRule class Refactoring

    Change the Rule class definition to abstractCreate the following classes:

  • 7/31/2019 Gol en Eclipse

    22/28

    EclipseUML by examples 22

    Create InterfaceCreate Interface

    Create new interface TwoDimensionalRule with two methods:

    doesApply()

    getCellValue()

    it should simply return int value,

    for birth and survival rules it would be 1,

    for death rule it would be 0.

  • 7/31/2019 Gol en Eclipse

    23/28

    EclipseUML by examples 23

    Create InterfaceCreate Interface

  • 7/31/2019 Gol en Eclipse

    24/28

    EclipseUML by examples 24

    Import rulesImport rules

    Import the 3

    provided rule

    classes.

  • 7/31/2019 Gol en Eclipse

    25/28

    EclipseUML by examples 25

    Redesign the Game classRedesign the Game class

    Make the Game class previously defined abstract.

    Create TwoDimensionalGame class that inherits from Game class.

    Default constructor in the Game class should initialize rules:public Game() {

    this.rules = new Rule[3];

    this.generation = 1;}

    Move singleton implementation to TwoDimensionalGame and let thefollowing be the constructor of the class:

    private TwoDimensionalGame(){

    setBoard(new TwoDimensionalBoard());

    getRules()[0] = new TwoDimensionalBirthRule("Birth Rule");

    getRules()[1] = new TwoDimensionalSurvivalRule("Survivel Rule");

    getRules()[2] = new TwoDimensionalDeathRule("Death Rule");

    }

  • 7/31/2019 Gol en Eclipse

    26/28

    EclipseUML by examples 26

    The evolve methodThe evolve method

    Implement evolve method in the TwoDimensionalBoard class.publicvoid evolve(Game aGame) {

    boolean doesAnyApply = false;

    TwoDimensionalRule rule = null;

    for (int i = 0; i < cells.length; i++){

    for (int j = 0; j < cells[0].length; j++){

    doesAnyApply = false;

    for (int k = 0; k < aGame.getRules().length; k++){

    rule = (TwoDimensionalRule)aGame.getRules()[k];

    if(rule.doesApply(getCells(), i, j)){

    getCells()[i][j] = rule.getCellValue();

    doesAnyApply = true;

    break;

    }

    }

    if (!(doesAnyApply))

    System.out.println("No rules apply for cell[" + i + "][" + j + "]");

    }

    }

    }

  • 7/31/2019 Gol en Eclipse

    27/28

    EclipseUML by examples 27

    Evolve and run methodsEvolve and run methods

    Implement a second evolve method which evolves board as many

    times as passed index:

    publicvoid evolve(Game aGame, int index){

    if (index == 0) return;

    for (int i = 1; i

  • 7/31/2019 Gol en Eclipse

    28/28

    EclipseUML by examples 28

    Test the gameTest the game

    Create a new package and a new class with main method lioke the

    following:publicstaticvoid main(String[] args) {

    int[][] boardCells = {

    {0, 0, 0, 1, 0, 0, 1 , 0, 1, 0},

    {0, 0, 1, 0, 1, 0, 1 , 0, 0, 0},

    {1, 0, 0, 1, 0, 0, 1 , 0, 0, 0},

    {0, 0, 0, 1, 0, 0, 1 , 0, 0, 0},

    {0, 0, 1, 0, 1, 0, 1 , 1 ,0, 0},

    {0, 0, 0, 1, 0, 0, 1 , 1, 1, 0},

    {0, 0, 0, 1, 0, 0, 1 , 1, 0, 1},

    {1, 0, 1, 0, 1, 0, 1 , 0, 1, 0},

    {0, 0, 1, 0, 1, 0, 1 , 0, 1, 1},

    {0, 0, 0, 1, 0, 0, 1 , 0, 0, 1}};

    TwoDimensionalBoard board = new TwoDimensionalBoard(boardCells);TwoDimensionalGame game = TwoDimensionalGame.getInstance();

    game.setBoard(board);

    game.setGeneration(10);

    game.run();

    }