Java Introduction
Java Introduction
Introduction to
Computers and Java
Chapter 1
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Objectives
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Outline
Computer Basics
Designing Programs
A Sip of Java
Graphics Supplement
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Computer Basics: Outline
Hardware and Memory
Programs
Programming Languages and Compilers
Java Byte-Code
Graphics Supplement
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Hardware and Software
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Hardware and Memory
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
The Processor
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Memory
Memory holds
programs
data for the computer to process
the results of intermediate processing.
Two kinds of memory
main memory
auxiliary memory
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Main memory
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Auxiliary Memory
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Bits, Bytes, and Addresses
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Main Memory
Figure 1.1
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Storing Data
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Files
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
0s and 1s
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Programs
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Input and Output
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Running a Program
Figure 1.2
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
The Operating System
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Programming Languages
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Compilers
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Java Byte-Code
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Java Byte-Code
A byte-code program is easy to translate into
machine language for any particular computer.
A program called an interpreter translates each
byte-code instruction, executing the resulting
machine-language instructions on the particular
computer before translating the next byte-code
instruction.
Most Java programs today are executed using a
Just-In-Time or JIT compiler in which byte-code is
compiled as needed and stored for later reuse
without needing to be re-compiled.
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Compiling, Interpreting, Running
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Portability
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Class Loader
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Compiling and Running a
Program
Figure 1.3
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
A Sip of Java: Outline
History of the Java Language
Applications and Applets
A First Java Application Program
Writing, Compiling, and Running a
Java Program
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
History of Java
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
History of Java
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Applications and Applets
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
A First Java Application
View sample program Listing 1.1
class FirstProgram
Sample
Sample
screen
screen
output
output
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Some Terminology
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Some Terminology
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Printing to the Screen
objectName.methodName(argumentsTheMethodNeeds);
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Compiling a Java Program or Class
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Compiling and Running
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Compiling and Running
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Programming Basics: Outline
Object-Oriented Programming
Algorithms
Testing and Debugging
Software Reuse
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Programming
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Object-Oriented Programming
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
OOP Terminology
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
OOP Design Principles
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Introduction to Encapsulation
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Accessibility Example
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Introduction to Polymorphism
From the Greek meaning many forms
The same program instruction adapts to
mean different things in different
contexts.
A method name, used as an instruction,
produces results that depend on the class of
the object that used the method.
Everyday analogy: take time to recreate
causes different people to do different
activities
More about polymorphism in Chapter 8
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Introduction to Inheritance
Figure 1.4
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Introduction to Inheritance
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Algorithms
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Algorithms
An algorithm is a set of instructions for
solving a problem.
An algorithm must be expressed
completely and precisely.
Algorithms usually are expressed in
English or in pseudocode.
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Example: Total Cost of All Items
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Reusable Components
Most programs are created by combining
components that exist already.
Reusing components saves time and
money.
Reused components are likely to be
better developed, and more reliable.
New components should designed to be
reusable by other applications.
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Testing and Debugging
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Errors
An error in a program is called a bug.
Eliminating errors is called debugging.
Three kinds or errors
Syntax errors
Runtime errors
Logic errors
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Syntax Errors
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Runtime Errors
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Logic Errors
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Software Reuse
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Software Reuse
Description of class
Scanner
Package
names
Class names
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Graphics Supplement: Outline
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Objects and Methods
Recall that a method is an action which can
be performed by an object.
In this section, well name our object
canvas and well use it to draw figures
inside an applet display.
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
A Sample Graphics Applet
View sample program Listing 1.2
class HappyFace (page 31)
Sample
Sample
screen
screen
output
output
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
A Sample Graphics Applet
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Screen Coordinate System
Figure 1.6
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Screen Coordinate System
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Drawing Ovals and Circles
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Drawing Ovals and Circles
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Drawing Ovals and Circles
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Size and Positions of Figures
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Drawing Arcs
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Specifying an Arc
Figure 1.8b
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Running and Closing an
Applet
There are two ways to run an applet:
Embed the applet in a Web page and run it
Use an applet viewer from the IDE.
There are two corresponding ways to end
an applet:
If you are running the applet from a web site,
close the page or navigate away from the page
If you are using an applet viewer, use the mouse
to click the close-window button.
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Summary
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved