360 likes | 465 Views
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
E N D
Neal Stublen nstublen@jccc.edu Programming Fundamentals
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
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"
Identifier "Casing" • UPPERCASE • lowercase • PascalCase or UpperCamelCase • lowerCamelCase • Each language typically has its own style for casing
Examples Pseudocode class Hello main() output "Hello” return endClass
Examples Java public class Hello { public static void main(String[] args) { System.out.println("Hello"); } }
Examples C# public class Hello { static void Main() { System.Console.WriteLine("Hello"); } }
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
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
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
Examples myName= "John Smith" output myName • myName is a variable • “John Smith” is a literal constant
Data Types • Numeric types • Integer • Floating point • String types • Casting converts between data types
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"
Named Constants • Variables can refer to values that are fixed area = 3.14159265 * radius * radius circumference = 2 * 3.14159265 * radius
Named Constants • Variables can refer to values that are fixed num PI = 3.1415926 area = PI* radius * radius circumference = 2 * PI* radius
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
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
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
Arithmetic Operations • Addition, + • Subtraction, - • Multiplication, * • Division, / • Modulus, %
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
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
Searching for something? • a = w * l • area = width * length • aTmp = 98.6 • avgTemp = 98.6 • averageTemperature = 98.6 • crntSpd • currentSpeed • ct • cnt • count
Readability • employeename • employeeName • employee_name • getTestComplete • isTestComplete
Spacing and Line Breaks • Use consistent spacing and line breaks • Indentation can show structure class Circle calculateArea apply area formula return endClass
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
Write Clear Prompts • Calculation error. • The starting balance is not valid.
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
Example fillContactList() { // Retrieve contacts from a database // Sort contacts by last name // Add each contact to the contact list }
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
Review • What is the value of: • 8 – 4 * 6 / 4 • ( 8 – 4 ) * 6 / 4 • 8 – 4 * ( 6 / 4 )
Exercise • Case Projects, p. 61, #1
Summary • Classes • Identifiers • Variables • Data types • Arithmetic operations and precedence • Good practices • Programming structures