Chapter 1: Introduction to Computers
Why Program?
General Importance
Computers are versatile tools capable of performing a range of functions such as:
Spreadsheets
Databases
Word processing
Games
And many other applications.
The adaptability of computers stems from their programming capabilities.
Computer programmers are the professionals who implement these multifunctional programs.
Aspects of Program Design
Designing a computer program involves several critical factors:
The logical flow of instructions
The mathematical procedures involved
The layout of programming statements
The visual appearance of screens within the application
The presentation of information to the user
Overall user friendliness of the program
The availability of manuals, help systems, and other documentation forms.
Challenges in Programming
Programs must be analytically correct, hence they seldom run perfectly on the first try.
Programmers must continuously engage in:
Analyzing code
Experimenting with solutions
Correcting issues
Redesigning flawed parts of the program.
Programming languages have specific rules, known as syntax, that must be adhered to strictly.
Computer Systems: Hardware
Overview of Computer Hardware
Computer hardware refers to the tangible components of a computer.
Key hardware components include:
Central Processing Unit (CPU)
Main memory (RAM)
Secondary storage devices
Input and output devices.
Computer Block Diagram
Components are interconnected via buses:
Address bus
Data bus
Control bus.
Central Processing Unit (CPU)
The CPU is crucial for processing program information and consists of:
Arithmetic Logic Unit (ALU) for performing mathematical operations.
Control Unit for managing the flow of information.
The CPU goes through a cycle called fetch, decode, execute to process instructions.
Fetch: The control unit retrieves the next instruction from RAM, represented in binary encoding.
Decode: The control unit interprets the instruction and generates appropriate signals.
Execute: Signals are dispatched to the corresponding component (like ALU or output devices) to perform the operation.
Computer Systems: Main Memory
Random-Access Memory (RAM)
Commonly known as RAM, it retains:
Currently running programs
Data that these programs utilize.
Structured in bytes, where:
A byte is composed of 8 bits (binary digits).
Each bit can be:
1 (on) or 0 (off);
Bits form patterns that represent characters or numbers.
Each byte is given a unique address in memory.
RAM is volatile, meaning its contents disappear when power is lost; it can be visually represented as a grid of cells.
Computer Systems: Secondary Storage Devices
These devices store data for longer periods, thus being non-volatile.
Common types include:
Disk drives
Solid State Drives (SSDs)
External drives
CDs, DVDs, and USB drives.
Computer Systems: Input Devices
Input devices collect data from the environment, such as:
Keyboards
Mice
Scanners
Digital cameras.
Computer Systems: Output Devices
Output devices present data from the computer, including:
Monitors
Printers.
Devices like disk drives can serve both input and output functions, hence termed I/O devices.
Computer Systems: Software
Types of Software
Software encompasses the programs that operate on a computer and can be classified into:
Operating Systems: Manages hardware and executes tasks.
Application Software: Provides user-oriented functionalities.
Operating Systems
An operating system consists of a collection of programs that manage computer hardware and processes, supporting multitasking capabilities, such as:
Unix
Linux
macOS
Windows.
It allocates hardware resources and CPU attention via time-sharing.
Application Software
The application software is intended to provide usability for tasks like:
Spreadsheets
Word processing
Games and more.
Programming Languages
Definition of a Program
A program consists of a series of instructions for the computer to follow in order to accomplish specific tasks.
A programming language serves as the medium for writing these instructions.
Algorithms
An algorithm is a precisely defined sequence of steps for completing a task.
Instructions must be converted into machine language, which uses binary numbers (base 2), comprised of only 0s and 1s.
Machine Language
Each CPU type has its unique machine language:
Motorola 68000 series
Intel x86 series
ARM processors, etc.
An example of a machine language instruction: 1011010000000101.
Transition from Machine Language to Higher Languages
Historically, programming was done in machine language.
As programming needs evolved, higher-level languages were created, starting with assembler, which eased the coding process but was still dependent on the CPU architecture.
Later, high-level languages emerged that were not CPU-dependent.
Common High-Level Programming Languages
These languages include:
Java
C
C++
C#
Python
Ruby
JavaScript
PHP
Rust
Go
BASIC.
Common Language Elements
Key concepts found across programming languages include:
Keywords
Operators
Punctuation
User-defined identifiers
Strict syntactic conventions.
Java Sample Programs
HelloWorld Example (Java)
public class HelloWorld {
public static void main(String[] args) {
String message = "Hello World";
System.out.println(message);
}
}
HelloWorld Example (C++)
#include <iostream>
#include <string>
int main() {
string message = "Hello World";
cout << message << endl;
}
Keywords in Java
Java keywords consist of important reserved words such as: public, class, static, and void, which are all lower case due to Java's case sensitivity.
Keywords cannot be used as identifiers defined by the programmer.
Statements in Java terminate with a semicolon, although not every line ends with a statement.
The objective is to learn the correct punctuation for Java coding.
Lines vs. Statements in Code
Understanding the distinction between a line of code and a statement is crucial:
E.g.,
System.out.println(message);can span multiple lines but remains a single statement.
A statement is a complete instruction for the computer.
Variables in Java
Definition and Creation
Variables in Java denote memory locations, often referred to as fields.
They are established by assigning them a programmer-defined identifier, for example:
int hours = 40;(wherehoursis the variable representing an integer value of 40).
Representation and Storage
Variables are merely names assigned for ease of referencing memory locations.
The placement of variable values into memory is decided by the Java Virtual Machine (JVM).
Compiler and Java Virtual Machine (JVM)
Programming Statements and Source Code
Java programming statements formulated by a programmer are categorized as source code, typically edited and saved in a text file with a .java extension.
Compilation Process
The compiler translates this source code into executable formats:
Identifies syntax errors during compilation.
Generates a separate file containing translated instructions.
Byte Code and Execution
The Java compiler produces bytecode which is the machine language of the JVM, not the CPU directly.
Files generated end with a .class extension and require the JVM for execution.
JVM serves as a microprocessor emulator executing the bytecode one instruction at a time, often termed as an interpreter.
Consequently, Java is known as an interpreted language.
Program Development Process
Clearly define the objectives of the program.
Visualize the execution of the program on the computer.
Utilize design tools for creating a program model.
Validate the model for errors.
Code and compile the program.
Debug any issues and repeat coding as necessary.
Run the program with test data inputs.
Rectify runtime errors that arise during execution. Repeat debugging as needed.
Validate the final results produced by the program.
Software Engineering
Overview
Software engineering involves all stages of software creation.
Key responsibilities include:
Designing
Coding
Testing
Debugging
Documenting
Modifying
Maintaining.
Software Development Components
Engineers prepare various materials, like:
Specifications for programs
Examples of inputs and outputs
Data flow diagrams
Pseudocode.
Team Collaboration
Software engineering often requires collaboration across teams. Tasks are divided into subtasks amongst team members, allowing focus on respective project areas.
Procedural Programming
Definition
Earlier programming paradigms employed procedural languages where procedures consist of statements that perform specific tasks on global data.
Data is often passed between procedures, leading to potential challenges in maintaining global data formats.
Object-Oriented Programming
Core Concepts
Object-oriented programming emphasizes the creation of objects that encapsulate data and the procedures manipulating it.
Data elements are termed attributes.
Procedures linked to those data are termed methods.
Encapsulation and Data Hiding
Encapsulation combines data and behavior within objects.
Data hiding refers to restricting direct access to attributes, with interaction permitted only through methods, establishing a programming interface.
Java History
The origins date back to 1991 when the Green Team at Sun Microsystems initiated a project for a cross-platform language named Oak.
The significant showcase was the HotJava web browser introduced in 1995, evidencing Java's cross-platform capability.
Java Applications and Applets
Java programs can be categorized as:
Applications: Self-contained programs that can be executed independently.
Applets: Smaller programs meant to run within a Java-enabled web browser, emphasizing a strict security model.