An Object-Oriented Approach to Programming Logic and Design - Chapter Notes
Introduction
This chapter provides foundational concepts in programming logic and design.
Topics covered include:
Literals, variables, and named constants
Value assignment to variables
Definition and naming of identifiers
Arithmetic operations
Attributes of good program design
Introduction to programming structure
Creating an Application Class
Application: A software system designed for data collection, processing, and presentation to aid decision-making.
Examples of applications include:
Sales dashboard
Hospital reporting system
Student grading system
Inventory management system
National disease monitoring dashboard
Using Literals, Variables, and Named Constants
Programs accept input in various forms:
User input through interactive programs
Data from storage devices in batch processing
Data Types:
Literals (unnamed constants)
Variables
Named constants
Understanding Unnamed, Literal Constants and Their Data Types
Types of literals:
Numeric literals: Numbers without quotation marks.
Example:
613(cannot contain alphabetic characters).String literals: Text enclosed in quotation marks.
Example: "Jenna" (can include digits, punctuation).
Understanding Variables
Variables:
Named memory locations that can hold varying values.
Only store one value at any time.
Pseudocode example of variable use:
Doubling input number:
INPUT number double = number * 2 OUTPUT double
Working with Variables
Declaration Requirements:
Must declare variables before use.
A variable declaration includes the data type and identifier:
Example data types in the text:
numfor numeric datastringfor string data
Sample Variable Declarations:
num mySalarystring myName
Understanding a Variable’s Data Type
Each variable has a data type:
Numeric variable: Holds digits, used in mathematical operations.
Example:
testScore = 96.String variable: Holds text, including digits but not used in arithmetic operations.
Example:
zipcode = "08202".
Assignment Validity:
Correct type assignment only:
Valid:
num taxRate = 2.5Invalid:
num taxRate = "2.5"
Working with Variables (cont’d)
Initializing Variables:
Providing initial values to variables:
Valid examples:
num yourSalary = 14.55string yourName = "Pat"
Garbage Values:
Uninitialized variables contain unknown values known as garbage, which are usually illegal for output.
Naming Variables
Importance of meaningful names for variables.
Names are associated with specific memory addresses.
Assigning Values to Variables
Assignment statement example:
myAnswer = myNumber * 2(performs a calculation and stores the result).
Assignment operator:
=A binary operator that operates from the right to left.
The right side is evaluated before assignment; the left side must be a memory address.
Declaring Named Constants
Similar to variables but assigned a value only once.
Used to identify immutable values during execution.
Improves program clarity and maintainability.
Naming convention: ALL CAPITALS with underscores (e.g.,
SALES_TAX).
Identifier Naming
Identifier: Name of programming objects (class, method, variable).
Common rules:
Can include letters and digits.
Special characters allowed in some languages (e.g.,
_).Cannot start with a digit.
No white space allowed.
Cannot be keywords.
Case sensitivity applies.
Choosing Identifiers
General guidelines for naming identifiers:
Use nouns or nouns combined with adjectives.
Ensure names are meaningful (self-documenting).
Keep names pronounceable.
Minimize abbreviations.
Performing Arithmetic Operations
Standard arithmetic operators:
+(addition)-(subtraction)*(multiplication)/(division)
Valid assignment statements:
someNumber = 2 * 25 / 5someNumber = anotherNumber + 3totalScore = 0totalScore += 10(increment totalScore).
Invalid statements:
3 + 5 = someNumber(left side not a memory location).
Rules of Operator Precedence
Evaluates parentheses first.
Multiplication and division from left to right.
Addition and subtraction from left to right.
Writing Clear Prompts and Echoing Input
Prompts: Messages that request user input, should be clear.
Echoing input: Repeating user input to aid with error identification.
Designing Clear Statements
Example demonstrating prompts and echoes effectively.
Features of Good Program Design
Best practices for maintainable programs include:
Using comments where necessary.
Choosing meaningful identifiers.
Designing clear statements.
Writing clear prompts and echoing input.
Maintaining good programming habits.
Internal documentation: Comments within code for clarity.
External documentation: Documentation outside the program.
An Introduction to Structure
Structures represent basic units of programming logic:
Sequence Structure: Executes actions linearly with no decision-making (e.g., driving directions).
Selection Structure: Chooses a path based on conditions.
Loop Structure: Repeats instructions based on a condition.
Summary
Data values can be stored as literals, variables, and named constants (numeric or string).
Naming identifiers follow specific rules.
Variables are named memory locations allowing changing content.
Data types dictate what values a variable can hold.
Declaration statements specify variable types and identifiers.
Named constants are assigned values once.
Know the four standard arithmetic operations and their precedence.
Practice good programming habits, including comments, meaningful identifiers, and clear statements.
Understand programming structures: sequence, selection, and loop.
Exercise
Write pseudocode that:
Prompts the user for their name and score.
Echoes the input received.
Uses a selection structure to return "Pass" or "Fail" based on score.
Concludes with a "Thank you" message.