Big Java 5

Chapter 1 – Introduction

Chapter Goals

""

Computer Programs

Self Check 1.1

What is required to play a music CD on a computer?

Self Check 1.2

Why is a CD player less flexible than a computer?

Self Check 1.3

What does a computer user need to know about programming in order to play a video game?

The Anatomy of a Computer

Central Processing Unit

CPU

Figure 1 Central Processing Unit

A Hard Disk

hard disk

Figure 2 Hard Disk

Schematic Diagram of a Computer

Figure 3 Schematic Design of a Personal Computer

Self Check 1.4

Where is a program stored when it is not currently running?

Self Check 1.5

Which part of the computer carries out arithmetic operations, such as addition and multiplication?

Self Check 1.6

A modern smartphone is a computer, comparable to a desktop computer. Which components of a smartphone correspond to those shown in Figure 5 (Schematic Diagram of a Computer)?

Computers are Everywhere

transit card

This transit card contains a computer.

The ENIAC

Eniac computer

The Eniac

The Java Programming Language

Applet on a Web Page

Applet on a Web Page

Figure 4 An Applet for Visualizing Molecules Running in a
Browser Window (http://jmol.sourceforge.net/)

Java Versions

Version Year Important New Features
1.0 1996  
1.1 1997 Inner classes
1.2 1998 Swing, Collections framework
1.3 2000 Performance enhancements
1.4 2002 Assertions, XML support
5 2004 Generic classes, enhanced for loop, auto-boxing, enumerations, annotations
6 2006 Library improvements
7 2011 Small language changes and library improvements

Self Check 1.7

What are the two most important benefits of the Java language?

Self Check 1.8

How long does it take to learn the entire Java library?

Becoming Familiar with Your Programming Environment

Becoming Familiar with Your Programming Environment

  1. Start the Java development environment.
  2. Write a simple program.
  3. Run the program.
  4. Organize your work.

Becoming Familiar with Your Programming Environment

IDE

Figure 5 Running the HelloPrinter Program in an Integrated Development Environment

Becoming Familiar with Your Programming Environment

Console window

Figure 6 Running the HelloPrinter Program in a Console Window

Becoming Familiar with Your Programming Environment

 

From Source Code to Running Program

Figure 7 From Source Code to Running Program

Becoming Familiar with Your Programming Environment

directory structure

Figure 8 A Folder Hierarchy

Self Check 1.9

Where is the HelloPrinter.java file stored on your computer?

Self Check 1.10

What do you do to protect yourself from data loss when you work on programming projects?

Section_5/HelloPrinter.java

Your browser does not support this feature

Analyzing Your First Program: Class Declaration

Analyzing Your First Program: Methods

Analyzing Your First Program: main Method

Analyzing Your First Program: Statements

Analyzing Your First Program: Method Call

Syntax 1.1 Java Program

syntax diagram

Analyzing Your First Program: Strings

Analyzing Your First Program: Printing

Self Check 1.11

How do you modify the HelloPrinter program to greet you instead?

Self Check 1.12

How would you modify the HelloPrinter program to print the word "Hello" vertically?

Self Check 1.13

Would the program continue to work if you replaced line 7 with this statement?
System.out.println(Hello);

Self Check 1.14

What does the following set of statements print?
System.out.print("My lucky number is");
System.out.println(3 + 4 + 5);

Self Check 1.15

What do the following statements print?
System.out.println("Hello");
System.out.println("");
System.out.println("World");

Errors

Errors

Self Check 1.16

Suppose you omit the "" characters around Hello, World! from the HelloPrinter.java program. Is this a compile-time error or a run-time error?

Self Check 1.17

Suppose you change println to printline in the HelloPrinter.java program. Is this a compile-time error or a run-time error?

Self Check 1.18

Suppose you change main to hello in the HelloPrinter.java program. Is this a compile-time error or a run-time error?

Self Check 1.19

When you used your computer, you may have experienced a program that "crashed" (quit spontaneously) or "hung" (failed to respond to your input). Is that behavior a compile-time error or a run-time error?

Self Check 1.20

Why can't you test a program for run-time errors when it has compiler errors?

Problem Solving: Algorithm Design

An Algorithm for Solving an Investment Problem

An Algorithm for Solving an Investment Problem - continued

Pseudocode

From Algorithm to Programs

Program Development Process

Self Check 1.21

Suppose the interest rate was 20 percent. How long would it take for the investment to double?

Self Check 1.22

Suppose your cell phone carrier charges you $29.95 for up to 300 minutes of calls, and $0.45 for each additional minute, plus 12.5 percent taxes and fees. Give an algorithm to compute the monthly charge for a given number of minutes.

Self Check 1.23

Consider the following pseudocode for finding the most attractive photo from a sequence of photos:
Pick the first photo and call it "the best so far".
For each photo in the sequence
   If it is more attractive than the "best so far"
      Discard "the best so far".
      Call this photo "the best so far".
The photo called "the best so far" is the most attractive photo in the sequence. 
Is this an algorithm that will find the most attractive photo?

Self Check 1.24

Suppose each photo in Self Check 23 had a price tag. Give an algorithm for finding the most expensive photo.

Self Check 1.25

Suppose you have a random sequence of black and white marbles and want to rearrange it so that the black and white marbles are grouped together. Consider this algorithm
Repeat until sorted
    Locate the first black marble that is preceded by a white marble, and switch them.
What does the algorithm do with the sequence W B W B B? Spell out the steps until the algorithm stops.

Self Check 1.26

Suppose you have a random sequence of colored marbles. Consider this pseudocode:
Repeat until sorted
   Locate the first marble that is preceded by a marble of a different color, and switch them.
Why is this not an algorithm?