0% found this document useful (0 votes)
9 views

Module 3 Guided Notes_Using Abstractions

Uploaded by

woodruem
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Module 3 Guided Notes_Using Abstractions

Uploaded by

woodruem
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Answer Key

Using Abstraction
Guided Notes
Mathematical Expressions
Vocabulary Term Definition
Element A smaller component in a larger system
Constructs The basic building blocks of a program
Sequence The order in which programming statements are executed.
Selection The path a program takes given a choice
Iteration The repeated execution of a section of code
Code Statement A single line of code that performs a specific task

Describe the concept of an algorithm:

Algorithms are any set of instructions for completing a specific task.

Explain the three different types of algorithms:

Natural language Flow charts Pseudocode

Natural language uses the Flow charts are a more Pseudocode is a programming-like
language you speak to formal and visual way to way of presenting algorithms that
communicate the steps of an express algorithms. Each uses some of the structure of a
algorithm and is a good choice flow chart element has a programming language but is
for programmers working on standard meaning. intended for people to read and
teams where some people may be understand. The AP™ Exam uses
unfamiliar with coding. pseudocode for some
However, natural language can programming questions.
sometimes be too vague or have
alternative meanings that may
need to be clarified when
defining algorithms.

Unless Otherwise Noted All Content © 2023 Florida Virtual School. FlexPoint™ is a trademark of Florida
Virtual School.
Answer Key

Natural language Flow charts Pseudocode

Ex) Ex) Ex)

1. Input test grade 1 as


grade1, grade 2 as
grade2, grade 3 as
grade3.
2. Add grade1, grade2, and
grade3 and store the
result in totalPoints.
3. Divide totalPoints by 3
and store the result in
gradeAverage.
4. Display gradeAverage.

Explain the three different types of constructs:

Sequence Selection Iteration

the order in which the path a program takes the repeated execution of a
programming statements given a choice section of code
are executed.

Describe the order in which programs are read:


All programs use the sequence construct since they must execute their lines of code in order. Most
programs also use selection to add choice and iteration to repeat sections of code.

Unless Otherwise Noted All Content © 2023 Florida Virtual School. FlexPoint™ is a trademark of Florida
Virtual School.
Answer Key

Explain the process of swapping variable values:


Suppose you are writing a farming simulation video game. You have cows in a barn and chickens in
a pen. You want your player to be able to swap the cows and chickens, so the cows are in the pen,
and the chickens are in the barn. Neither structure can house both groups of animals at the same
time. How can you accomplish this?

1. To move the cows to the pen and the chickens to the barn, one set of animals must go to a
temporary holding pen while the other is transferred.
2. The cows are moved from the barn to the temporary pen. Now the barn is empty, and the
chickens can move from their pen to the barn.
3. The chickens are all settled, and the cows can move from the temporary pen into the pen
that originally held the chickens.

1 3

2
Explain the role of and provide the symbol for each of the operators:

Review the table to see the way JavaScript uses arithmetic operators.

Purpose Addition Subtraction Multiplication Division Exponentiation

𝑥𝑥 𝑛𝑛
Arithmetic
Operator + - × ÷
JavaScript
Operator + - * / **
JavaScript a+b b−a a*b b/a b ** a
Examples 5 + 10 10 − 5 5 * 10 10 / 5 5 ** 10

Unless Otherwise Noted All Content © 2023 Florida Virtual School. FlexPoint™ is a trademark of Florida
Virtual School.
Answer Key

Detail the steps for using PEMDAS:

Expressions in programming are evaluated from left to right, following the order of operations you
learned in math class. You may have learned the PEMDAS acronym to help remember the order:

P Parentheses ( )

E Exponents **

M Multiplication *

D Division /

A Addition +

S Subtraction -

Explain what the modulus operator calculates:

Modulus is an arithmetic operator that finds and only returns the remainder of the division of two
positive integers. It can be expressed as: a MOD b or a % b.
• In programming, modulus is expressed using the % operator and is evaluated at the
same level as multiplication and division for the order of operations.
• Even numbers modulus 2 evaluate to 0, and odd numbers modulus 2 evaluate to 1.
• Programmers use modulus 2 to figure out if a value is even or odd.

What are the results of the following?

Code Result

13 MOD 2 1
6 MOD 3 0
20 % 6 2
32 % 7 4

Unless Otherwise Noted All Content © 2023 Florida Virtual School. FlexPoint™ is a trademark of Florida
Virtual School.
Answer Key

Calling Functions

Vocabulary Term Definition


Call a code statement that requests a function or method to execute

Functions a set of code used to accomplish a specific task

Methods a set of code that performs a specific task


a set of code to complete a task that is designed to be reused in a
Procedures
program
Parameter input used in a function

Arguments the value that is passed into a function through a parameter


a value that a function returns after it completes executing its code
Return values
block
Modular programming separating the functionality of a program into small components

Describe the role of functions in programming:

Functions help programmers divide a program into smaller parts to create reusable and less complex
code. Writing a function once and using it multiple times reduces the complexity of a program
through easier debugging.

What takes place in the program when a function is called?


Once a function is declared and defined, the program needs to call the function to execute it.
Calling a function makes the program execute the code inside the function. To call a function, you
use its name on a code line, including its parentheses.

For example, winLevel(); calls the function winLevel() and tells the program to execute it now.

Unless Otherwise Noted All Content © 2023 Florida Virtual School. FlexPoint™ is a trademark of Florida
Virtual School.
Answer Key

Identify the parameters in the functions below:


Functions use a special type of variable called a parameter. Parameters are defined during the
function's declaration and are used like variables in the function's code block. Parameters help
programmers write reusable functions without the programmer needing to know any specific input
values ahead of time.

Function Parameters
function calcValue(num1) Num1
function printName(first, last) first, last

Explain the concept of casting:

JavaScript stores user input from the prompt() method as a string. Storing data as a string is
useful for inputs like the current month or a person's name, but it causes a problem when that
input should be a number. Programmers resolve this issue by casting a variable's data type from one
type to another. The Number() method casts a string to a number. Remember that JavaScript is
case-sensitive, so the Number() method must start with an uppercase N.

Identify the casting function in the code below:


let studentGrade1 = Number(prompt("Enter your first grade: "));

The prompt() method is inside the Number() method. The Number() method casts the string
returned by the prompt() methos to the number data type!

What role do return values play in a function?

programmers often need to create functions that return data to be used elsewhere in the program
or as input for another function. Functions do this by using return values. Return values return
data and execution control back to the program.

What is the value that is returned when the code below runs if val1 is 10 and val2 is 7?

Result: If val1 is 10 and val2 is 12 the function will be:

function addNumbers(val1, val2) function addNumbers(10,12)


{
{
let sum = val1 + val2;
return sum; Let sum = 10 + 12;
} Return 22;
}
22 will be the value returned!

Unless Otherwise Noted All Content © 2023 Florida Virtual School. FlexPoint™ is a trademark of Florida
Virtual School.
Answer Key

What takes place when a return is called in a function?

When a program reaches the return value in a function, it stops executing and returns the control
to the main program. The program will not execute any code statements after the return
statement.

Developing Functions

Vocabulary Term Definition


Modularity breaking down a complex program into smaller components

Functional abstraction hides the details of a function from its implementation

Describe how functional abstraction helps programmers:

a programmer may work on a program that needs a user login. Since users log into many
applications, the programmer may create a reusable function for this task and share it with other
programmers to use in their programs. The programmers who reuse the user login code don't need to
know exactly how the function works; they only need to know its name, the inputs, and the
expected output. Reusing a function without knowing its details is called functional abstraction.

Explain how functions help with managing complexity:

Using functional abstraction to create generalized functions helps programmers manage the
complexity of their programs. Defining functions once and reusing them as needed helps make the
code easier to read and update.

Explain how programmers determine what is needed in a program:


in this example, we are creating an airplane reservation system simulation.

The purpose The functions The expected output

Programmers first Next, the programmers map out Then the programmer mocks up an expected
define the purpose, the functionality of the program. output that they want the program to output

Unless Otherwise Noted All Content © 2023 Florida Virtual School. FlexPoint™ is a trademark of Florida
Virtual School.
Answer Key

The purpose The functions The expected output

Ex) Ex) Ex)


Functionality of the system: *****************************************************
The purpose of the A passenger enters their travel Thank you for choosing Blue Skies Airlines!
simulation airline • details. Passenger: Hopper / Grace
reservation system • The system displays a Flight number: 4379
is to help matching flight. Leaving: New York Arriving: London On:
passengers find The system asks November 30, 2024
and book a flight. • the passenger to enter a Seat Number: 4B
seat number. Enjoy your flight!
• The system prints a ticket. *****************************************************

What is meant by logical flow in a function?


When creating a program, programmers map out the logical flow of the program and then
determine which parts should be functions.
Ex)

** For this simulation, the display flight information, user input for seat number, and print ticket
processes are good candidates for functions because the program can reuse the processes for each
passenger. **

How are the variables in a program determined?


The user inputs can help identify the variables the program should use:
Ex)

Unless Otherwise Noted All Content © 2023 Florida Virtual School. FlexPoint™ is a trademark of Florida
Virtual School.
Answer Key

What value is returned as a result of the code below?

PROCEDURE calcAverage(a, b, c) This function takes 3 numbers, adds


{ them up, then divides by 3. It would
answer ← (a + b + c) / 3; return the average of any 3 numbers
RETURN (answer)
that would be used.
}

Programming Libraries

Vocabulary Term Definition


collections of prewritten code that programmers can use for
Libraries
their programs
Modules a self-contained set of code that has one or more functions

Application program interfaces the details and directions for using methods from libraries

Explain how libraries are developed:

Let's say you're writing a program for a website that calculates the total for an online shopping
cart, but you also want to add a currency conversion option for your customers that live in other
countries. You could write a currency conversion function yourself. However, since currency
conversion is a common task, you could search for a currency conversion programming library
instead.

If you find a library that fits your needs, you could use that code in your program. Finding a
currency conversion library can save you time because you don't need to create the code. But
remember, when programmers use code authored by another programmer, they must also give credit
to the original programmer.

Software languages also supply their own standard libraries for common tasks. For example, the
Math module in JavaScript provides functions for most common mathematical tasks.

Unless Otherwise Noted All Content © 2023 Florida Virtual School. FlexPoint™ is a trademark of Florida
Virtual School.
Answer Key

Explain the purpose of each of the Math functions:

Function Purpose Example


console.log(Math.abs(-4.6));
Math.abs(x) Returns the absolute value of x
Output: 4.6
console.log(Math.PI);
Math.PI Returns the constant PI
Output: 3.141592653589793
Returns the value of x to the console.log(Math.pow(4,2));
Math.pow(x,y) power of y
Output: 16
Returns x rounded to the nearest console.log(Math.round(4.7);
Math.pow(x) integer Output: 5
console.log(Math.sqrt(144));
Math.sqrt(x) Returns the square root of x
Output: 12

What are some different uses for libraries?

What may start as a function in a programmer's code may also become a library that can help
other programmers. Let's explore some standard libraries that programmers may use to help reduce
their coding time and manage the complexity of their programs.

Different types of libraries:


Maps | Animations | Data Visualization| Database Queries | Audio | Logic | Numerical | Procedural

What is the purpose of an API?

Application program interfaces (APIs) provide documentation details for libraries and include when
to use a library and the way the library works. This documentation instructs programmers on using
the library in their code and
should include a name, description,
introduction, background, and
usage examples.

In summary: ***APIs are the


instructions provided to help
programmers understand a
library's function and how to use
it in their own programs.***

Ex)

Unless Otherwise Noted All Content © 2023 Florida Virtual School. FlexPoint™ is a trademark of Florida
Virtual School.

You might also like