AP Computer Science Principles Notes
Five Big Ideas
- Big Idea 1: Creative Development (CRD)
- Big Idea 2: Data (DAT)
- Big Idea 3: Algorithms and Programming (AAP)
- Big Idea 4: Computing Systems and Networks (CSN)
- Big Idea 5: Impact of Computing (IOC)
Pair Programming
- Two programmers work together.
- Driver: Writes program code.
- Observer (Pointer, Navigator): Reviews each line of code as it is typed.
Collaboration
- Definition: Action of working with someone else to produce or create something.
- AP Video:
- Collaboration in big companies involves sharing and discussing ideas to determine the best solutions for users.
- Collaboration with diverse perspectives helps avoid bias.
- Interpersonal skills learned through collaboration:
- Communication
- Consensus Building
- Conflict Resolution
- Negotiation
- Team: People working together to accomplish a common goal.
- Benefits:
- Task Sharing
- Joint Expertise
- Overcoming Project Obstacles
- Networking
Program Aspects
- Program Function and Purpose
- Program Design and Development
- Identifying and Correcting Errors
HTML5 - Hypertext Markup Language
- Foundation of the structure for a website.
- Allows web designers to create web pages across operating systems.
- Dominant language of the Web, used by millions of websites.
Think-Pair-Share
- Students think through a problem alone, pair with a partner to share ideas, and then share results with the class.
Code Comments
- Purpose:
- Communicate with partner(s).
- Communicate with yourself.
- Acknowledge code segments used from other sources.
- Acknowledge media used from other sources.
- Differentiate contributions of different partners.
- Clarify the functionality of your code
Collaboration Tools
- repl.it - multiplayer
- GitHub - Collaboration and repository
- Shared Doc or Folder
- Seeking Feedback: Ask friends and family to test drive your program and give you feedback
- Collaboration tools allow students, instructors, and teaching assistants to exchange resources in a number of different ways, depending on what is needed for a particular task.
- Assignments and tasks can be created both in and outside class.
- They can be used to help develop active learning assignments.
Computers and Data
- Computers are digital and store/process data.
- Most use a binary system.
- Binary System: Number system with two digits (0 & 1).
Bits and Bytes
- Bit: Binary Digit, smallest unit of data a computer can process.
- Byte: Eight bits combined.
- A byte can represent a single character.
Abstraction
- Approach used in object-oriented programming (C++, Java).
- Humans interact with objects (smartphones, tables).
- Objects have properties: size, shape, color, etc.
- Objects have different uses.
Data Hiding
- Allows external use of an object without knowing internal workings.
- Users need to know how to use an object, not how it works.
Procedural Abstraction
- Code sections referred to as procedures, functions, or methods.
- Procedure: Name given to a group of programming instructions used multiple times in a program.
- Makes code easier to develop, edit, and read.
Modularity
- Simplifying code into smaller, manageable blocks (modules) with comments.
- Easier to pinpoint issues instead of checking the entire program.
- Benefits:
- Reduced development time (code module reuse)
- More reliable code
- Standardized development process
- Easier to transport code
- Easier troubleshooting
- Easier to modify
Computing Innovations
- Applications:
- Games
- Social Media
- Business
- Productivity
- Physical Devices:
- Computers
- Smart Phones/Tablets
- Wearables
- Systems:
- E-commerce
- Cloud Services
Program Complexity
- Complexity Increases with functionality and size.
- Can cause code organization, debugging, and program flow issues.
Reducing Complexity
- Modularity: Divide code into sections with comments.
- Hierarchy: Follow logical programming rules.
- Independence: Make each program module independent.
The DRY Principle
- DRY: Don’t Repeat Yourself
- Aim: Eliminate code repetition.
- Applies to code and documentation.
- Results in cleaner, easier-to-follow code.
Computer Hardware
- Physical parts of a computer system.
- Categories:
- External Hardware:
- Computer Case
- Monitor
- Keyboard
- Mouse
- Internal Hardware:
- Hard Disk Drive (HDD): Permanent storage for data and software.
- Motherboard: Largest circuit board, holds the CPU.
- Video Card: Controls output to the computer screen.
- CPU: Central processing unit, processes data and instructions.
- Ports: HDMI, Network, USB, Audio, VGA, DVI, SATA, etc.
- Power Supply: Converts household electricity to usable voltage.
- External Hardware:
Memory
- Electronic component that stores:
- Instructions for the processor
- Data for the instructions
- Results of processing
- Types:
- Volatile Memory: Needs power (RAM).
- Nonvolatile Memory: Permanent.
- Random Access Memory (RAM)
- Virtual Memory: Transfers data from RAM
- Read-Only Memory (ROM)
- Flash Memory: USB devices
Program Input
- Computer programs accept input in different forms:
- Tactile (touch)
- Audio
- Visual
- Text (including numerical values)
- Programs are written in an event-driven environment.
- Events triggered by actions send input to the program.
- User-triggered events:
- Mouse clicks
- Screen taps/Swipes (force touch)
- Physical button clicks
- Keyboard entries
- Audio trigger (keyword or phrase)
- Input affects the output.
- Outputs produced by a device:
- Visual
- Audio
- Tactile (touch/feel)
- Text
- Action triggers an event.
- Action may be initiated by the user or another program/device.
- The program “jumps” to the code segment according to the event.
- The code segment is executed.
- Output is triggered by the code segment (or additional events are triggered).
- The program code is not necessarily executed “in order”.
- Code segments are executed as they are called, according to the events triggered.
Commenting Code
- Descriptive text added to code to help understand it.
Provide description abt purpose and meaning of code that has been commented - Comments are ignored by the browser.
- Helpful when:
- Revisiting code after a long period of time.
- Code reuse
- Code libraries
- Sharing code (e.g., assuming responsibility for other people’s code, new job, new project, new team).
- Combining code from multiple sources (team members).
- Add comments throughout the code as it is being created.
- Types of comments:
- Single-line:
- Brief comments/descriptions
- Can be placed on the same line as the command being commented.
- Multi-line:
- Extended comments/descriptions
- Placed above or beside the command/code being commented.
- Single-line:
- Syntax:
- HTML:
<!-- Code comment goes here --> - JavaScript and C++:
// single comment
- HTML:
Managing Events
- Event: Specific action on a site that causes the browser to run a set of code commands.
- JavaScript supports various event types (associated with forms and form fields).
- Managing events is required for any website to run effectively and meet user expectations.
- Events can occur in rapid succession.
- Event Handler: Block of code executed when an event is triggered.
- Syntax:
<HTML tag event_handler = “Javascript commands;”>- HTML tag: HTML tag where the event occurred.
- Event_handler: The event handler name.
- JS commands: Code that will be executed as a result.
Event Handler Objects
- onClick: Mouse and Keyboard
- onLoad, onUnload, onError: Window and Document
- onSubmit, onReset, onBlur, onFocus: Form
- onLoad, onError, onAbort: Hyperlink
Searches
- User populates an input box with a search term and triggers the search by pressing a button.
- Search Types:
- Transactional: Involves making a purchase or conducting a financial transaction.
- Common Searches:
- Buying an item
- Getting a loan
- Sending money
- Common Searches:
- Navigational: User is searching for a specific page they want to access.
- Common Searches:
- Amazon
- Bestbuy
- Princeton University
- Common Searches:
- Informational: User is searching for specific info
- Common Searches.
- How to build a website?
- How to build my own computer?
- Common Searches.
- Transactional: Involves making a purchase or conducting a financial transaction.
Number Systems
Binary Number System
- Computers are digital and used to store and process data.
- Most computers use a binary system to operate.
- Binary System: Number system with just two digits (0 and 1).
Hexadecimal Number System
- Numbering systems used in electronic systems
- Makes large binary numbers smaller
- Arranges them into groups of four digits to make them easier to understand
- Also used to represent colors in digital systems using color codes (e.g., HTML).
- Binary Number System: All values are represented by two symbols, 0 and 1.
- Hexadecimal Number System: All values are represented by 15 symbols.
- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E and F
ASCII Codes
- American Standard Code for Information Interchange
- Character sets used in today’s computers and in HTML are all based on ASCII.
- A character code consisting of up to 7-bits
- Numbers 0-9
- Upper and lower letters A-Z
- Some special characters
Program Design and Development
- Programs are developed with a specific purpose in mind.
- Developers follow specific steps and stick to their plan
- Sometimes the development is more exploratory than anything and the steps are dictated by what happens (both good and bad)
How is a program developed?
- Developers start by investigating the problem/purpose and reflect
- An investigation is an important step in the process
- Developers must:
- Determine the requirements of the program
- Understand the constraints
- Understand the user concerns and interests
- How do developers investigate?
- Surveys
- User testing
- Interviews
- Direct observations
- After initial investigation and reflection:
- Developers design a program by:
- Brainstorming (draw on investigation)
- Storyboarding the program
- Planning user experience
- Laying out the user interface
- Organizing into modules
- Developers design a program by:
- Developers decide on the program requirements that:
- Describe how a program should behave
- Include a list of user interactions
- Program specification outlines requirements
- Developer creates a prototype of the program (or components):
- An incremental process
- Developers test the program every step of the way
- Testing occurs at the
- Micro level
- Macro level
- Testing occurs at the
- Developers refine and revise through testing, feedback, and reflection.
Logical programming
- Allows programs to follow an ordered sequence of events
- Provides for better program organization
- A necessary component of various programming types.
Algorithms
- An algorithm is a set of step-by-step logical instructions leading to a solution to a problem.
- Algorithms are used in a variety of fields, such as mathematics, physics, chemistry, biology.
- They are also used in computer sciences as a planning tool before code is written.
Benefits
- Creates a blueprint that can be followed
- Eliminates potential logic and runtime errors
- A roadmap to the expected solution
Planning the code
- The key to writing effective code is to do the planning ahead of time before any of the code is actually written
Flowcharting
- One technique commonly used to plan the code is to create a flowchart prior to the code being written
- As a result the entire program flow can be reviewed from beginning to end ahead of time
- Potential mistakes can be spotted early and corrected before a single line of code is written.
Binary Numbers
- All the multimedia that we see on modern computers is made possible by simple on/off switches
- Binary is a number system that uses two digits
- 1 - Represents an electrical signal in your computer being on
- 0 - Represents an electrical signal in your computer being off
Binary Numbers and the physical process of a computer
- Circuits in a computer's processor are made up of billions of transistors
- The digits 1 and 0 used in binary reflect the electrical signal being on and off
- All software, music, documents, and any other information that is processed by a computer is stored in sequences of binary
- How a binary sequence is interpreted depends on how it will be used
- A byte of information like 0100 and 1001 is used to represent instructions to the computer
Digital Data and Abstractions
- At the lowest level, bits represent all digital data
- Abstraction reduces complexity by allowing us to focus on the central problem/idea
- Analog: a mechanism, device, or technology that represents data by measurement of the continuous physical variable
- The use of digital data to approximate real world analog data is an example of abstraction’
- How do you represent the smoothness of a piece of analog data with a simple binary on/off switch?
- Sampling - measure the values
Data - AP Exam Weighting 17-22%
- Binary number conversion
- Data compression
- Extracting information from data
- using programs with data
- Error handling - Types of Errors
- Significance of Data
- Recently computer processing and web design have come to heavily rely on data and data retention
- Anything from software to personal records can be described as computer data
Data
- Most online businesses wouldn’t be able to function without the ability to process and retain customer data.
- Amazon
- American International Group (AIG)
- Verizon Wireless
- Internal Revenue Service
- CityBannm
Storage
- Data can be stored temporarily in the following structures
- Variables
- Constants
- Arrays and lists
- Tables
Program Errors
- Programmers constantly run into errors when programming
- It’s an expectation that it's part of the process
- Proficient programmers can identify errors as the following types
Logic error
- Error caused by programmer making mistake in the algorithm, making it behave unexpectedly
- Syntax errorMistake when a programmer makes a type or writes some code that doesn't follow language or forgets to write some code
- Example:
- colon/semicolon
- Parenthesis
- Curly braces
- Indentation
- Quotes
- Variable definition
- Example:
Run time error
- Program fails in midst of running, commonly referred to as bug, key to identifying this error is that the program runs/complies initially but fails and cannot complete it’s processes
- May result from dividing by zero
Overflow error
- When a program is required to perform calculation that is outside of defined range of values
- Due to memory allocation constraints that programming languages require, certain values are too big to calculate and/or display
Debugging
- Process of identifying and correcting those errors is known as debugging code
- Typical reasons
- Mistakes in syntax
- Issues with logic design
- Compilation steps
- Execution errors
- Incorrect arithmetic
- Missing/misplaced resources
- Incompatible interfaces and/or software
Load time errors
- Occurs as soon as script is loaded by browser, browser reads the code looking for mistakes
Run time errors - Occur after the script has been loaded
Logical errors - No syntax errors
Debugging Techniques
- Key to writing effective code is to do the planning ahead of time before any code is actually written
Flowcharting
- Technique that can be used to plan the code to prevent logical errors
Pseudocode
- Verbal description of programs actions
- English language representation of computer code
- Short phrases describing actions the program will take
Benefits
- Helps develop algorithms
- An alternative to creating flowcharts
- Allows easily find logical mistakes before a single line of code is written
Integers
- Integers are represented by a Fixed Number of Bits
- All data is represented by 1s and 0s arranged in groups called bytes
- This includes integers - whole numbers, even, and odd, including 0
- Integers are represented in computers by a fixed number of bits
- Example
- Some programming languages store data values in up to 32 bits or (4 bytes)
- 4 bytes can represent 2^{32} different values or a little over 4 billion different values total
- Example
Overflow Error
- If a program encounters a calculation that requires a number larger than what its memory will allow it to be stored, this can result in an overflow error.
Round-off Error
- Programming languages can have problems with real numbers like pi
Computers Available memory
- Ideal situation
Manual coding
- Requires knowledge of first, second, and third-generation programming languages and their syntax
- Binary machine languages
- Machine dependent assembly languages (C/ C++/ Java, python)
Low code development
- Eliminates need for writing code manually
- Relies on menus and graphical objects
- allows developers to build software using drag and drop features
- Provides developers with the ability to view/update code if necessary
- Low Code development platform (CLDP) is an environment focus on developing software applications through the use of graphical user interfaces.
- LCDPs allows developers without formal programming skills to construct fully functional apps
- Frequently used to introduce students with no prior computer science background to basic programming concepts
Some of best known low code development
- Code.org
- Non profit website designed to introduce students without prior CS experiences to computer programming
- Utilizes a variety of drag and drop projects used to introduce basic programming concepts
- Mit app inventor
- Greenfoot
- Alice
- Scratch
Decimal Number System Place Values … Powers of 10
- 5 0 1 2
- 1000s 100s 10s 1s
- 1 1 0 1
- 8s 4s 2s 1s
- 1 * 8, 4 * 1, 2 * 0, 1 * 1, 8 + 4 + 0 + 1
- Constructing a binary number means figuring out which powers of 2 add up to the number you want
- 00101001
- 01000111 = 71
- 01111111 = 127
- 1001 001 1 + 8 + 64 =73
- 1001010
Line Breaks
- Unlike HTML JavaScript is not flexible when it comes to the placement of line breaks
- Breaks cannot be placed within commands or quoted text
JavaScript Identifiers
- Names used to uniquely identify JavaScript variables, keywords, and functions.
- Numbers are not allowes to be used as identifiers
- All Javascript names must star with one of the following
- Lowercase or uppercase letters
- A special character ie a dollar sign
- An underscore
Hyphenated Words
- Occasionally programs may include hyphenated names and/or hyphenated names
- Check-in
- Clean-out
- Know-how
Data Compression
- Data compression is the reduction of the number of bits needed to represent data
- Used to save transmission time and storage space
- When data is compressed, you look for repeated patterns and predictability.
Text Compression
- Remove all repeated characters and insert a single character or symbol in its place.
Data compression methods
- Lossless: reduces the number of bits stored or transmitted while guaranteeing complete reconstruction of the original date, typical approach where loss of words or numbers would change the information
- Lossy: Significantly reduces the number of bits stored or transmitted but only allows reconstruction of an approximation of the original data. Typical approach where removal of some data little or no discernible effect on the representation of the content since the data removed is redundant, unimportant or imperceptible
- All data compression algorithms have trade-offs
- Lossy data compression can reduce data more than lossless.
- Lossless data compression allows u to restore the original data but it can not be compressed C a as much as lossy algorithms
Essential Knowledge
- THe ability to process data depends on the capabilities of the users and their tools
- Data sets pose challenges regardless og size, such as
- The need to clean data
- Incomplete date
- Invalid data
- The need to combine data source
- Depending on how data were collected, they may not be uniform
- Cleaning data is a process that makes the data uniform without changing their meaning
- Problems of bias are often created by the type or source of data being collected. Bias is not eliminated by simply collecting more data
Data Collection
- Collecting data
- Issues to consider
- Source
- Do you need more sources
- Tools to analyze data
- Source
- Issues to consider
- Processing data is affected by the size
- How much information can we get?
- Can one computer handle our task(s)
- May need to use parallel processing
- Use two or processors to handle different parts of the task
- Check the scalability of the system
- May need to use parallel processing
- Is there a potential bias
- Intentional
- Who collected the data
- Do they have an agenda
- Unintentional
- How is the data collected
- Who collected the data
- Intentional
Data Cleaning
- Identifying incomplete, corrupt, duplicate, or inaccurate records
- Replacing, modifying, or deleting the “dirty” data
- Be careful about modifying or deleting
- Be sure there is a mistake
- Keep records of what data is modified/deleted and why
- Invalid data may need to be modified - keep form consistent
- Be careful about modifying or deleting
Operators and Operands
- One of the main advantages of languages like Javascript and Python over HTML is their ability to perform calculations involving multiple variables or expressions.
Operators
- This can be done thanks to the presence if operations
- An operator is an element/symbol within an expression that can be used to perform an action, such as adding or combining multiple elements
- This includes standard mathematical operators and others.
Operands
- The variables and/or expressions that the operator is being applied to are known as operands.
- These include variables, data, elements or both.
Metadata
- Essential Knowledge
- Information is the collection of facts and patterns extracted from data
- Data provide opportunities for identifying trends, making connections, and addressing problems
- Digitally processed data may show a correlation between variables. A correlation found in data does not necessarily indicate that a causal relationship exists. Additional research is needed to understand the exact nature of the relationship
- Often a single source does not contain the data needed to draw a conclusion
- Metadata is data about data
- Changes and deletions made to metadata do not change the primary data
- Metadata is used for finding, organizing, and managing informationMetadata - data about data
Lecture
- Metadata can increase the effective use of data or data sets by providing additional information
- Metadata allows data to be structured and organized
What is MetaData? - Prefix Mets
- Behind, among, between
- Some data has information about itself
- Author
- Date
- Length/sizeWhy?
- Identify
- Track/Organize
- Process
Finding patterns
- Data allows us to gain knowledge
- Look for trends
- Look for patterns
- Answer questions
Lecture part: Arithmetic Operators
- In JavaScript programming, arithmetic operators are used in mathematical formulas to perform simple math
Unary Operators
- If an operator impacts just one operand, it is known as a unary operator
- X++ is same as x = x + 1
- X-- is same as x = x - 1
Binary Operators
- If an operator impacts two operands in an expression, it is known as the binary operator.
Assignment operators
- In addition to arithmetic operators, javascript also uses Assignment operators to assign specific values to a declared variable.
- For quicker results, they can be combined with arithmetic operators
- For example, using
+=, it will add the two variables and store the value in the variable to the left of the operator - X +=2 and x = x + 2
Using Programs with Data
- Users can interact with the data:
- Filter
- Sort
- Combine
- Transform
- Cluster
- Classify
- Each iteration leads to more knowledge and insight
- Output is for the user
Insights
- Essential Knowledge
- Programs are used in an iterative and interactive way when processing information to allow users to gain insight and knowledge about data.
- Programmers can use programs to filter and clean digital data
- Combining data sources, clustering data, and classifying data are parts of the process of using programs to gain insight and knowledge from the data
- Insight and knowledge can be obtained from translating and transforming digitally represented information
- Patterns can emerge when data is transformed using programs
Lecture
- Boolean Variables
- Store one of two values (True/False)
- It makes it possible to build a boolean expression that controls the flow of the program through loops and conditionals.
Comparison Operators
- In order for Boolean expressions to work, must be evaluated using a comparison operator.
- The comparison operator compares two expressions in order to determine which is greater or if they are equal.
Logical operators
- While comparison operators will compare two or more expressions to one another, applying a logical operator will allow you to connect them instead.
- Doing this would be required if actions in your code were dependent on the outcome of multiple expressions being evaluated.
Data Processing
- Essential Knowledge
- Programs can be used to process data to acquire information
- Some processes that can be used to extract or modify information from data include the following
- Tables, diagrams, text, and other visual tools can be used to communicate insight and knowledge gained from data
- Search tools are useful for efficiently finding information
- Data filtering systems are important tools for finding information and recognizing patterns in data
- Programs such as spreadsheets help efficiently organize and find trends in information
- Transforming every element of a data set
- Filtering a data set
- Combining or comparing data in some way
- Visualizing a data set through a chart, graph, or other visual representation
Spread sheets very powerful
- Selection
- Iteration
Lists in Programming Languages
- Flexibility to do anything programmer wants
- Visit in Big Idea 3 Lecture Powerpoint
Powerpoint: Functions
- In math, a function is an operation performed with a set of inputs that produces an output
- Computer programming languages allow you to combine different expressions, operators and commands into bricks of commands also known as functions
- A function is a series of commands that can be executed by your program to calculate a value or perform an action
- Due to their ability to combine multiple commands and/or tasks, functions are an excellent data analysis tool
- Functions may be used for
- Complex mathematical calculations
- Data processing
- Data analysis
- And many more 🎉
Benefits
- Complex programming tasks can be broken down into simpler steps:
- Easier to understand
- Easier to debug
- Code does not have to be duplicated within a program
- Saves time
- Saves space
- Efficiency
- Syntax for a function
- Example
Function creation
- Upcoming Topics
- Lecture
Function Parts
- Just like a variable, a function has to be created using a function declaration like the one below.
Functions
- The function declaration can be placed in the or the section of the program.
- Functions are then invoked/executed through a JavaScript statement known as the Function Call
Function Calls
- Once declared, a function can be executed multiple times simply by calling the function name along with its parameters in any line of code
Ex. AnyFunction(Parameters);
Functions
- Function Output
- In a standard function, all of the function commands are contained inside of the function
- When the function is called upon to execute these commands, the output is provided from within the function
Returning values
- In addition to displaying pre-defined data on the screen, functions can send results back to the function call statement is these results are needed by the program for additional processing.
- This action can be accomplished using the return command along with the variable data that is being passed back
Arrow functions
- In an arrow function, the function return statement is implicit as long as the function contains a single expression only
- If the function requires multiple expressions, the return statement must be stated explicitly in the code
Variable
- Abstraction inside a program that can hold a value
Lecture
- Variables
- Declaring a variables
- Assigning values
- Data types
Variables
A variable is a defined element in a computer program or inside of a data file that can substituted to retrieve a piece of information stored under the element name. - A variable is a name given to a piece of data/information
- Ex.
- Contact names in a cell phone
- Favorites in a browser menu
- Mathematic symbols, e.g. x,y,z, etc.
Variables Benefits
- Easier to Remember
- Quickly Accessible
- Save time
- Variable - abstraction in a program that holds a value
Reusable
Variable Declaration
- Before a variable can be used in the code, it has to be created in the program
- To do so you need a JavaScript statement known as the variable declaration
varcreates a global variable (can be called by entire program and is recognized by all of it)letcreates a local variable (only used in code block where it was created)
Global Variable
- A Global variable can be used throughout the entire program
Local Variable
- A Local Variable can only be used inside of the code block where it has been created (e.g. function parameter)
Data Types
- In addition to text, variables can store different types of information depending on program need
- The type of information found inside of a variable is known as the data type
- Javascript supports the follow data types
- Numeric
- String
- Boolean
- Null
Numeric Values
- Any number in standard or scientific notation
- No quotation marks are needed
String Variables
- Contains any group of alphanumeric characters
- Variable values must be in quotes
- If there is numbers it cant be used in math
- String variables would