1 / 33

Programming Fundamentals

Neal Stublen nstublen@jccc.edu. Programming Fundamentals. Applications and DAta. What's a class?. A class is comprised of a set of data and the actions taken on that data Each action is represented by a method A method is a set of statements that performs a specific task

lynde
Download Presentation

Programming Fundamentals

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. Neal Stublen nstublen@jccc.edu Programming Fundamentals

  2. Applications and DAta

  3. What's a class? • A class is comprised of a set of data and the actions taken on that data • Each action is represented by a method • A method is a set of statements that performs a specific task • Program execution will begin in a “main” method

  4. What's an identifier? • An identifier is a name we give to a class or method • No whitespace - spaces, tabs, line breaks, etc. • Not a keyword - words reserved by the programming language • Most likely case-sensitive, so "count" is not the same as "Count"

  5. Identifier "Casing" • UPPERCASE • lowercase • PascalCase or UpperCamelCase • lowerCamelCase • Each language typically has its own style for casing

  6. Examples Pseudocode class Hello main() output "Hello” return endClass

  7. Examples Java public class Hello {     public static void main(String[] args)     { System.out.println("Hello");     } }

  8. Examples C# public class Hello {     static void Main()     { System.Console.WriteLine("Hello");     } }

  9. What's a variable? • Data resides in the computer's memory • A variable names the data stored at a specific location in the computer's memory • Computer programs use variables to access and modify data stored in memory

  10. What's a literal constant? • Data that doesn't change is a constant • Fixed values that appear in a computer program are called literals or literal constants

  11. Examples input myNumber myAnswer = myNumber * 2 myAnswer = myAnswer + 42 output myAnswer • myNumber is a variable • myAnswer is a variable • 2 is a literal constant • 42 is a literal constant

  12. Examples myName= "John Smith" output myName • myName is a variable • “John Smith” is a literal constant

  13. Data Types • Numeric types • Integer • Floating point • String types • Casting converts between data types

  14. Variable Declaration • Variables are declared with a data type • Variables are initialized with a value num myNumber num myNumber = 12 string myName string myName = "John"

  15. Named Constants • Variables can refer to values that are fixed area = 3.14159265 * radius * radius circumference = 2 * 3.14159265 * radius

  16. Named Constants • Variables can refer to values that are fixed num PI = 3.1415926 area = PI* radius * radius circumference = 2 * PI* radius

  17. Uninitialized Variables • Initialization places a known value into the memory location represented by a variable • Without initialization, the contents of a memory location could be anything • Typically called garbage • And the source of many headaches

  18. Assignment • Assign values to variables • Assign using a literal constant • Assign using another variable or expression • In some languages assignment can only be performed between matching types

  19. Examples a = 1 // ‘a’ now has the value ‘1’ b = a // ‘b’ now has the value ‘1’ c = a + b // ‘c’ now has the value ‘2’ num a = 1 string name = “John” name = a // Not allowed

  20. Arithmetic Operations • Addition, + • Subtraction, - • Multiplication, * • Division, / • Modulus, %

  21. Precedence • Multiplication and division precede addition and subtraction • Parentheses are evaluated from the inside out • 2 + 3 * ( ( 4 + 5 ) / 3 + 1 ) • 2 + 3 * ( 9 / 3 + 1 ) • 2 + 3 * ( 3 + 1 ) • 2 + 3 * 4 • 2 + 12 • 14

  22. Good Practices • Use code comments to clarify what is intended • Choose identifiers that are clear • Variable names are typically nouns (things, e.g. radius) • Method names typically combine a verb and noun (act on a thing, e.g. calculateArea) • Code that is easily readable can become self-documenting • What's clear now may not be clear later or clear to someone else

  23. Searching for something? • a = w * l • area = width * length • aTmp = 98.6 • avgTemp = 98.6 • averageTemperature = 98.6 • crntSpd • currentSpeed • ct • cnt • count

  24. Readability • employeename • employeeName • employee_name • getTestComplete • isTestComplete

  25. Spacing and Line Breaks • Use consistent spacing and line breaks • Indentation can show structure class Circle calculateArea apply area formula return endClass

  26. Use Temporary Variables • Break complex algorithms down into component parts total = subtotal * (1 - discountRate) * (1 + taxRate) discount = subtotal * discountRate discountedTotal = subtotal - discount salesTax = dscountedTotal * taxRate total = discountedTotal + salesTax

  27. Write Clear Prompts • Calculation error. • The starting balance is not valid.

  28. Comment First, Code Second • Alternative to pseudocode and flowcharting • Stub out a class or method • Write comments to document what should happen within a method • “Rewrite” the comments as code

  29. Example fillContactList() { // Retrieve contacts from a database // Sort contacts by last name // Add each contact to the contact list }

  30. Programming Structures • Sequence structure • Performs a series of actions in order • Selection structure • Performs one action or sequence or another action or sequence based on a decision • Loop structure • Repeats an action or sequence

  31. Review • What is the value of: • 8 – 4 * 6 / 4 • ( 8 – 4 ) * 6 / 4 • 8 – 4 * ( 6 / 4 )

  32. Exercise • Case Projects, p. 61, #1

  33. Summary • Classes • Identifiers • Variables • Data types • Arithmetic operations and precedence • Good practices • Programming structures

More Related