chapter 1 introduction. chapter goals to understand the activity of programming to understand the...
TRANSCRIPT
CHAPTER 1
INTRODUCTION
CHAPTER GOALS To understand the activity of
programming
To learn about the architecture of computers
To learn about machine code and high level programming languages
To become familiar with your computing environment and your compiler
To compile and run your first Java program
To recognize syntax and logic errors
PREREQUISITES
Computer savvy (file management, text editing)
· Problem solving skills · Time management · High school math (Geometry, Algebra
II) · No prior programming background
required
What do computers do?
Help with organizing and writing term papers
Help balance checkbooks Keep track of students' grades They are even good game machines
How do computers do it?
Computers must be programmed to perform tasks. Different tasks require different programs.
A computer program executes a sequence of very basic operations in rapid succession
What is a computer?
· Central processing unit · Memory · Peripherals · Executes very simple instructions
· Executes instructions very rapidly
· General purpose device · Programs describe specific actions
Self Check1. What is required to play a music CD
on a computer? Ans: A program that reads the data on the CD and sends output to the speakers and the screen.
2. Why is a CD player less flexible than a computer? Ans: A CD player can do one thing–play music CDs. It cannot execute programs.
Self Check Cont…
3. Can a computer program develop the initiative to execute tasks in a better way than its programmers envisioned?
Ans: No–the program simply executes the instruction sequences that the programmers have prepared in advance.
Central Processing Unit
CPU and what it does
A single chip consisting of millions of transistors
performs program control arithmetic operations data movement
A Memory Module with Memory Chips
Figure 2: A Memory Module with Memory Chips
A Hard Disk
Figure 3: A Hard Disk
A Motherboard
Figure 4: A Motherboard
Schematic Diagram of a Computer
Figure 5: Schematic Diagram of a Computer
The ENIAC
Figure 6: The ENIAC
Self Check4. Where is a program stored when it is
not currently running? Ans: In secondary storage, typically a hard disk.
5. Which part of the computer carries out arithmetic operations, such as addition and multiplication?Ans: The central processing unit.
Programming Languages
· Machine/Virtual Machine21 40 16 100 163 240
· Assembleriload intRatebipush 100if_icmpgt intError
· High-level languageif (intRate > 100) . . .
Compilers
Language specific Sophisticated programs that
translate logical statements such as ifs into sequences of computations, tests and jumps
find memory locations for variables and more . . .
The Java Programming Language
· Simple · Safe · Platform-independent ("write once, run anywhere")
· Rich library · Designed for the internet
Applets on a Web Page
Figure 7: Applets on a Web Page
Version Year Important New Features
1.0 1996
1.1 1997 Inner classes
1.2 1998 Swing, Collections
1.3 2000 Performance enhancements
1.4 2002 Assertions, XML
5 2004 Generic classes, enhanced for loop, auto-boxing, enumerations
6 2006 Library improvements
Java Versions
Self Check
5. What are the two most important benefits of the Java language? Ans: Safety and portability.
6. How long does it take to learn the entire Java library? Ans: No one person can learn the entire library–it is too large.
HOMEWORK! Complete the computer room
agreement form and have it signed by parent/guardian
Be sure you have a computer password
Read and take notes section 1-1 through 1-6 and complete the self-check exercises. italicized words -- vocabulary boxes points of interest (ENIAC) p. 29 exercises R1.1 – R1.3 to be
collected tomorrow with the self check exercises.
Becoming Familiar with your Computer
Login Locate the Java compiler Understand files and folders Write a simple program (later) Save your work
A Console Window
An Integrated Development Environment
Figure 9:An Integrated Development Environment
A simple program· public class ClassName · public static void main(String[] args)
· // comment· Method call object.methodName(parameters)
· System class · System.out object · println method
File Hello.java
1 public class Hello
2 {
3 public static void main(String[] args)
4 {
5 // display a greeting in the console window
6 System.out.println("Hello, World!");
7 }
8 }
Output: Hello, World!
A Simple Program
Figure 13:Calling a Method
System ClassSystem.out Objectprintln Method
• public class ClassName • public static void main(String[] args) • // comment• Method call
Self Check 7. How would you modify the HelloTester program
to print the words "Hello," and "World!" on two lines? Ans:
8. Would the program continue to work if you omitted the line starting with //? Ans: Yes–the line starting with // is a comment, intended for human readers. The compiler ignores comments.
System.out.println("Hello,");System.out.println("World");
Self Check cont…
9. What does the following set of statements print?
Ans: The printout is My lucky number is12.
(It would be a good idea to add a space after the is. )
System.out.print("My lucky number is");System.out.println(3 + 4 + 5);
Errors· Syntax errorsSystem.ouch.print("...");System.out.print("Hello);
· Detected by the compiler · Logic errorsSystem.out.print("Helop");
· Detected (hopefully) through testing
Self Check10. 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?Ans: A compile-time error. The compiler will not know what to do with the word display.
11. How can you find logic errors in a program?Ans: You need to run the program and observe its behavior.
From Source Code to Running Program
The Edit-Compile-Test Loop
CLASSWORK/HOMEWORK! R1.11 Use the Hello.java program to
create different syntax errors. Record these errors. Then use the program to create a logic error. Describe the error you created.
Do Exercises p 30 – 31 P1.1, P1.2, P1.4 - P1.7
At home Read sections 1.7 through 1.8 taking notes (pp 23-27)
On p 29 do review exercises R1.4 – 1.6, R1.10, and R1.12 to hand in.
Project 1.1