Object-Oriented Programming Fundamental - Part 2
Roles of Variable
Developers often use a program variable for a specific purpose.
If a variable has multiple purposes to be used at the same time, the logic becomes less obvious. -> it is easier to introduce bugs in our program if we are unable to consistently maintain the value of the variable for all purposes.
The "role" of variable - is a way for developers to represent meta-level programming knowledge.
Ten common variables:
![](https://images2018.cnblogs.com/blog/1307302/201803/1307302-20180321224948721-37370093.png)
![](https://images2018.cnblogs.com/blog/1307302/201803/1307302-20180321225148969-291278426.png)
1. Constant/Fixed value
Purpose: Write once and read many
Value is never changed after initialization (or the first assignment by users)
Example: read the radius of a circle, then print the area of the circle. variable r is a fixed value, gets its value once, never changes after that(e.g., the variable PI in the following example).
It is better to enforce using “final” in Java or “const” in C/C++ syntax: final float PI = 3.14F;
2. Stepper (Incrementer, i++)
Purpose: Goes through a sequence of values systematically. E.g., counting items, moving through array index, i++, k++, j++
Example: Loop where multiplier is used as a stepper. Outputs multiplication table, stepper goes through values from 1 to 10.
![](https://images2018.cnblogs.com/blog/1307302/201803/1307302-20180322110357018-1161119637.png)
3. Most-Recent Holder (latest input value)
Purpose: Most recent member of a group, or simply latest input value. E.g.,: when asking the user for an input until the input value is valid.
Example: Variable s is a most-recent holder since it is withholding the latest input value
![](https://images2018.cnblogs.com/blog/1307302/201803/1307302-20180322110619091-1630907568.png)
4. Most-Wanted Holder (the answer…)
Purpose: Keeps the "best" (biggest, smallest, closest) of values seen.
Example: Find the smallest integer out of ten integers. Variable smallest is a most-wanted holder since it is given the most recent value if it is smaller than the smallest one so far.
![](https://images2018.cnblogs.com/blog/1307302/201803/1307302-20180322110912839-1736422025.png)
5. Gatherer (collector, accumulator)
Purpose: Accumulates values seen so far. E.g.,: accepts integers, then calculates mean.
Example: Variable sum is a gatherer. the total of the inputs is gathered in it. balance = balance + deposit; (balance is a Gatherer)
6. Transformation
Purpose: Compute value in new unit. E.g.: ask the user for capital amount, calculate interest and total capital for ten years.
Example: Variable interest is a transformation and is always calculated from the capital.
![](https://images2018.cnblogs.com/blog/1307302/201803/1307302-20180322111703076-1827482991.png)
7. One-way Flag (Boolean, True/False)
Purpose: serve as a Boolean variable (true or false)
Example: sum input numbers and report if any negative numbers. The one-way flag neg in the example monitors whether there are negative numbers among the inputs. If a negative value is found, it will never return to false.
8. Follower (pointer…, following another object)
Purpose: Points to the old value of another variable as its current value.
Example: input 12 integers and find biggest difference between successive inputs.
![](https://images2018.cnblogs.com/blog/1307302/201803/1307302-20180322112218845-1905368542.png)
9. Temporary (tmp value variable)
Purpose: Keep values that are only needed for very short period (e.g. separated by a few lines of code).
Example: output two numbers in size order, swapping if necessary.
![](https://images2018.cnblogs.com/blog/1307302/201803/1307302-20180322112320956-597576400.png)
10. Organizer (Data Container, Array, List, etc…)
Purpose: An array [ ] for is containing and rearranging elements
Example: input ten characters and output in reverse order.
Example:
![](https://images2018.cnblogs.com/blog/1307302/201803/1307302-20180321225351900-71022125.png)
Wrong Example:
![](https://images2018.cnblogs.com/blog/1307302/201803/1307302-20180322111453129-53522519.png)
Avoid conflicting variables for different purposes, use a new variable if needed.
Exercise:
![](https://images2018.cnblogs.com/blog/1307302/201803/1307302-20180322112509325-788665174.png)
![](blob:https://i.cnblogs.com/1a861e6b-aa12-4dbe-8fc0-75b3dcf7ab5f)