1 / 15

BIT115: Introduction to Programming

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.

mervin
Download Presentation

BIT115: Introduction to Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. BIT115: Introduction to Programming Instructor:Craig Duckett Lecture 4b

  2. 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.

  3. 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.

  4. 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

  5. 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.

  6. 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--

  7. To Summarize:Temporary Variables (Local Variables)

  8. Declaring an Integer Variable

  9. 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;

  10. 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; }

  11. EXAMPLE:Counting Things on an Intersection

  12. Storing the Results of a Query

  13. A Quick Word About Scope

  14. Time for Some ICE

More Related