+ All Categories
Home > Documents > ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1...

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1...

Date post: 16-Jan-2016
Category:
Upload: blaise-ferguson
View: 228 times
Download: 1 times
Share this document with a friend
40
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction
Transcript
Page 1: ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction.

©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e

Chapter 1: Introduction1

Chapter 1

Introduction

Page 2: ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 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

Page 3: ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction.

©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

Page 4: ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction.

©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

Page 5: ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction.

©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

Page 6: ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction.

©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e

Chapter 1: Introduction6

Figure 1Central Processing Unit

Page 7: ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction.

©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e

Chapter 1: Introduction7

Figure 2CPU Chip Detail

Page 8: ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction.

©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e

Chapter 1: Introduction8

Figure 3RAM Chips

Page 9: ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction.

©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e

Chapter 1: Introduction9

Figure 4A Hard Disk

Page 10: ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction.

©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e

Chapter 1: Introduction10

Figure 5A High-CapacityFloppy Disk and ItsDrive

Page 11: ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction.

©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e

Chapter 1: Introduction11

Figure 6A CD-ROM Drive

Page 12: ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction.

©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e

Chapter 1: Introduction12

Figure 7Tape Backup Drives and Data Tape

Page 13: ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction.

©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e

Chapter 1: Introduction13

Figure 8A Personal Computer

Page 14: ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction.

©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e

Chapter 1: Introduction14

Figure 9A Motherboard

Page 15: ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction.

©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e

Chapter 1: Introduction15

Figure 10Schematic Diagram of aPersonal Computer

Page 16: ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction.

©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) . . .

Page 17: ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction.

©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

Page 18: ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction.

©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e

Chapter 1: Introduction18

Figure 12An Applet on a Web Page

Page 19: ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction.

©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

Page 20: ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction.

©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e

Chapter 1: Introduction20

Figure 13A Startup Screen with Icons

Page 21: ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction.

©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e

Chapter 1: Introduction21

Figure 14A Directory Hierarchy

Page 22: ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction.

©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e

Chapter 1: Introduction22

Figure 15Screen Layout of an Integrated JavaEnvironment

Page 23: ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction.

©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

Page 24: ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction.

©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!"); }}

Page 25: ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction.

©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

Page 26: ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction.

©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e

Chapter 1: Introduction26

Figure 16From Source Code toRunning Program

Page 27: ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction.

©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e

Chapter 1: Introduction27

Edit–Compile–Debug Loop

Figure 17

Page 28: ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction.

©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

Page 29: ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction.

©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

Page 30: ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction.

©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

Page 31: ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction.

©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;

Page 32: ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction.

©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e

Chapter 1: Introduction32

Figure 18Rectangular Shapes

Page 33: ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction.

©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e

Chapter 1: Introduction33

Figure 19

Rectangle Objects

Page 34: ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction.

©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e

Chapter 1: Introduction34

Object

Refers to an

An Object Variable That

Figure 20

Page 35: ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction.

©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e

Chapter 1: Introduction35

An Uninitialized ObjectVariable

Figure 21

Page 36: ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction.

©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e

Chapter 1: Introduction36

Figure 22Two Object Variables That Refer tothe Same Object

Page 37: ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction.

©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;

Page 38: ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction.

©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); }}

Page 39: ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction.

©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

Page 40: ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction.

©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


Recommended