



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




1
Gregor Johann Mendel (1822–1884)
2
3
class Puzzle {
//representation of a puzzle state private int state;
//create a new random instance public void scramble() {...}
//say which tile occupies a given position public int tile(int row, int col) {...}
//move a tile public boolean move(char c) {...} }
4
7
8
state scramble() tile() move()
Puzzle EPuzzle state scramble() tile() move() numMoves printNumMoves()
9
class EPuzzle extends Puzzle { private int numMoves = 0; public void scramble() {...} public boolean move(char d) {...} public void printNumMoves() {...} }
10
Object
Puzzle Array
EPuzzle
subclass of Puzzle and Object
superclass of EPuzzle and Puzzle
superclass of EPuzzle subclass of Object
class hierarchy is a directed acyclic graph (dag)
19
class EPuzzle extends Puzzle { ...
public void scramble() { state = "978654321"; //OK since state inherited numMoves = 0; }
//similar code for move }
20
21
22
class EPuzzle extends Puzzle { protected int numMoves = 0; ... public void scramble() { super.scramble(); numMoves = 0; } public boolean move(char d){ boolean p = super.move(d); if (p) numMoves++; //legal move? return p; } }
class A { public int m() {...} }
class B extends A { private int m() {...} //illegal! }
A supR = new B(); //upcasting supR.m(); //would invoke private method in class B at runtime!
25
26
27
abstract class Puzzle { protected int state; public void scramble() { state = 978654321; }
//abstract methods (no code) abstract public int tile(int r, int c); abstract public void move(char d); }
28