Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
1
Chapter 1: Introduction
Chapter 1
Introduction
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
2
Chapter 1: Introduction
Welcome to CS46A
•
•
•
•
•
www.horstmann.com/sjsu
Green Sheet
Register for Companion Lab
Send email to add
First homework due September 7
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
3
Chapter 1: Introduction
Course Goals
•
•
•
•
•
•
Introduction to computer science
Programming concepts
Problem solving
Object-oriented programming
Introduction to Java (but not a Java course)
Foundation for CS majors
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
4
Chapter 1: Introduction
Prerequisites
• Computer savvy (file management, text
editing)
• Problem solving skills
• Time management
• High school math (algebra, trigonometry)
• No prior programming background required
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
5
Chapter 1: Introduction
What is a computer?
•
•
•
•
•
•
•
Central processing unit
Memory
Peripherals
Executes very simple instructions
Executes instructions very rapidly
General purpose device
Programs describe specific actions
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
6
Chapter 1: Introduction
Figure 1
Central Processing Unit
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
7
Chapter 1: Introduction
Figure 2
CPU Chip Detail
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
8
Chapter 1: Introduction
Figure 3
RAM Chips
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
9
Chapter 1: Introduction
Figure 4
A Hard Disk
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
10
Chapter 1: Introduction
Figure 5
A High-Capacity
Floppy Disk and Its
Drive
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
11
Chapter 1: Introduction
Figure 6
A CD-ROM Drive
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
12
Chapter 1: Introduction
Figure 7
Tape Backup Drives and Data Tape
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
13
Chapter 1: Introduction
Figure 8
A Personal Computer
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
14
Chapter 1: Introduction
Figure 9
A Motherboard
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
15
Chapter 1: Introduction
Figure 10
Schematic Diagram of a
Personal Computer
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
16
Chapter 1: Introduction
Programming Languages
• Machine/Virtual Machine
21 40 16 100 163 240
• Assembler
iload intRate
bipush 100
if_icmpgt intError
• High-level language
if (intRate > 100) . . .
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
17
Chapter 1: Introduction
The Java Programming Language
• Simple
• Safe
• Platform-independent ("write once, run
anywhere")
• Rich library
• Designed for the internet
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
18
Chapter 1: Introduction
Figure 12
An Applet on a Web Page
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
19
Chapter 1: Introduction
Becoming Familiar with your
Computer
•
•
•
•
•
Login
Locate the Java compiler
Understand files and folders
Write a simple program (later)
Save your work
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
20
Chapter 1: Introduction
Figure 13
A Startup Screen with Icons
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
21
Chapter 1: Introduction
Figure 14
A Directory Hierarchy
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
22
Chapter 1: Introduction
Figure 15
Screen Layout of an Integrated
Java
©2000, John Wiley & Sons, Inc.
Environment Horstmann/Java Essentials, 2/e
23
Chapter 1: Introduction
A simple program
• public class ClassName
• public static void
main(String[] args)
• object.methodName(parameters)
• System class
• System.out object
• println method
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
24
Chapter 1: Introduction
Program Hello.java
public class Hello
{ public static void main(String[] args)
{ System.out.println("Hello, World!");
}
}
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
25
Chapter 1: Introduction
Compiling and Running
•
•
•
•
Type program into text editor
Save
Open command shell
Compile into byte codes
javac Hello.java
• Execute byte codes
java Hello
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
26
Chapter 1: Introduction
Figure 16
From Source Code to
Running Program
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
27
Chapter 1: Introduction
Figure 17
Edit–Compile–Debug Loop
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
28
Chapter 1: Introduction
Errors
• Syntax errors
System.ouch.print("...");
System.out.print("Hello);
• Detected by the compiler
• Logic errors
System.out.print("Hell");
• Detected (hopefully) through testing
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
29
Chapter 1: Introduction
Objects and Classes
• Object: entity that you can manipulate in
your programs (by invoking methods)
• Each object belongs to a class
• Class: Set of objects with the same behavior
• Class determines legal methods
"Hello".println() // Error
"Hello".length() // OK
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
30
Chapter 1: Introduction
A Class has 4 Purposes:
• A class specifies which methods you can
apply to its objects
• A class is a factory for objects (new)
• A class is a holding place for static methods
and objects (main, System.out)
• A class defines implementation details:
object fields and code for methods
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
31
Chapter 1: Introduction
Rectangle class
• Construct a rectangle:
new Rectangle(5, 10, 20, 30)
new Rectangle()
• Object variables:
Rectangle cerealBox = new
Rectangle(5, 10, 20, 30);
Rectangle crispyCrunchy;
• Sharing objects:
©2000, John Wiley
Sons, Inc.
crispyCrunchy
= &cerealBox;
Horstmann/Java Essentials, 2/e
32
Chapter 1: Introduction
Figure 18
Rectangular Shapes
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
33
Chapter 1: Introduction
Figure 19
Rectangle Objects
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
34
Chapter 1: Introduction
Figure 20
An Object Variable That
Refers to an
Objec
t
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
35
Chapter 1: Introduction
Figure 21
An Uninitialized Object
Variable
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
36
Chapter 1: Introduction
Figure 22
Two Object Variables That Refer to
the Same Object
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
37
Chapter 1: Introduction
Rectangle class 2
• Print a rectangle
System.out.println(cerealBox);
• Translate a rectangle
cerealBox.translate(15, 25)
• Import statement:
import java.awt.Rectangle;
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
38
Chapter 1: Introduction
Program MoveRectangle.java
import java.awt.Rectangle;
public class MoveRectangle
{ public static void main(String[] args)
{ Rectangle cerealBox = new Rectangle(5, 10, 20, 30);
cerealBox.translate(15, 25);
System.out.println(cerealBox);
}
}
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
39
Chapter 1: Introduction
Algorithms 1
• Example: You put $10,000 into a bank
account that earns 5% interest per year.
Interest is compounded annually. How many
years does it take for the account balance to
be double the original?
Year
Balance
0
1
2
3
$10,000.00
$10,500.00
$11,025.00
$11,576.25
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
40
Chapter 1: Introduction
Algorithms 2
•
•
•
•
Unambiguous
Executable
Terminating
If you can't find an algorithm, the computer
can't solve your problem
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e