Programming Fundamentals

33
PROGRAMMING FUNDAMENTALS Neal Stublen [email protected]

description

Neal Stublen [email protected]. 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 - PowerPoint PPT Presentation

Transcript of Programming Fundamentals

Page 1: Programming Fundamentals

PROGRAMMING FUNDAMENTALS

Neal Stublen

[email protected]

Page 2: Programming Fundamentals

APPLICATIONS AND DATA

Page 3: 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

Page 4: Programming Fundamentals

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"

Page 5: Programming Fundamentals

Identifier "Casing"

UPPERCASE lowercase PascalCase or UpperCamelCase lowerCamelCase Each language typically has its own

style for casing

Page 6: Programming Fundamentals

Examples

Pseudocode

class Hello    main()        output "Hello”    returnendClass

Page 7: Programming Fundamentals

Examples

Java

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

Page 8: Programming Fundamentals

Examples

C#

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

Page 9: Programming Fundamentals

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

Page 10: Programming Fundamentals

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

Page 11: Programming Fundamentals

Examples

input myNumber

myAnswer = myNumber * 2

myAnswer = myAnswer + 42

output myAnswer

myNumber is a variablemyAnswer is a variable2 is a literal constant42 is a literal constant

Page 12: Programming Fundamentals

Examples

myName = "John Smith"

output myName

myName is a variable“John Smith” is a literal constant

Page 13: Programming Fundamentals

Data Types

Numeric typesIntegerFloating point

String types Casting converts between data types

Page 14: Programming Fundamentals

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"

Page 15: Programming Fundamentals

Named Constants

Variables can refer to values that are fixed

area = 3.14159265 * radius * radius

circumference = 2 * 3.14159265 * radius

Page 16: Programming Fundamentals

Named Constants

Variables can refer to values that are fixed

num PI = 3.1415926

area = PI* radius * radius

circumference = 2 * PI* radius

Page 17: Programming Fundamentals

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 garbageAnd the source of many headaches

Page 18: Programming Fundamentals

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

Page 19: Programming Fundamentals

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

Page 20: Programming Fundamentals

Arithmetic Operations

Addition, + Subtraction, - Multiplication, * Division, / Modulus, %

Page 21: Programming Fundamentals

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

Page 22: Programming Fundamentals

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

Page 23: Programming Fundamentals

Searching for something? a = w * l area = width * length

aTmp = 98.6 avgTemp = 98.6 averageTemperature = 98.6

crntSpd currentSpeed

ct cnt count

Page 24: Programming Fundamentals

Readability

employeename employeeName employee_name

getTestComplete isTestComplete

Page 25: Programming Fundamentals

Spacing and Line Breaks

Use consistent spacing and line breaks Indentation can show structure

class Circle

calculateArea

apply area formula

return

endClass

Page 26: Programming Fundamentals

Use Temporary Variables

Break complex algorithms down into component parts

total = subtotal * (1 - discountRate) * (1 + taxRate)

discount = subtotal * discountRatediscountedTotal = subtotal - discountsalesTax = dscountedTotal * taxRatetotal = discountedTotal + salesTax

Page 27: Programming Fundamentals

Write Clear Prompts

Calculation error. The starting balance is not valid.

Page 28: Programming Fundamentals

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

Page 29: Programming Fundamentals

Example

fillContactList()

{

// Retrieve contacts from a database

// Sort contacts by last name

// Add each contact to the contact list

}

Page 30: Programming Fundamentals

Programming Structures

Sequence structurePerforms a series of actions in order

Selection structurePerforms one action or sequence or another

action or sequence based on a decision Loop structure

Repeats an action or sequence

Page 31: Programming Fundamentals

Review

What is the value of:

8 – 4 * 6 / 4( 8 – 4 ) * 6 / 48 – 4 * ( 6 / 4 )

Page 32: Programming Fundamentals

Exercise

Case Projects, p. 61, #1

Page 33: Programming Fundamentals

Summary

Classes Identifiers Variables Data types Arithmetic operations and precedence Good practices Programming structures