neal stublen [email protected]. what's a class? a class is comprised of a set of data and the...
TRANSCRIPT
![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](https://reader036.vdocuments.us/reader036/viewer/2022062517/56649f255503460f94c3bdbe/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062517/56649f255503460f94c3bdbe/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062517/56649f255503460f94c3bdbe/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062517/56649f255503460f94c3bdbe/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062517/56649f255503460f94c3bdbe/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062517/56649f255503460f94c3bdbe/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062517/56649f255503460f94c3bdbe/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062517/56649f255503460f94c3bdbe/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062517/56649f255503460f94c3bdbe/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062517/56649f255503460f94c3bdbe/html5/thumbnails/11.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062517/56649f255503460f94c3bdbe/html5/thumbnails/12.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062517/56649f255503460f94c3bdbe/html5/thumbnails/13.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062517/56649f255503460f94c3bdbe/html5/thumbnails/14.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062517/56649f255503460f94c3bdbe/html5/thumbnails/15.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062517/56649f255503460f94c3bdbe/html5/thumbnails/16.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062517/56649f255503460f94c3bdbe/html5/thumbnails/17.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062517/56649f255503460f94c3bdbe/html5/thumbnails/18.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062517/56649f255503460f94c3bdbe/html5/thumbnails/19.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062517/56649f255503460f94c3bdbe/html5/thumbnails/20.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062517/56649f255503460f94c3bdbe/html5/thumbnails/21.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062517/56649f255503460f94c3bdbe/html5/thumbnails/22.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062517/56649f255503460f94c3bdbe/html5/thumbnails/23.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062517/56649f255503460f94c3bdbe/html5/thumbnails/24.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062517/56649f255503460f94c3bdbe/html5/thumbnails/25.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062517/56649f255503460f94c3bdbe/html5/thumbnails/26.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062517/56649f255503460f94c3bdbe/html5/thumbnails/27.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062517/56649f255503460f94c3bdbe/html5/thumbnails/28.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062517/56649f255503460f94c3bdbe/html5/thumbnails/29.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062517/56649f255503460f94c3bdbe/html5/thumbnails/30.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062517/56649f255503460f94c3bdbe/html5/thumbnails/31.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062517/56649f255503460f94c3bdbe/html5/thumbnails/32.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062517/56649f255503460f94c3bdbe/html5/thumbnails/33.jpg)
Summary
Classes Identifiers Variables Data types Arithmetic operations and precedence Good practices Programming structures