+------------------------------------------------------------------------------+ | CS100 | | Box Scope Diagram rules | | Modified from Fall 2000 | | (work in progress) | +------------------------------------------------------------------------------+ | General: | + the following boxes get drawn at CERTAIN points in the code | + changes in later portions of code change the diagram +------------------------------------------------------------------------------+ | Scope: | + Scope refers to the visibility of a piece of code | + What the code can, and cannot, access/use | + statement BLOCKS | - blocks are statements within braces ({ }) | - "stuff" in a block can see other "stuff" in that block: | + variables declared in a block exist within that block | + methods within a block can be called by other methods in that block +------------------------------------------------------------------------------+ | Boxes: | + indicate the limit of SCOPE | + if the calling code cannot find the variable/method/object/class inside | the current box, the calling code "looks" outside the current box | + if the thing sought after is still not found, the calling code | keeps looking at the outer boxes until there no more boxes | + usually the search stops at the class box | + the location in a certain box indicates an entity's scope +------------------------------------------------------------------------------+ | Default values of class and instance variables are zeros: | + char: ASCII Code 0 (ASCII code of 0 is something called NUL PROMPT) | + int: 0 | + float: 0.0 | + double: 0.0 | + boolean: false | + Object: null | + String: null (not ""! Strings are objects) +------------------------------------------------------------------------------+ | Arrow: | + Draw an arrow to connect a reference variable to an object when an | assignment occurs | + Local variables might also contain references, so they may connect | to objects. Draw arrows unless instructed otherwise: | - for clarity, sometimes we will not draw these arrows for | variables inside methods that finish execution | - methods which we occasionally ignore are setters and getters | + When a connection to an object is lost/terminates, draw an X on | the arrow -- the X indicates you are "crossing off" the arrow | + arrows point from variable to object +------------------------------------------------------------------------------+ | Class: | + every class gets a box +------------------------------------------------------------------------------+ | Class variable: | + variable modified as $static$ | + gets a box inside the class box | + values start off as zero (according to type) | + if value changes, cross off the old value and write the new one nearby +------------------------------------------------------------------------------+ | Class method: | + gets a box inside the class box WHEN the method is activated | + write the method name and input argument variables above the method box | + cross off the method when the activation ends | + when a the method is called again write a new box for the method | to the right | + ultimately, you will not draw method boxes to improve clarity +------------------------------------------------------------------------------+ | Object: | + Draw a box inside the class when an object is created | + the reference variable that refers to the object has an arrow drawn to the | object box | + Cross off the object when it is destroyed. | - Sometimes you might skip this step. | - Why? Most of the diagrams we'll have you draw won't correspond to | code that clearly indicates the death of an object. | + Cross off an object for the following conditions:...... +------------------------------------------------------------------------------+ | Instance variable: | + gets a box inside the object box | + values start off as zero (according to type) | + if value changes, cross off the old value and write the new one nearby +------------------------------------------------------------------------------+ | Instance method: | + gets a box inside the object box WHEN the method is activated | + write the method name and input argument variables above the method box | + cross off the method when the activation ends | + when a the method is called again write a new box for the method | to the right | + ultimately, you will not draw method boxes to improve clarity +------------------------------------------------------------------------------+ | Constructor: | + technically, follow the same rules of instance methods | + usually you will not include constructors to ease clarity +------------------------------------------------------------------------------+ | Local variable: | + gets a box inside a method | + has unknown/undetermined values, so write a question mark (?) inside the | local-variable box | + the collection of variables inside the box for a method is called the | ACTIVATION RECORD | + cross off arrows of reference variables after method activation ends | (see Arrow, below) +------------------------------------------------------------------------------+ | Variable in statement block: | + You may declare variables in statement blocks that are within other | statement blocks. | + Examples include $for$ and $while$ structures. | + Draw a box within the method for the block. | + Draw boxes for the variables within that block for the block variables. +------------------------------------------------------------------------------+ | API: | + Builtin methods/variables/classes: | Do not draw boxes unless something your code does creates unique | objects modified by other portions of the code. | + Example: do not draw boxes for $System.out.println()$ +------------------------------------------------------------------------------+ | Strings: | + Draw a class box called STRING CLASS | + Draw string literals inside the string-class box | + Arrows point directly to the string literals +------------------------------------------------------------------------------+ | $args$: | + This is a local variable in the $main$ method. | + Draw an arrow from the $args$ box outside the Main Class. +------------------------------------------------------------------------------+ | Inheritance: | + This model breaks down for inheritance. | + See UML for a more rigorous approach. +------------------------------------------------------------------------------+ | Arrays: | + coming! +------------------------------------------------------------------------------+