70 likes | 332 Views
Problem Solving. Problem Solving Approach. Break it down into smaller, manageable pieces Procedural approach: Break it down into a sequence of steps Consider tasks to be accomplished. Problem Solving Approach. Break it down into smaller, manageable pieces Procedural approach:
E N D
Problem Solving Approach • Break it down into smaller, manageable pieces • Procedural approach: • Break it down into a sequence of steps • Consider tasks to be accomplished
Problem Solving Approach • Break it down into smaller, manageable pieces • Procedural approach: • Break it down into a sequence of steps • Consider tasks to be accomplished • Object-oriented approach: • Consider data (objects) that needs to managed • Tasks organized around the data
Compound Data Types • Create your own data type • Can contain any data • Can create methods to apply to type • Create solution focusing on data • Known as classes
Example • Grid in Battleship Game • Created module for functions that manipulate a grid. • Needed multiple grids. • But really want to put data (grid) with function that manipulate grid (methods) • Put grid inside a class
Grid as a Class class Grid: def __init__(self) : self.grid = [[B,B,B,B], [B,B,B,B],\ [B,B,B,B], [B,B,B,B]] defprint_grid(self) : … defhas_overlap(self,index,row,col,dir) : …
Using Class Grid g1 = grid.Grid() # creates an instance with its own listg2 = grid.Grid() # creates a 2nd instance with its own list index =0; row=0; col=0; dir=‘h’g1.add_vessel(index,row,col,dir) g2.add_vessel(0,1,2,’v’)# Each grid has aircraft carrier in different position!