Computers are programmed to perform many different tasks.
Computers execute very basic instructions in rapid succession.
A computer program is a sequence of instructions and decisions.
Programming is the act of designing and implementing computer programs.
The physical computer and peripheral devices are collectively called the hardware.
The programs the computer executes are called the software.
Self Check 1.1
What is required to play a music CD on a computer?
Answer: A program that reads the data on the CD and sends output to the speakers and the
screen.
Self Check 1.2
Why is a CD player less flexible than a computer?
Answer: A CD player can do one thing — play music CDs. It cannot execute programs.
Self Check 1.3
What does a computer user need to know about programming in order to play a video game?
Answer: Nothing
The Anatomy of a Computer
Central processing unit
(CPU)
performs
Program control
Data processing
Storage
Memory (Primary storage)
Secondary storage
Peripherals
To interact with human users
Networks
Central Processing Unit
Figure 1 Central Processing Unit
A 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?
Answer: In secondary storage, typically a hard disk.
Self Check 1.5
Which part of the computer carries out arithmetic operations, such as addition
and multiplication?
Answer: The central processing unit.
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)?
Answer: A smartphone has a CPU and memory, like any computer. A few smartphones have keyboards. Generally, the touchpad is used instead of a mouse. Secondary storage is in the form of a solid state drive. Of course, smartphones have a display, speaker, and microphone. The network connection uses the wireless radio to connect to a cell tower.
Computers are Everywhere
This transit card contains a computer.
The ENIAC
The Eniac
The Java Programming Language
Safe
Portable
Platform-independent
Distributed as instructions for a virtual machine
Vast set of library packages
Designed for the Internet
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?
Answer: Safety and portability.
Self Check 1.8
How long does it take to learn the entire Java library?
Answer: No one person can learn the entire library – it is too large.
Becoming Familiar with Your Programming Environment
An editor is a program for entering and modifying text, such as a Java program.
Java is case sensitive.
Java compiler translates source code into class files.
Class files contain instructions for the Java virtual machine.
Becoming Familiar with Your Programming Environment
Start the Java development environment.
Write a simple program.
Run the program.
Organize your work.
Becoming Familiar with Your Programming Environment
Figure 5 Running the HelloPrinter Program in an Integrated Development Environment
Becoming Familiar with Your Programming Environment
Figure 6 Running the HelloPrinter Program in a Console Window
Becoming Familiar with Your Programming Environment
Figure 7 From Source Code to Running Program
Becoming Familiar with Your Programming Environment
Figure 8 A Folder Hierarchy
Self Check 1.9
Where is the HelloPrinter.java file stored on your computer?
Answer:
The answer varies among systems. A typical answer:
/home/dave/cs1/hello/HelloPrinter.java or
c:\Users\Dave\Workspace\hello\HelloPrinter.java
Self Check 1.10
What do you do to protect yourself from data loss when you work on programming projects?
causes a program to perform an action that the programmer did not intend.
System.out.println("Hello, Word!");
Errors
Exception - a type of run-time error
Generates an error message from the Java virtual machine
This statement
System.out.println(1 / 0)
Generates this run-time error message
"Division by zero"
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?
Answer: This is a compile-time error. The compiler will complain that it does not know the meanings of the words Hello and World.
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?
Answer: This is a compile-time error. The compiler will complain that System.out does not have a method called printline.
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?
Answer: This is a run-time error. It is perfectly legal to give the name hello to a method, so the compiler won't complain. But when the program is run, the virtual machine will look for a main method and won't find one.
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?
Answer:
It is a run-time error. After all, the program had been compiled in order for you to run it.
Self Check 1.20
Why can't you test a program for run-time errors when it has compiler errors?
Answer:
When a program has compiler errors, no class file is produced, and there is nothing to run.
Problem Solving: Algorithm Design
Algorithm: A sequence of steps that is:
unambiguous
executable
terminating
An Algorithm for Solving an Investment Problem
The problem:
You put $10,000 into a bank account that earns 5 percent interest per year. How many years does it take for the account balance to be double the original?
Calculating by hand
An Algorithm for Solving an Investment Problem - continued
The steps in the algorithm
Start with a year value of 0, a column for the interest, and a balance of $10,000.
Repeat the following steps while the balance is less than $20,000
Add 1 to the year value.
Compute the interest as balance x 0.05 (i.e., 5 percent interest).
Add the interest to the balance.
Report the final year value as the answer.
Pseudocode
Pseudocode: An informal description of of a sequence of steps for solving a problem
Describe how a value is set or changed:
total cost = purchase price + operating cost
Multiply the balance value by 1.05.
Remove the first and last character from the word.
Describe decisions and repetitions:
If total cost 1 < total cost 2s
While the balance is less than $20,000
For each picture in the sequence
Use indentation to indicate which statements should be selected or repeated
For each car
operating cost = 10 x annual fuel cost
total cost = purchase price + operating cost
Indicate results:
Choose car1.
Report the final year value as the answer.
From Algorithm to Programs
Self Check 1.21
Suppose the interest rate was 20 percent. How long would it take for the investment to double?
Answer: 4 years:
0 10,000
1 12,000
2 14,400
3 17,280
4 20,736
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.
Answer:
Is the number of minutes at most 300?
a. If so, the answer is $29.95 × 1.125 = $33.70.
b. If not,
1. Compute the difference: (number of minutes) – 300.
2. Multiply that difference by 0.45.
3. Add $29.95.
4. Multiply the total by 1.125. That is the answer.
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?
Answer: No. The step If it is more attractive than the "best so far" is not executable because there is no objective way of deciding which of two photos is more attractive.
Self Check 1.24
Suppose each photo in Self Check 23 had a price tag. Give an algorithm for finding the most expensive photo.
Answer:
Pick the first photo and call it "the most expensive so far".
For each photo in the sequence
If it is more expensive than "the most expensive so far"
Discard "the most expensive so far".
Call this photo "the most expensive so far".
The photo called "the most expensive so far" is the most expensive photo in the sequence
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 ? Spell out the steps until the algorithm stops.
Answer:
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?
Answer:
The sequence doesn’t terminate. Consider the input . The first two marbles keep getting switched.