Chapter 1: Computers, Programs, and Java - Quick Reference

Computer Basics

  • Hardware: physical components (CPU, memory).

  • Software: instructions that control hardware.

  • Example: A calculator app uses hardware to operate.

Major Hardware Components

  • Storage (Disks, SSDs)

  • Memory (RAM)

  • CPU (Central Processing Unit)

  • Communication (Network Cards)

  • Input (Keyboard, Mouse)

  • Output (Monitor, Printer)

Central Processing Unit (CPU)

  • The brain of the computer; executes instructions including arithmetic and logic.

  • Measured in Hertz (GHz); 1 GHz=109 cycles per second1\ \text{GHz} = 10^9\ \text{cycles per second}. Modern CPUs have multiple cores for parallel processing.

  • Uses an internal clock for synchronization of tasks.

  • Examples: Game physics/graphics calculations; smartphone app tasks.

Bits and Bytes

  • Bit: the smallest data unit (0 or 1).

  • Byte: 8 bits.

  • ASCII: the letter 'A' is represented as 01000001 (binary).

  • Storage capacities: 1 KB=1024 bytes1\ \text{KB} = 1024\ \text{bytes}; 1 MB=1024 KB1\ \text{MB} = 1024\ \text{KB}; 1 GB=1024 MB1\ \text{GB} = 1024\ \text{MB}; 1 TB=1024 GB1\ \text{TB} = 1024\ \text{GB}.

  • A text file containing "Hello, World!" uses 13 bytes: 13 bytes13\ \text{bytes}.

Memory (RAM)

  • RAM is volatile memory used to store data and programs that are actively used by the CPU.

  • Function: provides quick read/write access, faster than storage.

  • Volatility: data is lost when power is turned off.

  • Example: multiple browser tabs’ content stored in RAM for fast switching.

Storage Devices

  • Types: Magnetic disks, SSDs, USB drives.

  • Permanent data storage; larger capacities than RAM.

  • Example: external SSD stores backups.

  • RAM is faster than storage devices but has less capacity.

Input/Output Devices

  • Input: keyboard, mouse, scanner, microphone.

  • Output: monitor, printer, speakers, projector.

  • Example: scanner to digitize a photo and display on a monitor.

Communication Devices

  • Enable computers to communicate over networks or the Internet.

  • Examples: Modems, network interface cards, Wi-Fi adapters.

  • Functions: data exchange on a network; Internet access; support wired (Ethernet) and wireless (Wi‑Fi, Bluetooth).

Programming Languages

  • Machine Language: binary code directly executed by the CPU.

  • Assembly Language: mnemonics; hardware-specific.

  • High-Level Language: human-readable and portable (e.g., Java, Python).

Machine Language

  • Binary instructions (e.g., 1010) directly understood by computers.

Assembly Language

  • Mnemonics for machine instructions; requires an assembler to convert to binary.

High-Level Language

  • Examples: Java, Python, C++.

  • Easier to learn and use; portable across platforms.

  • Example: area calculation in a high-level language: area=3.14×r2\text{area} = 3.14 \times r^2

Translating High-Level Code

  • Compiler: converts code to machine language.

  • Interpreter: executes code line-by-line.

  • Example: Java uses a compiler; Python uses an interpreter.

Java

  • Invented by James Gosling at Sun Microsystems in 1995.

  • Widely used in Web (backend frameworks like Spring), Mobile (Android), and Enterprise applications.

  • Java is a versatile language for software on mobile devices, desktops, and servers.

Applets

  • Java programs can run from a Web browser; applets are embedded in HTML.

Java Tools

  • API (Application Programming Interface): Java syntax defined in the language specification; library defined in the Java API.

  • JDK (Java Development Kit): software for developing and running Java programs.

  • IDEs (NetBeans, Eclipse): integrated development environments for rapid development.

A Simple Java Program

  • A simple program prints a message to the console.

  • Output: Welcome to Java!

Caution!!

  • Java source programs are case sensitive.

Programming Style and Documentation

  • Good style and proper documentation improve readability and reduce errors.

  • 1. Appropriate Comments and Comment Styles

    • Line Comment: //

    • Block Comment: /* … */

    • Javadoc Comment: /** … */

    • Javadoc comments precede the class or method and can be extracted into HTML.

  • Example: // This function calculates the factorial of a number.

  • Why: Clarifies intent and aids maintenance.

Naming Conventions

  • Variables: camelCase (e.g., studentAge).

  • Classes: PascalCase (e.g., StudentDetails).

  • Constants: UPPERCASE (e.g., MAXVALUE).

  • Why: Improves readability and reduces confusion.

Proper Indentation

  • Use consistent indentation (e.g., 4 spaces per level).

  • Align blocks, especially in loops and conditionals.

Block Styles

  • A block is a group of statements surrounded by braces.

  • Styles: next-line style vs end-of-line style.

Types of Errors

  • Syntax Errors: violate language syntax (e.g., missing semicolon, misspelled keywords).

  • Runtime Errors: occur during program execution (e.g., division by zero; array index out of bounds).

  • Logic Errors: run but produce incorrect results; no runtime crash.

Common Errors

  • 1) Missing Braces: ensure a closing brace follows an opening brace; many IDEs auto-insert.

  • 2) Missing Semicolons: every statement ends with a semicolon (;).

  • 3) Missing Quotation Marks: strings must be enclosed in double quotation marks.

  • 4) Misspelling Names: Java is case-sensitive; ensure correct casing.

Java IDEs

  • IDE = Integrated Development Environment for coding, debugging, and testing.

  • NetBeans: easier for beginners and small projects.

  • Eclipse: more powerful for advanced/enterprise projects.

Developing Java Programs Using Eclipse

  • Creating a Java Project: New Java Project; specify name and location; choose JRE; set project layout.

Creating a Java Class

  • New Java Class dialog: specify class name, modifiers; avoid default package when possible.

  • Example: Welcome class with a main method stub.

Compiling and Running a Class

  • In Eclipse, you compile and run a class; output appears in the Console window (e.g., Welcome to Java!).

Resources

  • Liang, Y. D. (2011). Introduction to Java programming, comprehensive. Pearson.