Neal Stublen [email protected]. What's a class? A class is comprised of a set of data and the...

33
PROGRAMMING FUNDAMENTALS Neal Stublen [email protected]

Transcript of Neal Stublen [email protected]. What's a class? A class is comprised of a set of data and the...

Page 1: Neal Stublen nstublen@jccc.edu. What's a class?  A class is comprised of a set of data and the actions taken on that data  Each action is represented.

PROGRAMMING FUNDAMENTALS

Neal Stublen

[email protected]

Page 2: Neal Stublen nstublen@jccc.edu. What's a class?  A class is comprised of a set of data and the actions taken on that data  Each action is represented.

APPLICATIONS AND DATA

Page 3: Neal Stublen nstublen@jccc.edu. What's a class?  A class is comprised of a set of data and the actions taken on that data  Each action is represented.

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: Neal Stublen nstublen@jccc.edu. What's a class?  A class is comprised of a set of data and the actions taken on that data  Each action is represented.

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: Neal Stublen nstublen@jccc.edu. What's a class?  A class is comprised of a set of data and the actions taken on that data  Each action is represented.

Identifier "Casing"

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

style for casing

Page 6: Neal Stublen nstublen@jccc.edu. What's a class?  A class is comprised of a set of data and the actions taken on that data  Each action is represented.

Examples

Pseudocode

class Hello    main()        output "Hello”    returnendClass

Page 7: Neal Stublen nstublen@jccc.edu. What's a class?  A class is comprised of a set of data and the actions taken on that data  Each action is represented.

Examples

Java

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

Page 8: Neal Stublen nstublen@jccc.edu. What's a class?  A class is comprised of a set of data and the actions taken on that data  Each action is represented.

Examples

C#

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

Page 9: Neal Stublen nstublen@jccc.edu. What's a class?  A class is comprised of a set of data and the actions taken on that data  Each action is represented.

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: Neal Stublen nstublen@jccc.edu. What's a class?  A class is comprised of a set of data and the actions taken on that data  Each action is represented.

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: Neal Stublen nstublen@jccc.edu. What's a class?  A class is comprised of a set of data and the actions taken on that data  Each action is represented.

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: Neal Stublen nstublen@jccc.edu. What's a class?  A class is comprised of a set of data and the actions taken on that data  Each action is represented.

Examples

myName = "John Smith"

output myName

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

Page 13: Neal Stublen nstublen@jccc.edu. What's a class?  A class is comprised of a set of data and the actions taken on that data  Each action is represented.

Data Types

Numeric typesIntegerFloating point

String types Casting converts between data types

Page 14: Neal Stublen nstublen@jccc.edu. What's a class?  A class is comprised of a set of data and the actions taken on that data  Each action is represented.

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: Neal Stublen nstublen@jccc.edu. What's a class?  A class is comprised of a set of data and the actions taken on that data  Each action is represented.

Named Constants

Variables can refer to values that are fixed

area = 3.14159265 * radius * radius

circumference = 2 * 3.14159265 * radius

Page 16: Neal Stublen nstublen@jccc.edu. What's a class?  A class is comprised of a set of data and the actions taken on that data  Each action is represented.

Named Constants

Variables can refer to values that are fixed

num PI = 3.1415926

area = PI* radius * radius

circumference = 2 * PI* radius

Page 17: Neal Stublen nstublen@jccc.edu. What's a class?  A class is comprised of a set of data and the actions taken on that data  Each action is represented.

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: Neal Stublen nstublen@jccc.edu. What's a class?  A class is comprised of a set of data and the actions taken on that data  Each action is represented.

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: Neal Stublen nstublen@jccc.edu. What's a class?  A class is comprised of a set of data and the actions taken on that data  Each action is represented.

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: Neal Stublen nstublen@jccc.edu. What's a class?  A class is comprised of a set of data and the actions taken on that data  Each action is represented.

Arithmetic Operations

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

Page 21: Neal Stublen nstublen@jccc.edu. What's a class?  A class is comprised of a set of data and the actions taken on that data  Each action is represented.

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: Neal Stublen nstublen@jccc.edu. What's a class?  A class is comprised of a set of data and the actions taken on that data  Each action is represented.

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: Neal Stublen nstublen@jccc.edu. What's a class?  A class is comprised of a set of data and the actions taken on that data  Each action is represented.

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: Neal Stublen nstublen@jccc.edu. What's a class?  A class is comprised of a set of data and the actions taken on that data  Each action is represented.

Readability

employeename employeeName employee_name

getTestComplete isTestComplete

Page 25: Neal Stublen nstublen@jccc.edu. What's a class?  A class is comprised of a set of data and the actions taken on that data  Each action is represented.

Spacing and Line Breaks

Use consistent spacing and line breaks Indentation can show structure

class Circle

calculateArea

apply area formula

return

endClass

Page 26: Neal Stublen nstublen@jccc.edu. What's a class?  A class is comprised of a set of data and the actions taken on that data  Each action is represented.

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: Neal Stublen nstublen@jccc.edu. What's a class?  A class is comprised of a set of data and the actions taken on that data  Each action is represented.

Write Clear Prompts

Calculation error. The starting balance is not valid.

Page 28: Neal Stublen nstublen@jccc.edu. What's a class?  A class is comprised of a set of data and the actions taken on that data  Each action is represented.

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: Neal Stublen nstublen@jccc.edu. What's a class?  A class is comprised of a set of data and the actions taken on that data  Each action is represented.

Example

fillContactList()

{

// Retrieve contacts from a database

// Sort contacts by last name

// Add each contact to the contact list

}

Page 30: Neal Stublen nstublen@jccc.edu. What's a class?  A class is comprised of a set of data and the actions taken on that data  Each action is represented.

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: Neal Stublen nstublen@jccc.edu. What's a class?  A class is comprised of a set of data and the actions taken on that data  Each action is represented.

Review

What is the value of:

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

Page 32: Neal Stublen nstublen@jccc.edu. What's a class?  A class is comprised of a set of data and the actions taken on that data  Each action is represented.

Exercise

Case Projects, p. 61, #1

Page 33: Neal Stublen nstublen@jccc.edu. What's a class?  A class is comprised of a set of data and the actions taken on that data  Each action is represented.

Summary

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