Lecture 4 Class

Embed Size (px)

Citation preview

  • 8/6/2019 Lecture 4 Class

    1/39

    SOFTENG251 Lecture 4: Making Classes palette 1 / 31

    SOFTENG 251:

    Object-Oriented Software Construction

    Lecture 4: Making Classes

    Ewan TemperoDepartment of Computer Science

  • 8/6/2019 Lecture 4 Class

    2/39

    Potential Assessment Question

    PAQ

    Agenda

    Review

    Classes

    Classes

    ASCIIBoard

    Status check

    Fields

    ASCIIBoard fields

    Java Conventions

    Using fields

    Initialising fields (1)

    Status Check

    No initialisation

    Interpreting .

    Deck class

    Refactoring

    Making Classes

    FreeCell

    Card: A first (static) go

    Using static Card

    Card: A second (static) go

    Using static Card

    Review

    SOFTENG251 Lecture 4: Making Classes palette 2 / 31

    Which of the following statements is most correct with respect to the

    following Java method?

    p r i v a t e void method ( ) {char a , b , c ;

    a = a ;

    b = c ;

    c = b ;

    System . o u t . p r i n t l n ( + b ) ;

    }

    (a) The method will compile and when executed print the character b.

    (b) The method will compile but will fail with an error when executed.

    (c) The method will not compile due not putting void as the parameter.

    (d) The method will not compile due to some other reason.

    Justify your answer.

    http://void/http://void/
  • 8/6/2019 Lecture 4 Class

    3/39

    Agenda

    PAQ

    Agenda

    Review

    Classes

    Classes

    ASCIIBoard

    Status check

    Fields

    ASCIIBoard fields

    Java Conventions

    Using fields

    Initialising fields (1)

    Status Check

    No initialisation

    Interpreting .

    Deck class

    Refactoring

    Making Classes

    FreeCell

    Card: A first (static) go

    Using static Card

    Card: A second (static) go

    Using static Card

    Review

    SOFTENG251 Lecture 4: Making Classes palette 3 / 31

    Admin

    Quiz 1

    What is a class

    Classes in Java Fields

    Reading:

    Lesson: Language Basics (finish)

    Lesson: Classes and Objects (first 3 sections) Text: Chapter 5

  • 8/6/2019 Lecture 4 Class

    4/39

    Previously in SOFTENG 251

    PAQ

    Agenda

    Review

    Classes

    Classes

    ASCIIBoard

    Status check

    Fields

    ASCIIBoard fields

    Java Conventions

    Using fields

    Initialising fields (1)

    Status Check

    No initialisation

    Interpreting .

    Deck class

    Refactoring

    Making Classes

    FreeCell

    Card: A first (static) go

    Using static Card

    Card: A second (static) go

    Using static Card

    Review

    SOFTENG251 Lecture 4: Making Classes palette 4 / 31

    public static void main(String[] args) {System.out.println("FreeCell started.");

    ....

    placeCardsOnBoard(board, deck);....

    displayBoard(board, foundations, cells);

    }

    /**

    * Produce a string that provides an ASCII representation

    * of the specified card. The card is an integer in the

    * ...*/

    private static String getCardASCIIDisplay(int card) {....

    }

  • 8/6/2019 Lecture 4 Class

    5/39

    What is a class?

    PAQ

    Agenda

    Review

    Classes

    Classes

    ASCIIBoard

    Status check

    Fields

    ASCIIBoard fields

    Java Conventions

    Using fields

    Initialising fields (1)

    Status Check

    No initialisation

    Interpreting .

    Deck class

    Refactoring

    Making Classes

    FreeCell

    Card: A first (static) go

    Using static Card

    Card: A second (static) go

    Using static Card

    Review

    SOFTENG251 Lecture 4: Making Classes palette 5 / 31

    A group of methods (and fields) identified by a name

    A unit of encapsulation

    Can be used to provide abstraction

    Can provide objects

    Can be used to for checking for errors

  • 8/6/2019 Lecture 4 Class

    6/39

    Where do Classes come from? (1)

    PAQ

    Agenda

    Review

    Classes

    Classes

    ASCIIBoard

    Status check

    Fields

    ASCIIBoard fields

    Java Conventions

    Using fields

    Initialising fields (1)

    Status Check

    No initialisation

    Interpreting .

    Deck class

    Refactoring

    Making Classes

    FreeCell

    Card: A first (static) go

    Using static Card

    Card: A second (static) go

    Using static Card

    Review

    SOFTENG251 Lecture 4: Making Classes palette 6 / 31

    A good class consists of methods (and fields) that belong together or

    do related things

    A good class provides a single coherent concept (an abstraction)

    A good class is generally easier to understand and easier to change

    (because everything in the class is related to a single concept) One attribute of goodness of class is cohesion

    find related things and put them in a class

  • 8/6/2019 Lecture 4 Class

    7/39

    Which of these things belong together?

    PAQ

    Agenda

    Review

    Classes

    Classes

    ASCIIBoard

    Status check

    Fields

    ASCIIBoard fields

    Java Conventions

    Using fields

    Initialising fields (1)

    Status Check

    No initialisation

    Interpreting .

    Deck class

    Refactoring

    Making Classes

    FreeCell

    Card: A first (static) go

    Using static Card

    Card: A second (static) go

    Using static Card

    Review

    SOFTENG251 Lecture 4: Making Classes palette 7 / 31

    public static void main(String[] args) {System.out.println("FreeCell started.");

    int[] deck = createDeck();shuffleDeck(deck);

    // Board consists of 8 cascades of maximum length 52

    int[][] board = new int[52][8];

    placeCardsOnBoard(board, deck);

    int[] foundations = new int[4]; // Stores the top card

    int[] cells = new int[4];

    // Print the board, including foundations and cellsdisplayBoard(board, foundations, cells);

    }

  • 8/6/2019 Lecture 4 Class

    8/39

    The Board Class

    PAQ

    Agenda

    Review

    Classes

    Classes

    ASCIIBoard

    Status check

    Fields

    ASCIIBoard fields

    Java Conventions

    Using fields

    Initialising fields (1)

    Status Check

    No initialisation

    Interpreting .

    Deck class

    Refactoring

    Making Classes

    FreeCell

    Card: A first (static) go

    Using static Card

    Card: A second (static) go

    Using static Card

    Review

    SOFTENG251 Lecture 4: Making Classes palette 8 / 31

    package se251.freecell.v4;

    public class ASCIIBoard {

    }

  • 8/6/2019 Lecture 4 Class

    9/39

    The Board Class

    PAQ

    Agenda

    Review

    Classes

    Classes

    ASCIIBoard

    Status check

    Fields

    ASCIIBoard fields

    Java Conventions

    Using fields

    Initialising fields (1)

    Status Check

    No initialisation

    Interpreting .

    Deck class

    Refactoring

    Making Classes

    FreeCell

    Card: A first (static) go

    Using static Card

    Card: A second (static) go

    Using static Card

    Review

    SOFTENG251 Lecture 4: Making Classes palette 8 / 31

    package se251.freecell.v4;

    public class ASCIIBoard {

    public static void placeCardsOnBoard(int[][] board,int[] deck) {

    .....

    }public static void displayBoard(int[][] board,

    int[] foundations,int[] cells) {

    .....

    }

    }

  • 8/6/2019 Lecture 4 Class

    10/39

    The Board Class

    PAQ

    Agenda

    Review

    Classes

    Classes

    ASCIIBoard

    Status check

    Fields

    ASCIIBoard fields

    Java Conventions

    Using fields

    Initialising fields (1)

    Status Check

    No initialisation

    Interpreting .

    Deck class

    Refactoring

    Making Classes

    FreeCell

    Card: A first (static) go

    Using static Card

    Card: A second (static) go

    Using static Card

    Review

    SOFTENG251 Lecture 4: Making Classes palette 8 / 31

    package se251.freecell.v4;

    public class ASCIIBoard {

    public static void placeCardsOnBoard(int[][] board,int[] deck) {

    .....

    }public static void displayBoard(int[][] board,

    int[] foundations,int[] cells) {

    .....

    }

    }

  • 8/6/2019 Lecture 4 Class

    11/39

    The Board Class

    PAQ

    Agenda

    Review

    Classes

    Classes

    ASCIIBoard

    Status check

    Fields

    ASCIIBoard fields

    Java Conventions

    Using fields

    Initialising fields (1)

    Status Check

    No initialisation

    Interpreting .

    Deck class

    Refactoring

    Making Classes

    FreeCell

    Card: A first (static) go

    Using static Card

    Card: A second (static) go

    Using static Card

    Review

    SOFTENG251 Lecture 4: Making Classes palette 8 / 31

    package se251.freecell.v4;

    public class ASCIIBoard {

    public static void placeCardsOnBoard(int[][] board,int[] deck) {

    .....

    }public static void displayBoard(int[][] board,

    int[] foundations,int[] cells) {

    .....

    System.out.print(FreeCell.getCardASCIIDisplay(card));

    .....

    }}

  • 8/6/2019 Lecture 4 Class

    12/39

    Alternate Design

    PAQ

    Agenda

    Review

    Classes

    Classes

    ASCIIBoard

    Status check

    Fields

    ASCIIBoard fields

    Java Conventions

    Using fields

    Initialising fields (1)

    Status Check

    No initialisation

    Interpreting .

    Deck class

    Refactoring

    Making Classes

    FreeCell

    Card: A first (static) go

    Using static Card

    Card: A second (static) go

    Using static Card

    Review

    SOFTENG251 Lecture 4: Making Classes palette 9 / 31

    public class ASCIIBoard {public static void placeCardsOnBoard(int[][] board,

    int[] deck) {...}

    public static void displayBoard(int[][] board,int[] foundations,

    int[] cells) {...}

    private static String getCardASCIIDisplay(int card){...}

    }

  • 8/6/2019 Lecture 4 Class

    13/39

    Alternate Design

    PAQ

    Agenda

    Review

    Classes

    Classes

    ASCIIBoard

    Status check

    Fields

    ASCIIBoard fields

    Java Conventions

    Using fields

    Initialising fields (1)

    Status Check

    No initialisation

    Interpreting .

    Deck class

    Refactoring

    Making Classes

    FreeCell

    Card: A first (static) go

    Using static Card

    Card: A second (static) go

    Using static Card

    Review

    SOFTENG251 Lecture 4: Making Classes palette 9 / 31

    public class ASCIIBoard {public static void placeCardsOnBoard(int[][] board,

    int[] deck) {...}public static void displayBoard(int[][] board,

    int[] foundations,

    int[] cells) {...}

    private static String getCardASCIIDisplay(int card){...}

    private static int getRank(int card) { ... }private static int getSuit(int card) { ... }

    }

  • 8/6/2019 Lecture 4 Class

    14/39

    Alternate Design

    PAQ

    Agenda

    Review

    Classes

    Classes

    ASCIIBoard

    Status check

    Fields

    ASCIIBoard fields

    Java Conventions

    Using fields

    Initialising fields (1)

    Status Check

    No initialisation

    Interpreting .

    Deck class

    Refactoring

    Making Classes

    FreeCell

    Card: A first (static) go

    Using static Card

    Card: A second (static) go Using static Card

    Review

    SOFTENG251 Lecture 4: Making Classes palette 9 / 31

    public class ASCIIBoard {public static void placeCardsOnBoard(int[][] board,

    int[] deck) {...}public static void displayBoard(int[][] board,

    int[] foundations,

    int[] cells) {...}

    private static String getCardASCIIDisplay(int card){...}

    private static int getRank(int card) { ... }private static int getSuit(int card) { ... }

    }

  • 8/6/2019 Lecture 4 Class

    15/39

    Status check

    PAQ

    Agenda

    Review

    Classes

    Classes

    ASCIIBoard

    Status check

    Fields

    ASCIIBoard fields

    Java Conventions

    Using fields

    Initialising fields (1)

    Status Check

    No initialisation

    Interpreting .

    Deck class

    Refactoring

    Making Classes

    FreeCell

    Card: A first (static) go

    Using static Card

    Card: A second (static) go Using static Card

    Review

    SOFTENG251 Lecture 4: Making Classes palette 10 / 31

    package se251.freecell.v4;

    public class FreeCell {.....

    public static void main(String[] args) {System.out.println("FreeCell started.");

    int[] deck = createDeck();

    shuffleDeck(deck);

    // Board consists of 8 cascades of maximum length 52

    int[][] board = new int[52][8];ASCIIBoard.placeCardsOnBoard(board, deck);

    int[] foundations = new int[4]; // Stores the top card

    int[] cells = new int[4];

    // Print the board, including foundations and cells

    ASCIIBoard.displayBoard(board, foundations, cells);

    System.out.println("FreeCell stopped.");

    }}

  • 8/6/2019 Lecture 4 Class

    16/39

    Fields

    PAQ

    Agenda

    Review

    Classes

    Classes

    ASCIIBoard

    Status check

    Fields

    ASCIIBoard fields

    Java Conventions

    Using fields

    Initialising fields (1)

    Status Check

    No initialisation

    Interpreting .

    Deck class

    Refactoring

    Making Classes

    FreeCell

    Card: A first (static) go

    Using static Card

    Card: A second (static) go Using static Card

    Review

    SOFTENG251 Lecture 4: Making Classes palette 11 / 31

    Fields are variables that are accessible from code anywhere in a class

    Fields are used to store state information that is available in multiple

    places (e.g., different methods) and across time (e.g., different method

    invocations)

    Like methods, fields can be used with respect to a class (static) orwith respect to an object (instance variables)

    Fields may be public or private (and other visibilities)

  • 8/6/2019 Lecture 4 Class

    17/39

    Fields

    PAQ

    Agenda

    Review

    Classes

    Classes

    ASCIIBoard

    Status check

    Fields

    ASCIIBoard fields

    Java Conventions

    Using fields

    Initialising fields (1)

    Status Check

    No initialisation

    Interpreting .

    Deck class

    Refactoring

    Making Classes

    FreeCell

    Card: A first (static) go

    Using static Card

    Card: A second (static) go Using static Card

    Review

    SOFTENG251 Lecture 4: Making Classes palette 11 / 31

    Fields are variables that are accessible from code anywhere in a class

    Fields are used to store state information that is available in multiple

    places (e.g., different methods) and across time (e.g., different method

    invocations)

    Like methods, fields can be used with respect to a class (static) orwith respect to an object (instance variables)

    Fields may be public or private (and other visibilities)

    public fields are almost always a Bad Idea

  • 8/6/2019 Lecture 4 Class

    18/39

    Adding fields to ASCIIBoard

    PAQ

    Agenda

    Review

    Classes

    Classes

    ASCIIBoard

    Status check

    Fields

    ASCIIBoard fields

    Java Conventions

    Using fields

    Initialising fields (1)

    Status Check

    No initialisation

    Interpreting .

    Deck class

    Refactoring

    Making Classes

    FreeCell

    Card: A first (static) go

    Using static Card

    Card: A second (static) go Using static Card

    Review

    SOFTENG251 Lecture 4: Making Classes palette 12 / 31

    package se251.freecell.v4;

    public class ASCIIBoard {public static void placeCardsOnBoard(int[][] board,

    int[] deck) { ... }public static void displayBoard(int[][] board,

    int[] foundations,

    int[] cells) { ... }}

    package se251.freecell.v5;

    public class ASCIIBoard {// Board consists of 8 cascades of maximum length 52

    private static int[][] board;

    private static int[] foundations; // Stores the top card

    private static int[] cells;

    public static void initialiseBoard() { ... }public static void placeCardsOnBoard(int[] deck) { ... }public static void displayBoard() { ... }

    }

  • 8/6/2019 Lecture 4 Class

    19/39

    More Java conventions

    PAQ

    Agenda

    Review

    Classes

    Classes

    ASCIIBoard

    Status check

    Fields

    ASCIIBoard fields

    Java Conventions

    Using fields

    Initialising fields (1)

    Status Check

    No initialisation

    Interpreting .

    Deck class

    Refactoring

    Making Classes

    FreeCell

    Card: A first (static) go

    Using static Card

    Card: A second (static) go Using static Card

    Review

    SOFTENG251 Lecture 4: Making Classes palette 13 / 31

    Put all field declarations at the top of the class declaration

    Name (private) fields with leading underscore, E.g., board

    This is not a Sun standard

    http://java.sun.com/docs/codeconv

    http://java.sun.com/docs/codeconvhttp://java.sun.com/docs/codeconv
  • 8/6/2019 Lecture 4 Class

    20/39

    Using fields

    PAQ

    Agenda

    Review

    Classes

    Classes

    ASCIIBoard

    Status check

    Fields

    ASCIIBoard fields

    Java Conventions

    Using fields

    Initialising fields (1)

    Status Check

    No initialisation

    Interpreting .

    Deck class

    Refactoring

    Making Classes

    FreeCell

    Card: A first (static) go

    Using static Card

    Card: A second (static) go Using static Card

    Review

    SOFTENG251 Lecture 4: Making Classes palette 14 / 31

    public static void placeCardsOnBoard(int[] deck) {// Place cards on board

    int row = 0;

    int cascade = 0;

    for (int card = 0; card < deck.length; card++) {if (cascade == board[row].length) {

    cascade = 0;

    row++;

    }

    // Dont need to test row because board is big enoughboard[row][cascade] = deck[card];

    cascade++;

    }}

    Fields are declared outside methods, but are accessible from inside all

    methods.

    Fields retain their values between method calls

  • 8/6/2019 Lecture 4 Class

    21/39

    Initialising fields (1)

    PAQ

    Agenda

    Review

    Classes

    Classes

    ASCIIBoard

    Status check

    Fields

    ASCIIBoard fields

    Java Conventions

    Using fields

    Initialising fields (1)

    Status Check

    No initialisation

    Interpreting .

    Deck class

    Refactoring

    Making Classes

    FreeCell

    Card: A first (static) go

    Using static Card

    Card: A second (static) go Using static Card

    Review

    SOFTENG251 Lecture 4: Making Classes palette 15 / 31

    public static void initialiseBoard() {board = new int[52][8];

    foundations = new int[4];

    cells = new int[4];

    }

    It doesnt make sense to initialise fields in each method, so typicallythere will be a separate method to do this

  • 8/6/2019 Lecture 4 Class

    22/39

    Status Check

    PAQ

    Agenda

    Review

    Classes

    Classes

    ASCIIBoard

    Status check

    Fields

    ASCIIBoard fields

    Java Conventions

    Using fields

    Initialising fields (1)

    Status Check

    No initialisation

    Interpreting .

    Deck class

    Refactoring

    Making Classes

    FreeCell

    Card: A first (static) go

    Using static Card

    Card: A second (static) go Using static Card

    Review

    SOFTENG251 Lecture 4: Making Classes palette 16 / 31

    public static void main(String[] args) {System.out.println("FreeCell started.");

    int[] deck = createDeck();

    shuffleDeck(deck);

    ASCIIBoard.initialiseBoard();

    ASCIIBoard.placeCardsOnBoard(deck);

    ASCIIBoard.displayBoard();

    System.out.println("FreeCell stopped.");

    }

  • 8/6/2019 Lecture 4 Class

    23/39

    No initialisation

    PAQ

    Agenda

    Review

    Classes

    Classes

    ASCIIBoard

    Status check

    Fields

    ASCIIBoard fields

    Java Conventions

    Using fields

    Initialising fields (1)

    Status Check

    No initialisation

    Interpreting .

    Deck class

    Refactoring

    Making Classes

    FreeCell

    Card: A first (static) go

    Using static Card

    Card: A second (static) go Using static Card

    Review

    SOFTENG251 Lecture 4: Making Classes palette 17 / 31

    se251 prompt> java -cp target/classes se251.freecell.v5.FreeCellFreeCell started.

    Exception in thread "main" java.lang.NullPointerException

    at se251.freecell.v5.ASCIIBoard.\placeCardsOnBoard(ASCIIBoard.java:20)

    at se251.freecell.v5.FreeCell.main(FreeCell.java:118)

  • 8/6/2019 Lecture 4 Class

    24/39

    No initialisation

    PAQ

    Agenda

    Review

    Classes

    Classes

    ASCIIBoard

    Status check

    Fields

    ASCIIBoard fields

    Java Conventions

    Using fields

    Initialising fields (1)

    Status Check

    No initialisation

    Interpreting .

    Deck class

    Refactoring

    Making Classes

    FreeCell

    Card: A first (static) go

    Using static Card

    Card: A second (static) go Using static Card

    Review

    SOFTENG251 Lecture 4: Making Classes palette 17 / 31

    se251 prompt> java -cp target/classes se251.freecell.v5.FreeCellFreeCell started.

    Exception in thread "main" java.lang.NullPointerException

    at se251.freecell.v5.ASCIIBoard.\placeCardsOnBoard(ASCIIBoard.java:20)

    at se251.freecell.v5.FreeCell.main(FreeCell.java:118)

  • 8/6/2019 Lecture 4 Class

    25/39

    No initialisation

    PAQ

    Agenda

    Review

    Classes

    Classes

    ASCIIBoard

    Status check

    Fields

    ASCIIBoard fields

    Java Conventions

    Using fields

    Initialising fields (1)

    Status Check

    No initialisation

    Interpreting .

    Deck class

    Refactoring

    Making Classes

    FreeCell

    Card: A first (static) go

    Using static Card

    Card: A second (static) go Using static Card

    Review

    SOFTENG251 Lecture 4: Making Classes palette 17 / 31

    se251 prompt> java -cp target/classes se251.freecell.v5.FreeCellFreeCell started.

    Exception in thread "main" java.lang.NullPointerException

    at se251.freecell.v5.ASCIIBoard.\placeCardsOnBoard(ASCIIBoard.java:20)

    at se251.freecell.v5.FreeCell.main(FreeCell.java:118)

  • 8/6/2019 Lecture 4 Class

    26/39

    No initialisation

    PAQ

    Agenda

    Review

    Classes

    Classes

    ASCIIBoard

    Status check

    Fields

    ASCIIBoard fields

    Java Conventions

    Using fields

    Initialising fields (1)

    Status Check

    No initialisation

    Interpreting .

    Deck class

    Refactoring

    Making Classes

    FreeCell

    Card: A first (static) go

    Using static Card

    Card: A second (static) go Using static Card

    Review

    SOFTENG251 Lecture 4: Making Classes palette 18 / 31

    se251 prompt> java -cp target/classes se251.freecell.v5.FreeCellFreeCell started.

    Exception in thread "main" java.lang.NullPointerException

    at se251.freecell.v5.ASCIIBoard.\

    placeCardsOnBoard(ASCIIBoard.java:20)

    at se251.freecell.v5.FreeCell.main(FreeCell.java:118)

    15 public static void placeCardsOnBoard(int[] deck) {16 // Place cards on board

    17 int row = 0;18 int cascade = 0;

    19 for (int card = 0; card < deck.length; card++) {20 if (cascade == board[row].length) {21 cascade = 0;

    22 row++;

    23 }24 // Dont need to test row because board is big enough

    25 board[row][cascade] = deck[card];

    26 cascade++;

    27 }

    28 }

  • 8/6/2019 Lecture 4 Class

    27/39

    Interpreting .

    PAQ

    Agenda

    Review

    Classes

    Classes

    ASCIIBoard

    Status check

    Fields

    ASCIIBoard fields

    Java Conventions

    Using fields

    Initialising fields (1)

    Status Check

    No initialisation

    Interpreting .

    Deck class

    Refactoring

    Making Classes

    FreeCell

    Card: A first (static) go

    Using static Card

    Card: A second (static) go Using static Card

    Review

    SOFTENG251 Lecture 4: Making Classes palette 19 / 31

    A decimal place 3.14159 Separating a contained package from a containing package

    se251.freecell

    Separating the package part of a class name from the class name

    se251.freecell.v1.FreeCell Accessing a member of a class:

    deck.length length is a field of the array named deck

    ASCIIBoard.initialiseBoard() initialiseBoard() is

    a method on the class (se251.freecell.v5.)ASCIIBoard System.out.println() out is a field of the class

    (java.lang.)System and println() is a method of out

  • 8/6/2019 Lecture 4 Class

    28/39

    Another Class

    PAQ

    Agenda

    Review

    Classes

    Classes

    ASCIIBoard

    Status check

    Fields

    ASCIIBoard fields

    Java Conventions

    Using fields

    Initialising fields (1)

    Status Check

    No initialisation

    Interpreting . Deck class

    Refactoring

    Making Classes

    FreeCell

    Card: A first (static) go

    Using static Card

    Card: A second (static) go Using static Card

    Review

    SOFTENG251 Lecture 4: Making Classes palette 20 / 31

    package se251.freecell.v6;

    public class Deck {private static int[] deck;

    public static void create() {// Create sorted card deck

    deck = new int[52];

    ....

    }

    public static void shuffle() {...

    }

    public static int[] getCardArray() {return deck;

    }}

  • 8/6/2019 Lecture 4 Class

    29/39

    Using Deck

    PAQ

    Agenda

    Review

    Classes

    Classes

    ASCIIBoard

    Status check

    Fields

    ASCIIBoard fields

    Java Conventions

    Using fields

    Initialising fields (1)

    Status Check

    No initialisation

    Interpreting . Deck class

    Refactoring

    Making Classes

    FreeCell

    Card: A first (static) go

    Using static Card

    Card: A second (static) go Using static Card

    Review

    SOFTENG251 Lecture 4: Making Classes palette 21 / 31

    class ASCIIBoard {...

    public static void placeCardsOnBoard() {int[] cards = Deck.getCardArray();

    // Place cards on board

    int row = 0;int cascade = 0;

    for (int card = 0; card < cards.length; card++) {if (cascade == board[row].length) {

    cascade = 0;

    row++;}// Dont need to test row because board is big enough

    board[row][cascade] = cards[card];

    cascade++;

    }}

  • 8/6/2019 Lecture 4 Class

    30/39

    Refactoring

    PAQ

    Agenda

    Review

    Classes

    Classes

    ASCIIBoard

    Status check

    Fields

    ASCIIBoard fields

    Java Conventions

    Using fields

    Initialising fields (1)

    Status Check

    No initialisation

    Interpreting . Deck class

    Refactoring

    Making Classes

    FreeCell

    Card: A first (static) go

    Using static Card

    Card: A second (static) go Using static Card

    Review

    SOFTENG251 Lecture 4: Making Classes palette 22 / 31

    Changing the design without changing the functionality with the intent of

    reducing future costs

    One example of changing the design is to move code into methods.

    Other examples of changing the design include

    creating/modifying/deleting classes, moving methods or/and fieldsbetween classes

    When functionality changes (e.g. changing presentation of cards) then

    that is not considered refactoring, but better designs makes it easier to

    also improve/add functionality so often happens during refactoring work.

  • 8/6/2019 Lecture 4 Class

    31/39

    Where do Classes come from? (2)

    PAQ

    Agenda

    Review

    Classes

    Classes

    ASCIIBoard

    Status check

    Fields

    ASCIIBoard fields

    Java Conventions

    Using fields

    Initialising fields (1)

    Status Check

    No initialisation

    Interpreting . Deck class

    Refactoring

    Making Classes

    FreeCell

    Card: A first (static) go

    Using static Card

    Card: A second (static) go Using static Card

    Review

    SOFTENG251 Lecture 4: Making Classes palette 23 / 31

    A good class represents a concept Concepts are often described using nouns

    One way to find candidates for classes is to look at the nouns in the

    description of the system

    A way to find candidates for methods of a class is to look at the verbsassociated with the nouns

    A way to find candidates for fields of a class is to look at the properties

    or attributesassociated with the nouns

  • 8/6/2019 Lecture 4 Class

    32/39

    FreeCell

    PAQ

    Agenda

    Review

    Classes

    Classes

    ASCIIBoard

    Status check

    Fields

    ASCIIBoard fields

    Java Conventions

    Using fields

    Initialising fields (1)

    Status Check

    No initialisation

    Interpreting . Deck class

    Refactoring

    Making Classes

    FreeCell

    Card: A first (static) go

    Using static Card

    Card: A second (static) go Using static Card

    Review

    SOFTENG251 Lecture 4: Making Classes palette 24 / 31

    The FreeCell board starts with the 52 cards of a standard

    card deck shuffled and laid out face up in 8 vertical cascades,

    the first 4 cascades with 7 cards and the remaining 4 with 6

    cards. There are also 4 empty cells and 4 empty foundation

    piles, one for each suit. The object of FreeCell is to play all

    cards onto the foundation piles in ascending order of rank.

    Any card at the bottom end of a cascade (called the

    uncovered card) may be moved into an empty cell. An

    uncovered card or card in a cell may be moved onto afoundation pile if it is the next highest card in the suit (starting

    with Ace if the foundation pile is empty). An uncovered card or

    a card in a cell may be moved to the end of any cascade,

    provided the colour of the suits of the two cards are different,

    and the rank of the moved card is one less than the rank of

    the card being covered.

  • 8/6/2019 Lecture 4 Class

    33/39

    FreeCell

    PAQ

    Agenda

    Review

    Classes

    Classes

    ASCIIBoard

    Status check

    Fields

    ASCIIBoard fields

    Java Conventions

    Using fields

    Initialising fields (1)

    Status Check

    No initialisation

    Interpreting . Deck class

    Refactoring

    Making Classes

    FreeCell

    Card: A first (static) go

    Using static Card

    Card: A second (static) go Using static Card

    Review

    SOFTENG251 Lecture 4: Making Classes palette 25 / 31

    The FreeCell board starts with the 52 cards of a standard

    card deck shuffled and laid out face up in 8 vertical cascades,

    the first 4 cascades with 7 cards and the remaining 4 with 6

    cards. There are also 4 empty cells and 4 empty foundation

    piles, one for each suit. The object of FreeCell is to play all

    cards onto the foundation piles in ascending order of rank.

    Any card at the bottom end of a cascade (called the

    uncovered card) may be moved into an empty cell. An

    uncovered card or card in a cell may be moved onto afoundation pile if it is the next highest card in the suit (starting

    with Ace if the foundation pile is empty). An uncovered card or

    a card in a cell may be moved to the end of any cascade,

    provided the colour of the suits of the two cards are different,

    and the rank of the moved card is one less than the rank of

    the card being covered.

  • 8/6/2019 Lecture 4 Class

    34/39

    FreeCell

    PAQ

    Agenda

    Review

    Classes

    Classes

    ASCIIBoard

    Status check

    Fields

    ASCIIBoard fields

    Java Conventions

    Using fields

    Initialising fields (1)

    Status Check

    No initialisation

    Interpreting . Deck class

    Refactoring

    Making Classes

    FreeCell

    Card: A first (static) go

    Using static Card

    Card: A second (static) go Using static Card

    Review

    SOFTENG251 Lecture 4: Making Classes palette 26 / 31

    The FreeCell board starts with the 52 cards of a standard

    card deck shuffled and laid out face up in 8 vertical cascades,

    the first 4 cascades with 7 cards and the remaining 4 with 6

    cards. There are also 4 empty cells and 4 empty foundation

    piles, one for each suit. The object of FreeCell is to play all

    cards onto the foundation piles in ascending order of rank.

    Any card at the bottom end of a cascade (called the

    uncovered card) may be moved into an empty cell. An

    uncovered card or card in a cell may be moved onto afoundation pile if it is the next highest card in the suit (starting

    with Ace if the foundation pile is empty). An uncovered card or

    a card in a cell may be moved to the end of any cascade,

    provided the colour of the suits of the two cards are different,

    and the rank of the moved card is one less than the rank of

    the card being covered.

  • 8/6/2019 Lecture 4 Class

    35/39

    Card: A first (static) go

    PAQ

    Agenda

    Review

    Classes

    Classes

    ASCIIBoard

    Status check

    Fields

    ASCIIBoard fields

    Java Conventions

    Using fields

    Initialising fields (1)

    Status Check

    No initialisation

    Interpreting . Deck class

    Refactoring

    Making Classes

    FreeCell

    Card: A first (static) go

    Using static Card

    Card: A second (static) go Using static Card

    Review

    SOFTENG251 Lecture 4: Making Classes palette 27 / 31

    package se251.freecell.v7;

    public class Card {private static int code;

    public static void setCode(int code) {code = code;}public static String getASCIIDisplay() { ... }private static int getRank() { ... }

    private static int getSuit() { ... }}

  • 8/6/2019 Lecture 4 Class

    36/39

    Using static Card

    PAQ

    Agenda

    Review

    Classes

    Classes

    ASCIIBoard

    Status check

    Fields

    ASCIIBoard fields

    Java Conventions

    Using fields

    Initialising fields (1)

    Status Check

    No initialisation

    Interpreting . Deck class

    Refactoring

    Making Classes

    FreeCell

    Card: A first (static) go

    Using static Card

    Card: A second (static) go Using static Card

    Review

    SOFTENG251 Lecture 4: Making Classes palette 28 / 31

    public static void displayBoard() {...

    while (not finished) {....

    Card.setCode( board[row][cascade]);

    System.out.print(Card.getASCIIDisplay());

    ...

    }

  • 8/6/2019 Lecture 4 Class

    37/39

    Card: A second (static) go

    PAQ

    Agenda

    Review

    Classes

    Classes

    ASCIIBoard

    Status check

    Fields

    ASCIIBoard fields

    Java Conventions

    Using fields

    Initialising fields (1)

    Status Check

    No initialisation

    Interpreting . Deck class

    Refactoring

    Making Classes

    FreeCell

    Card: A first (static) go

    Using static Card

    Card: A second (static) go Using static Card

    Review

    SOFTENG251 Lecture 4: Making Classes palette 29 / 31

    package se251.freecell.v7;

    public class Card {public static String getASCIIDisplay(int card) {

    ...}private static int getRank() { ... }private static int getSuit() { ... }

    }

  • 8/6/2019 Lecture 4 Class

    38/39

    Using static Card

    PAQ

    Agenda

    Review

    Classes

    Classes

    ASCIIBoard

    Status check

    Fields

    ASCIIBoard fields

    Java Conventions

    Using fields

    Initialising fields (1)

    Status Check

    No initialisation

    Interpreting . Deck class

    Refactoring

    Making Classes

    FreeCell

    Card: A first (static) go

    Using static Card

    Card: A second (static) go Using static Card

    Review

    SOFTENG251 Lecture 4: Making Classes palette 30 / 31

    public static void displayBoard() {...

    while (not finished) {...

    int card = board[row][cascade];

    System.out.print(Card.getASCIIDisplay(card));

    ...

    }

  • 8/6/2019 Lecture 4 Class

    39/39

    Review

    PAQ

    Agenda

    Review

    Classes

    Classes

    ASCIIBoard

    Status check

    Fields

    ASCIIBoard fields

    Java Conventions

    Using fields

    Initialising fields (1)

    Status Check

    No initialisation

    Interpreting . Deck class

    Refactoring

    Making Classes

    FreeCell

    Card: A first (static) go

    Using static Card

    Card: A second (static) go Using static Card

    Review

    Learnt something about:

    Classes

    what properties good classes should have

    how to find them what they look like in Java

    Fields

    what fields are

    what they look like in Java

    A multiple-class program

    More Java code conventions