Chapter 1 – Introduction
Computer Programs
- 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?
Class Question Jan 23 - 1
Where is a program like Microsoft Word usually stored when it is not currently running?
- On the printer
- On a memory stick
- On a hard drive
- In the cloud
The Java Programming Language
-
Safe
-
Portable
-
Platform-independent
- Distributed as instructions for a virtual machine
-
Vast set of library packages
-
Designed for the Internet
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 7 From Source Code to Running Program
Self Check 1.10
What do you do to protect yourself from data loss when you work on programming projects?
-
Answer:
You back up your files and folders.
In the real world
- Take a look at a simple program in Bluej
Analyzing Your First Program: Methods
- Each class contains declarations of methods.
- Each method contains a sequence of instructions.
- A method contains a collection of programming instructions that describe how to carry out a particular task
- A method is called by specifying the method and its arguments.
Analyzing Your First Program: main Method
Analyzing Your First Program: Statements
Analyzing Your First Program: Method Call
Syntax 1.1 Java Program

Analyzing Your First Program: Strings
-
String: a sequence of characters enclosed in double quotation marks:
"Hello, World!"
Analyzing Your First Program: Printing
Class Question Jan 28 - 1
How do you modify the HelloPrinter program to greet you if your name is Carlos
-
System.out.println(Hello,
Carlos);
- System.out.println("Hello, Carlos");
- system.out.println("Hello, Carlos");
- System.print("Hello, Carlos");
Class Question Jan 28 - 2
What does the following set of statements print?
System.out.print("My lucky number is");
System.out.println(3 + 4 + 5);
- My lucky number is 12
- My lucky number is12
- My lucky number is
12
- It does not print anything. There is a syntax error
Errors
- A compile-time error (syntax error)
- A run-time error (logic errors)
Errors
- Syntax errors - compile time error
- Exception - a type of run-time error
- logic error - another type of rune type error
Class Question Jan 28 - 3
Suppose you omit the "" characters around Hello, World! from the HelloPrinter.java program. Is this a compile-time error or a run-time error?
- compile-time
- run-time
- It is not an error
Class Question Jan 28 - 4
Suppose you change println to printline in the HelloPrinter.java program. Is this a compile-time error or a run-time error?
- compile-time
- run-time
- It is not an error
Class Question Jan 28 - 5
Suppose you change main to hello in the HelloPrinter.java program. Is this a compile-time error or a run-time error?
- compile-time
- run-time
- It is not an error
Class Question Jan 28 - 6
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?
- compile-time
- run-time
- It is not an error
Notes
- Need a notetaker
- If you were not here last class, come tell me after class
- Bring paper and pencil next class.
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

Class Question Jan 30 - 1
Suppose the interest rate was 20 percent. How long would it take for the investment to double?
- 3
- 4
- 5
- There is not enough information to tell
-
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.
Use paper and pencil
-
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.
Use pencil and paper
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
Use pencil and paper
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.