150 likes | 272 Views
BIT115: Introduction to Programming. Instructor: Craig Duckett. Lecture 4b. Creating Named Constants with final. Many programs have data that does not need to be changed. Littering programs with literal values can make the program hard do read and maintain.
E N D
BIT115: Introduction to Programming Instructor:Craig Duckett Lecture 4b
Creating Named Constants with final • Many programs have data that does not need to be changed. • Littering programs with literal values can make the program hard do read and maintain. • Replacing literal values with constants remedies this problem. • Constants allow the programmer to use a name rather than a value throughout the program. • Constants also give a singular point for changing those values when needed.
Creating Named Constants with final • Constantskeep the program organized and easier to maintain. • Constantsare identifiers that can hold only a single value. • Constantsare declared using the keyword final. • Constantsneed not be initialized when declared; however, they must be initialized before they are used or a compiler error will be generated.
Creating Named Constants with final • Once initialized with a value, constants cannot be changed programmatically. • By convention, constants are all upper case and words are separated by the underscore ‘_’ character. final float CAL_SALES_TAX = 0.75; Both the Java and Becker API libraries have several constants built in programmatically by default. For example: Java has math.PI(where PI = 3.14159265) Becker has direction.NORTH (including EAST, SOUTH, WEST) where the direction represents specific degrees on a compass like 0, 90, 180, 270
lisa.grabThings(6) Example from Robot World A B 1 public void grabThings() 2{ intnumMoves = 0; 3// move one fewer times than there are Things 4while (numMoves < 4) 5 { this.pickThingIfPresent(); 6 this.move(); 7 numMoves= numMoves + 1; 8 } 9 this.pickThingIfPresent(); 10 } • 1 public void grabThings(int numObjects) • 2{ intnumMoves = 0; • 3// move one fewer times than there are Things • 4while (numMoves < numObjects) • 5 { this.pickThingIfPresent(); • 6 this.move(); • numMoves = numMoves + 1; • numObjects = numObjects - 1; • 9 } • 10 this.pickThingIfPresent(); • 11 } First Pass C • 1 public void grabThings(6) • 2{ intnumMoves = 0; • 3// move one fewer times than there are Things • 4while (0 < 6) • 5 { this.pickThingIfPresent(); • 6 this.move(); • 1 = 0 + 1; • 5 = 6 - 1; • 9 } • 10 this.pickThingIfPresent(); • 11 } numThingsis like a temporary variable that is automatically assigned a value just before grabThingsbegins to execute. The value it is assigned is the value given between the parentheses when grabThingsis called. Writing lisa.grabThings(6), numThingswill be given the value six. Writing karel.grabThings(7), numThingswill be given the value seven. Writing jasmine.grabThings(8), numThingswill be given the value eight.
Another Way to Show Increment & Decrement numMoves = numMoves + 1; numThings = numThings ˗ 1; SAMEAS numMoves++; numThings ˗ ˗; To Repeat: X = X + 1 is the same as X++ X = X - 1 is the same as X--
Counters • Declare the datatype, and give it a name: int counter; • Then, initialize it with a value: counter = 0; • So, putting it together it might look like this: int counter; counter = 0; • You can also do this all on the same line by combining the declaration and the initialization, which saves keystrokes: intcounter = 0;
CountersCONTINUED • You can use initialize counters outside of loops and inside of loops, which affects their scope(which we’ll talk about in a moment), all depending on the logic of the code. intcounter = 0; while (counter < 10) // As long as this is true, loop { move(); counter++; // Same as counter = counter + 1; }