Chapter 24

Explain the Purpose and Use of Programming Concepts

In this chapter, you discover the purpose and use of programming concepts such as identifiers (variables, constants), containers (arrays, vectors), functions, and objects (properties, attributes, methods). This chapter covers CompTIA IT Fundamentals+ objective 4.3.

Foundation Topics

Identifiers

In programming, an identifier is a name assigned by the user to a program element such as a variable or constant.

Variables

Image

A variable is a value paired with a symbolic name, enabling the program to refer to the name in calculations. For example, if x=3 and y=4, you can use the expression x × y to calculate the result (in base 10) of 12.

Constants

In programming, a constant is a value that cannot be altered by a program. Sometimes, constants are also called literals. Constant types include the following:

Image
  • Integer: A whole number, such as 3.

  • Floating point: A number with a decimal, such as 54.4.

  • Character: A single character, such as ‘z’. Escape sequences, which are used to control printer or display behavior, are also considered characters, such as ‘ ’ (new line). A universal character (Unicode character set or UCS) such as ‘u02C0’ (caron character) is also considered a single character. Character variables are enclosed in single quotes.

  • String: A group of characters, escape sequences, or universal characters, such as “Hello, World!” String constants use double quotes.

Containers

In programming, a container is an object that can store other objects. Typically, the objects in a container include an array or vector to store data as well as objects for viewing, loading, and unloading data from the storage object.

Note

Containers are known as collections in some languages, such as Java.

Arrays

Image

An array is a collection of the same type of object with a fixed maximum size. If an array needs to grow to hold more data, the programmer must allocate a new array that is large enough to hold the additional data and copy the old array’s contents to it. For this reason, an array should be used only if the programmer knows in advance the maximum number of objects it needs to hold.

Vectors

Image

A vector is a resizable collection of the same type of object. Unlike an array, a vector can be resized as needed and new objects can be inserted anywhere into the collection as needed.

Note

For a useful discussion of arrays versus vectors in C++, see https://stackoverflow.com/questions/4424579/stdvector-versus-stdarray-in-c.

Functions

A function is a subroutine or a procedure inside a larger program. Programming languages usually have built-in functions (also known as library functions).

To use a particular set of functions, the programmer must call (request) the function library in the program. For example, to use a mathematical function in C, such as abs (absolute value of an integer) or fmax (larger of two floating-point values), the math.h header (cmath header in C++) must be included in the program. To use an input/output function such as fopen (opens file) or fread (reads from file), the stdio.h header (cstdio header in C++) must be included in a program. To learn more about C++ functions, see https://www.tutorialspoint.com/cplusplus/cpp_functions.htm. To learn more about C functions, see https://www.tutorialspoint.com/cprogramming/c_functions.htm.

A user-defined function is defined at the top of a program. This enables the function to be referred to repeatedly in a program and the compiler to compile the code before it is used. For example, a function called drawshape is defined from the lines of code needed to draw a shape. However, after it is defined, the programmer can simply refer to the function drawshape and the program will locate the function and repeat it.

Image

A function library is a collection of procedures that can be used (called) by other programs. By using a function library, a programmer can reduce development time by not needing to develop new code to perform a particular task. Some function libraries are included in a language, but others can be licensed from third-party developers.

Objects

Image

In programming, an object is a combination of data, its properties, its attributes, and procedures that can be used with that data, also known as methods. Object-oriented programming builds programs from objects.

Note

A useful, illustrated tutorial on object-oriented programming is available at https://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp3_OOP.html.

Properties

The exact meaning of properties in object-oriented programming varies with the language in question. Some languages define an object’s properties as the values associated with an object. For example, in JavaScript, in the variable (var) person, the firstname, lastname, age, and eyecolor can be defined as properties. To use a property, specify person.property.

However, in C#, properties are used to enable read, write, or compute access to a private field. This protects the contents of the data while enabling safe access. The access methods are often referred to as getter/setter.

Attributes

An attribute is a characteristic of an object. For example, a race car object in a racing game could have these attributes: car number, sponsors, driver, fuel type, manufacturer, racing team, tire type, tire pressure, and so on.

Methods

Methods, also known as member functions, define what an object can do. For example, in a racing game, a race car object could have methods such as start race, end race, drive, pit stop, accelerate, steer, brake, crash, and coast.

Exam Preparation Tasks

Review All Key Topics

Review the most important topics in this chapter, noted with the Key Topics icon in the outer margin of the page. Table 24-1 lists these key topics and the page number on which each is found.

Image

Table 24-1 Key Topics for Chapter 24

Key Topic Element

Description

Page Number

Paragraph

Variables

511

List

Constant types

511

Paragraph

Arrays

512

Paragraph

Vectors

512

Paragraph

Function library

513

Paragraph

Objects

513

Define Key Terms

Define the following key terms from this chapter and check your answers in the glossary:

array

attribute

character

container

constants

floating point

function

identifier

integer

methods

object

properties

variables

vector

Practice Questions for Objective 4.3

1. _____ define(s) what programming objects can do. (Fill in the blank.)

  1. Properties

  2. OOP

  3. Methods

  4. Getter/setter

2. An object’s characteristics are called _______. (Fill in the blank.)

  1. methods

  2. attributes

  3. properties

  4. functions

3. You are creating a container to hold employee information. For the current workforce, you need space for 500 records. However, the company is planning to expand its workforce by an unknown amount. What type of object should you create to hold employee information?

  1. Vector

  2. Method

  3. Array

  4. Object

4. You are writing a program in C that will use mathematical functions. You need to include which of the following headers in your program?

  1. abs

  2. cmath

  3. math.h

  4. cstio

5. You are writing a program that needs a complex function. Instead of writing the function yourself, you can use a ___________. (Fill in the blank.)

  1. user-defined function

  2. function library

  3. object

  4. property

6. An attribute is a _________ of an object. (Fill in the blank.)

  1. property

  2. characteristic

  3. method

  4. function

7. After a conference, you are given a list of attributes for an employee. One of the potential attributes is actually a method. Which of the following is it?

  1. Last name

  2. Sign in

  3. Shift

  4. Employee number

8. You need to add a user-defined function to your program. Where should it be located?

  1. Anywhere in the program code

  2. End of the program

  3. Immediately before its first use

  4. Top of the program

9. At the beginning of your program, you enter several lines of code to create a function you call makeshape. To use the same procedure elsewhere in your program, which of the following should you do?

  1. Reenter the code.

  2. Refer to makeshape.

  3. Add a line number for makeshape and use a GOTO statement.

  4. Create a function library file for makeshape.

10. Which of the following is not a constant?

  1. The number 3

  2. A string of characters such as “Good morning”

  3. Current date

  4. The number 23.1

11. You are creating a program that calculates the number of days between the current date and the user’s birthday. Which of the following do you need to set up to get the user’s birthday?

  1. Variable

  2. Constant

  3. Function

  4. Container

12. An object is a combination of all of the following except:

  1. Containers

  2. Properties

  3. Attributes

  4. Procedures

13. Identifiers include which of the following?

  1. Arrays and vectors

  2. Properties and attributes

  3. Variables and constants

  4. Functions and methods

14. In JavaScript, in the variable (var) person, the firstname, lastname, age, and eyecolor can be defined as _________. (Fill in the blank.)

  1. functions

  2. constants

  3. vectors

  4. properties

15. A function is a(n) _________or _________inside a larger program. (Fill in the blanks.)

  1. identifier or container

  2. escape sequence or universal character

  3. subroutine or procedure

  4. conjunction or junction

Your Next Steps (More Certs)

As we said in Chapter 22, “Compare and Contrast Programming Language Categories,” the best way to learn to program is… wait for it… to program! There are many degree programs available, but you can get started by downloading languages, using online interpreters and compilers, and learning to solve problems you run into at work. See Chapter 22 for online resources, including interpreters and compilers where you can practice safely.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset