All Java statements must be defined in a
All Java statements must be defined in a method m. How do you represent the data over which these statements perform computations? • Is data needed only within that method? • local variable • for statement private variable (i. e. , for (int i = 0; i < 10; i++) ) • Does the method require data? • Is data needed only when m is invoked? parameters m (p 1, p 2, …, pn) • is data constant? Make a static constant for class • Is same data needed by other methods? Make static variable for class • Does method behavior depend on an instance? • Have class model data as instance variables • Make method an instance method • Is data of a fixed primitive type? • int, char, boolean, float, long, double • Is data a collection of homogenous like-minded information of same type? • primitive. Type[] array • Is the size of the collection known in advance? Or can you determine size by user input? • use array • Is the size of the collection arbitrarily large? • use dynamic structure, such as the Linked List structure Nov 20 CS 2102 Handout: Data Modeling
Class design: Identify a clear concept, whose information can be encapsulated and whose behavior can be captured by a set of method definitions • Do you have specific functionality that exhibits no variability? • Perhaps a class with static methods is in order (i. e. , Periodic. Table) • Do you have functionality that differs based on the instance • Create instance methods (i. e. , Element. get. Symbol()) • Are there behaviors that are “just like class Base but also do X” • Identify derived classes that extend Base class • Ensure instance methods of derived class invoke super() • Are there behaviors that share common abstractions but no common implementation? • “Hey, I can play. mp 3, . wav, . snd, . au files” • Define abstract Base class • void play(), void pause(), void stop(), int length() Nov 20 CS 2102 Handout: Data Modeling
- Slides: 2