Chapter 1

Introduction

Chapter Goals

Prerequisites

What Is Programming?

Self Check

  1. What is required to play a music CD on a computer?
  2. Why is a CD player less flexible than a computer?
  3. Can a computer program develop the initiative to execute tasks in a better way than its programmers envisioned?

Answers

  1. A program that reads the data on the CD and sends output to the speakers and the screen.
  2. A CD player can do one thing–play music CDs. It cannot execute programs.
  3. No–the program simply executes the instruction sequences that the programmers have prepared in advance.

The Anatomy of a Computer

Central Processing Unit

A Memory Module with Memory Chips

A Hard Disk

A Motherboard

Schematic Diagram of a Computer

The ENIAC

Self Check

  1. Where is a program stored when it is not currently running?
  2. Which part of the computer carries out arithmetic operations, such as addition and multiplication?

Answers

  1. In secondary storage, typically a hard disk.
  2. The central processing unit.

Machine Code

Self Check

  1. What is the code for the Java virtual machine instruction "Load the contents of memory location 100"?
  2. Does a person who uses a computer for office work ever run a compiler?

Answers

  1. 21 100
  2. No–a compiler is intended for programmers, to translate high-level programming instructions into machine code.

The Java Programming Language

Applets on a Web Page



Self Check

  1. What are the two most important benefits of the Java language?
  2. How long does it take to learn the entire Java library?

Answers

  1. Safety and portability.
  2. No one person can learn the entire library–it is too large.

Becoming Familiar with your Computer

A Shell Window


An Integrated Development Environment


Nested Folders


Self Check

  1. How are programming projects stored on a computer?
  2. What do you do to protect yourself from data loss when you work on programming projects?

Answers

  1. Programs are stored in files, and files are stored in folders or directories.
  2. You back up your files and folders.

File HelloTester.java

Output

   Hello, World!


HelloTester in a Console Window


HelloTester in an IDE


A Simple Program

Syntax 1.1: Method Call

 
object.methodName(parameters)

Example:

 
System.out.println("Hello, Dave!");

Purpose:

To invoke a method of an object and supply any additional parameters

Self Check

  1. How would you modify the HelloTester program to print the words "Hello," and "World!" on two lines?
  2. Would the program continue to work if you omitted the line starting with //?
  3. What does the following set of statements print?
    System.out.print("My lucky number is");
    System.out.println(3 + 4 + 5);

Answers

  1. System.out.println("Hello,");
    System.out.println("World");
  2. Yes–the line starting with // is a comment, intended for human readers. The compiler ignores comments.
  3. The printout is My lucky number is12. It would be a good idea to add a space after the is.

Errors

Self Check

  1. Suppose you omit the // characters from the HelloTester.java program but not the remainder of the comment. Will you get a compile-time error or a run-time error?
  2. How can you find logic errors in a program?

Answers

  1. A compile-time error. The compiler will not know what to do with the word display.
  2. You need to run the program and observe its behavior.

The Compilation Process


The Edit-Compile-Test Loop

Self Check

  1. What do you expect to see when you load a class file into your text editor?
  2. Why can't you test a program for run-time errors when it has compiler errors?

Answers

  1. A sequence of random characters, some funny-looking. Class files contain virtual machine instructions that are encoded as binary numbers.
  2. When a program has compiler errors, no class file is produced, and there is nothing to run.