Date post: | 16-Jan-2016 |
Category: |
Documents |
Upload: | blaise-ferguson |
View: | 228 times |
Download: | 1 times |
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 1: Introduction1
Chapter 1
Introduction
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 1: Introduction2
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
Chapter 1: Introduction3
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
Chapter 1: Introduction4
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
Chapter 1: Introduction5
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
Chapter 1: Introduction6
Figure 1Central Processing Unit
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 1: Introduction7
Figure 2CPU Chip Detail
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 1: Introduction8
Figure 3RAM Chips
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 1: Introduction9
Figure 4A Hard Disk
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 1: Introduction10
Figure 5A High-CapacityFloppy Disk and ItsDrive
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 1: Introduction11
Figure 6A CD-ROM Drive
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 1: Introduction12
Figure 7Tape Backup Drives and Data Tape
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 1: Introduction13
Figure 8A Personal Computer
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 1: Introduction14
Figure 9A Motherboard
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 1: Introduction15
Figure 10Schematic Diagram of aPersonal Computer
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 1: Introduction16
Programming Languages
• Machine/Virtual Machine21 40 16 100 163 240
• Assembleriload intRatebipush 100if_icmpgt intError
• High-level languageif (intRate > 100) . . .
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 1: Introduction17
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
Chapter 1: Introduction18
Figure 12An Applet on a Web Page
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 1: Introduction19
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
Chapter 1: Introduction20
Figure 13A Startup Screen with Icons
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 1: Introduction21
Figure 14A Directory Hierarchy
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 1: Introduction22
Figure 15Screen Layout of an Integrated JavaEnvironment
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 1: Introduction23
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
Chapter 1: Introduction24
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
Chapter 1: Introduction25
Compiling and Running
• Type program into text editor• Save• Open command shell• Compile into byte codesjavac Hello.java
• Execute byte codesjava Hello
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 1: Introduction26
Figure 16From Source Code toRunning Program
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 1: Introduction27
Edit–Compile–Debug Loop
Figure 17
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 1: Introduction28
Errors
• Syntax errorsSystem.ouch.print("...");System.out.print("Hello);
• Detected by the compiler• Logic errorsSystem.out.print("Hell");
• Detected (hopefully) through testing
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 1: Introduction29
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
Chapter 1: Introduction30
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
Chapter 1: Introduction31
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: crispyCrunchy = cerealBox;
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 1: Introduction32
Figure 18Rectangular Shapes
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 1: Introduction33
Figure 19
Rectangle Objects
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 1: Introduction34
Object
Refers to an
An Object Variable That
Figure 20
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 1: Introduction35
An Uninitialized ObjectVariable
Figure 21
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 1: Introduction36
Figure 22Two Object Variables That Refer tothe Same Object
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 1: Introduction37
Rectangle class 2
• Print a rectangleSystem.out.println(cerealBox);
• Translate a rectanglecerealBox.translate(15, 25)
• Import statement:import java.awt.Rectangle;
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 1: Introduction38
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
Chapter 1: Introduction39
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 Balance0 $10,000.001 $10,500.002 $11,025.003 $11,576.25
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 1: Introduction40
Algorithms 2
• Unambiguous• Executable• Terminating• If you can't find an algorithm, the computer
can't solve your problem