Chapter One
1.1 — Introduction
The term Programming means to create (or develop) software, which is also called a program. In basic terms, software contains the instructions that tell a computer—or a computerized device—what to do.
Software developers create software with the help of powerful tools called programming languages.
Each language was invented for a specific purpose—to build on the strengths of a previous language, for example, or to give the programmer a new and unique set of tools.
1.2 — What Is a Computer?
A computer includes both hardware and software. In general, hardware comprises the visible, physical elements of the computer, and software provides the invisible instructions that control the hardware and make it perform specific tasks. Knowing computer hardware isn’t essential to learning a programming language, but it can help you better understand the effects that a program’s instructions have on the computer and its components.
A computer consists of the following major hardware components:
A central processing unit (CPU)
Memory (main memory)
Storage Devices (such as disk and CDs)
Input devices (such as the mouse and keyboard)
Output devices (such as monitors and printers)
Communication devices (such as modems and networks interface cards)
A computer’s components are interconnected by a subsystem called a bus.
The bus is built into the computer’s motherboard, which is a circuit case that connects all of the parts of a computer together.
1.2.1 — Central Processing Unit
The central processing unit (CPU) is the computer’s brain. It retrieves instructions from memory and executes them. The CPU usually has two components: a control unit and an arithmetic/logic unit. The control unit controls and coordinates the actions of the other components. The arithmetic/logic unit performs numeric operations (addition, subtraction, multiplication, division) and logical operations (comparisons).
Today’s CPUs are built on small silicon semiconductor chips that contain millions of tiny electric switches, called transistors, for processing information.
Every computer has an internal clock, which emits electronic pulses at a constant rate. These pulses are used to control and synchronize the pace of operations. A higher clock speed enables more instructions to be executed in a given period of time. The unit of measurement of clock speed is the hertz(Hz), with 1 hertz equaling 1 pulse per second.
The core is the part of the processor that performs the reading and executing of instructions. In order to increase CPU processing power, chip manufacturers are now producing CPUs that contain multiple cores.
1.2.2 — Bits and Bytes
A computer is really nothing more than a series of switches. Each switch exists in two states: on or off. If the switch is on, its value is 1. If the switch is off, its value is 0. These 0s and 1s are interpreted as digits in the binary number system and are called bits(binary digits). The minimum storage unit in a computer is a byte.
Data of various kinds, such as numbers and characters, are encoded as a series of bytes. The computer system performs the encoding and decoding automatically, based on the encoding scheme. An encoding scheme is a set of rules that govern how a computer translates characters, numbers, and symbols into data the computer can actually work with. Most schemes translate each character into a predetermined string of bits.
A computer’s storage capacity is measured in bytes and multiples of bytes.
1.2.3 — Memory
A computer’s memory consists of an ordered sequence of bytes for storing programs as well as data that the program is working with. You can think of memory as the computer’s work area for executing a program. A program and its data must be moved into the computer’s memory before they can be executed by the CPU.
Every byte in the memory has a unique address. The address is used to locate the byte for storing and retrieving the data. Since the bytes in the memory can be accessed in any order, the memory is also referred to as random-access memory (RAM).
The more RAM a computer has, the faster it can operate.
A memory is never empty, but its initial content may be meaningless to your program. The current content of a memory byte is lost whenever new information is placed in it.
Memory chips are built the same as CPU chips. Compared to CPU chips, memory chips are less complicated, slower, and less expensive.
1.2.4 — Storage Devices
A computer’s memory (RAM) is a volatile form of data storage: any information that has been stored in memory (i.e., saved) is lost when the system’s power is turned off. Programs and data are permanently stored on storage devices and are moved, when the computer actually uses them, to memory, which operates at much faster speeds than permanent storage devices can.
Disks: A computer usually has at least one hard disk drive. Hard disks are used for permanently storing data and programs. Hard disks are usually encased inside the computer, but removable, hard disks are also available.
CDs and DVDs: CD stands for compact disk. There are two types of `CD drives: CD-R and CD-RW. A CD-R is for read only permanent storage; the user cannot modify its contents once they are recorded. A CD-RW can be used like a hard disk; that is, you can write data onto the disc, and then overwrite that data with new data.
DVD stands for digital versatile disc or digital video disc. A DVD can hold more information than a CD. Like CDs, there are two types of DVDs: DVD-R and DVD-RW.
USB Flash Drives: Universal serial bus (USB) connectors allow the user to attach many kinds of peripheral devices to the computer. A USB flash drive is a device for storing and transporting data. It acts like a portable disc drive that can be plugged into your computer’s USB port.
1.2.5 — Input and Output Devices
Input and output devices let the user communicate with the computer.
The Keyboard:
Function Keys — Their function depends on the software currently being used.
A modifier key — is a special key (such as shift, alt, ctrl) that modifies the normal action of another key when the two are pressed simultaneously.
Numeric keypad — entering the number quickly.
Arrow keys — used to move the mouse pointer up, down, left, and right.
Insert, Delete, Page Up, and Page Down keys are used for inserting text and objects, deleting text and objects, and moving up or down through a document one screen at a time.
The Mouse:
It’s used to move a graphical pointer called a cursor around the screen or to click on-screen objects to trigger them to perform an action.
The Monitor:
It displays the information (text and graphics). The screen resolution and dot pitch determine the quality of the display.
The screen resolution specifies the number of pixels in horizontal and vertical dimensions of the display device. Pixels (short for “picture elements”) are tiny dots that form an image on the screen. The Dot Pitch is the amount of space between pixels.
1.3 — Programming Languages
1.3.1 — Machine Language
A computer’s native language, which differs among different types of computers, is its machine language— a set of built-in primitive instructions. These instructions are in the form of binary code, so if you want to give a computer an instruction in its native language, you have to enter the instruction as binary code.
1.3.2 — Assembly Language
Assembly Language was created in the early days of computing as an alternative to machine languages. It uses a short descriptive word, known as mnemonic, to represent each of the machine-language instructions,
Assembly languages were developed to make programming easier. However, because the computer cannot execute assembly language, another program—called an assembler—is used to translate assembly-language programs into machine code.
Writing in assembly requires that you know how the CPU works. It’s a low level language, because assembly language is close in nature to machine language and is machine dependent.
1.3.3 — High-Level Language
High-level languages are English-like and easy to learn and use. They are also platform independent. The instructions in a high-level programming language are called statements.
A program written in a high-level language is called a source program or source code. Because a computer cannot execute a source program, a source program must be translated into machine code for execution. The translation can be done using another programming tool called an interpreter or a compiler.
An interpreter reads one statement from the source code, translates it to the machine code or virtual machine code, and then executes it right away. Note that a statement from the source code may be translated into several machine instructions.
A compiler translates the entire source code into a machine-code file, and the machine-code file is then executed.
1.4 — Operating Systems
The operating System manages and controls a computer’s activities.
The popular operating systems for general-purpose computers are Microsoft Windows, Mac OS, and LInux. Application programs, such as a Web browser or a word processor, cannot run unless and operating system is installed and running on the computer.
Controlling and Monitoring System Activities
Allocating and Assigning System Resources
Scheduling Operations
Multiprogramming: allows multiple programs to run simultaneously by sharing the same CPU. The CPU is much faster than the computer’s other components.
Multithreading: allows a single program to execute multiple tasks at the same time.
Multiprocessing or parallel processing: uses two or more processors together to perform subtasks concurrently and then combine solutions of the subtasks to obtain a solution for the entire task.
1.5 — Java, the World Wide Web, and Beyond
Java has become popular because you can write a program once and run it anywhere.
Java is:
simple
object-oriented
distributed
interpreted
robust
secure
architecture neutral
portable
high performance
multithreaded
dynamic
Java initially became attractive because Java programs can be run from a Web browser. Such programs are called applets.
Applets make the Web responsive, interactive, and more fun to use. Applets are embedded in an HTML file.
A rich internet application (RIA) is a Web application designed to deliver the features and functions normally associated with desktop applications.
Java is a versatile programming language: you can use it to develop applications for desktop computers, servers, and small handheld devices.
1.6 — The Java Language Specification, API, JDK, and IDE
The Java language specification is a technical definition of the Java programming language’s syntax and semantics.
The application program interface (API), also known as library, contains predefined classes and interfaces for developing Java programs.
Java Standard Edition (Java SE) to develop client-side applications. They can run standalone or as applets running from a Web browser.
Java Enterprise Edition (Java EE) to develop server-side applications, such as Java servlets, JavaServer Pages (JSP), and JavaServer Faces (JSF).
Java Micro Edition (Java ME) to develop applications for mobile devices, such as cell phones.
1.7 — A SImple Java Program
The word console is an old computer term that refers to the text entry and display device of a computer. Console input means to receive input from the keyboard, and console output means to display output on the monitor.
Every Java program must have at least one class. Each class has a name. By convention, class names start with an uppercase letter.
The program is executed from the main method. A method is a construct that contains statements.
String is a programming term meaning a sequence of characters. A string must be enclosed in double quotation marks. Every statement in Java ends with a semicolon (;), known as the statement terminator.
Reserved words, or keywords, have a specific meaning to the compiler and cannot be used for other purposes in the program.
In Java, comments are preceded by two slashes (//) on a line, called a line comment, or enclosed between /* and / on one or several lines, called a block comment or paragraph comment. when the compiler sees //, it ignores the all text after .. on the same line. When it sees /* it scans for the next */ and ignored any text between /* and */.
A pair of curly braces in a program for a block that groups the program’s components.
Blocks can be nested, meaning that one block can be placed within another.
Java is case-sensitive.
1.8 — Creating, Compiling, and Executing a Java Program
You save a Java program in a .java file and compile it into a .class file. The .class file is executed by the Java Virtual Machine.
A java compiler translated a JAva source file into a Java bytecode file.
If there aren’t any syntax errors, the compiler generates a bytecode file with a .class extension. Thus, the preceding command generates a file names ClassName.class.
The Java language is a high-level language, but Java bytecode is a low-level language. The bytecode is similar to machine instructions but is architecture neutral and can run on any platform that has a Java Virtual Machine (JVM).
Java source code is compiled into Java bytecode and Java bytecode is interpreted by the JVM.
To execute a Java program is to run the program’s bytecode. You can execute the bytecode on any platform with a JVM, which is an interpreter.
1.9 — Programming Style and Documentation
Programming style deals with what programs look like.
Documentation is the body of explanatory remarks and comments pertaining to a program. Programming style and documentation are as important as coding.
1.9.1 — Appropriate Comments and Comment Styles
Include a summary at the beginning of the program that explains what the program does, its key features , and any unique techniques it uses.
Use javadoc comments (/** … */) for commenting on an entire class or an entire method. These comments must precede the class or the method header in order to be extracted into a javadoc HTML file.
1.9.2 — Proper Indentation and Spacing
Indentation is used to illustrate the structural relationships between a program’s components or statements.
Indent each subcomponent or statement at least two spaces more than the construct within with it is nested. A single space should be added on both sides of a binary operator.
1.9.3 — Block Styles
Next-line style
End-of-line style
You should use a block style consistently—mixing styles is not recommended. The end-of-line style is preferred.
1.10 — Programming Errors
Syntax Errors: Errors that are detected by the compiler are called syntax errors or compile errors.
Runtime Errors: are errors that cause a program to terminate abnormally. They occur while a program is running if the environment detect an operation that is impossible to carry out.
Logic Errors: occur when a program does not perform the way it was intended to.