Introduction To Computers and Java: Walter Savitch Frank M. Carrano
Introduction To Computers and Java: Walter Savitch Frank M. Carrano
Carrano
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Objectives
Overview computer hardware and software Introduce program design and objectoriented programming Overview the java programming language (Optional) introduce applets and graphics basics
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Outline
Computer Basics Designing Programs A Sip of Java
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
The Processor
Also called the CPU (central processing unit) or the chip (e.g. Pentium processor) The processor processes a programs instructions. It can process only very simple instructions. The power of computing comes from speed and program intricacy.
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 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, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Main memory
Working memory used to store
The current program The data the program is using The results of intermediate calculations
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Auxiliary Memory
Also called secondary memory Disk drives, CDs, DVDs etc. More or less permanent (nonvolatile) Usually measured in gigabytes (e.g. 50 gigabyte hard drive)
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
10
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
11
Main Memory
Figure 1.1
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
12
Storing Data
Data of all kinds (numbers, letters, strings of characters, audio, video, even programs) are encoded and stored using 1s and 0s. When more than a single byte is needed, several adjacent bytes are used.
The address of the first byte is the address of the unit of bytes.
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
13
Files
Large groups of bytes in auxiliary memory are called files. Files have names. Files are organized into groups called directories or folders. Java programs are stored in files. Programs files are copied from auxiliary memory to main memory in order to be run.
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
14
0s and 1s
Machines with only 2 stable states are easy to make, but programming using only 0s and 1s is difficult. Fortunately, the conversion of numbers, letters, strings of characters, audio, video, and programs is done automatically.
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
15
Programs
A program is a set of instructions for a computer to follow. We use programs almost daily (email, word processors, video games, bank ATMs, etc.). Following the instructions is called running or executing the program.
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
16
The output is the result(s) produced by following the instructions in the program.
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
17
Running a Program
Figure 1.2
Sometimes the computer and the program are considered to be one unit.
Programmers typically find this view to be more convenient.
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
18
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
19
Programming Languages
High-level languages are relatively easy to use
Java, C#, C++, Visual Basic, Python, Ruby.
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
20
Compilers
A compiler translates a program from a high-level language to a low-level language the computer can run. You compile a program by running the compiler on the high-level-language version of the program called the source program. Compilers produce machine- or assemblylanguage programs called object programs.
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
21
Compilers
Most high-level languages need a different compiler for each type of computer and for each operating system. Most compilers are very large programs that are expensive to produce.
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
22
Java Byte-Code
The Java compiler does not translate a Java program into assembly language or machine language for a particular computer. Instead, it translates a Java program into byte-code. Byte-code is the machine language for a hypothetical computer (or interpreter) called the Java Virtual Machine.
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
23
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.
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
24
25
Portability
After compiling a Java program into bytecode, that byte-code can be used on any computer with a byte-code interpreter and without a need to recompile. Byte-code can be sent over the Internet and used anywhere in the world. This makes Java suitable for Internet applications.
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
26
Class Loader
A Java program typically consists of several pieces called classes. Each class may have a separate author and each is compiled (translated into bytecode) separately. A class loader (called a linker in other programming languages) automatically connects the classes together.
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
27
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
28
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
29
History of Java
In 1991, James Gosling and Sun Microsystems began designing a language for home appliances (toasters, TVs, etc.).
Challenging, because home appliances are controlled by many different chips (processors) Programs were translated first into an intermediate language common to all appliance processors.
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
30
History of Java
Then the intermediate language was translated into the machine language for a particular appliances processor. Appliance manufacturers werent impressed.
In 1994, Gosling realized that his language would be ideal for a Web browser that could run programs over the Internet.
Sun produced the browser known today as HotJava.
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
31
Applets
Little applications Meant to be sent to another location on the internet and run there
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
32
class FirstProgram
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
33
Some Terminology
The person who writes a program is called the programmer. The person who interacts with the program is called the user. A package is a library of classes that have been defined already.
import java.util.Scanner;
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
34
Some Terminology
The item(s) inside parentheses are called argument(s) and provide the information needed by methods. A variable is something that can store data. An instruction to the computer is called a statement; it ends with a semicolon. The grammar rules for a programming language are called the syntax of the language.
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
35
is an object for sending output to the screen. println is a method to print whatever is in parentheses to the screen.
System.out
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
36
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
37
38
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
39
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
40
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
41
Programming
Programming is a creative process. Programming can be learned by discovering the techniques used by experienced programmers. These techniques are applicable to almost every programming language, including Java.
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
42
Object-Oriented Programming
Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). Objects can perform actions which affect themselves and other objects in the world. Object-oriented programming (OOP) treats a program as a collection of objects that interact by means of actions.
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
43
OOP Terminology
Objects, appropriately, are called objects. Actions are called methods. Objects of the same kind have the same type and belong to the same class.
Objects within a class have a common set of methods and the same kinds of data but each object can have its own data values.
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
44
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
45
Introduction to Encapsulation
The data and methods associated with any particular class are encapsulated (put together in a capsule), but only part of the contents is made accessible.
Encapsulation provides a means of using the class, but it omits the details of how the class works. Encapsulation often is called information hiding.
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
46
Accessibility Example
An automobile consists of several parts and pieces and is capable of doing many useful things.
Awareness of the accelerator pedal, the brake pedal, and the steering wheel is important to the driver. Awareness of the fuel injectors, the automatic braking control system, and the power steering pump is not important to the driver.
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
47
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
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
48
Introduction to Inheritance
Figure 1.4
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
49
Introduction to Inheritance
Classes can be organized using inheritance. A class at lower levels inherits all the characteristics of classes above it in the hierarchy. At each level, classifications become more specialized by adding other characteristics. Higher classes are more inclusive; lower classes are less inclusive.
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
50
Inheritance in Java
Used to organize classes Inherited characteristics do not need to be repeated. New characteristics are added. More about inheritance in chapter 8
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
51
Algorithms
By designing methods, programmers provide actions for objects to perform. An algorithm describes a means of performing an action. Once an algorithm is defined, expressing it in Java (or in another programming language) usually is easy.
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
52
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, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
53
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
54
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, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
55
Test your program with appropriate test cases (some where the answer is known), discover and fix any errors, then retest.
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
56
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, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
57
Syntax Errors
Grammatical mistakes in a program
The grammatical rules for writing a program are very strict
The compiler catches syntax errors and prints an error message. Example: using a period where a program expects a comma
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
58
Runtime Errors
Errors that are detected when your program is running, but not during compilation When the computer detects an error, it terminates the program and prints an error message. Example: attempting to divide by 0
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
59
Logic Errors
Errors that are not detected during compilation or while running, but which cause the program to produce incorrect results Example: an attempt to calculate a Fahrenheit temperature from a Celsius temperature by multiplying by 9/5 and adding 23 instead of 32
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
60
Software Reuse
Programs not usually created entirely from scratch Most contain components which already exist Reusable classes are used
Design class objects which are general Java provides many classes Note documentation on following slide
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
61
Software Reuse
Class names
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
62
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
63
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
64
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
65
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
66
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
67
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
68
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
69
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
70
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
71
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
72
Drawing Arcs
The drawArc method draws an arc.
drawArc(100, 50, 200, 200, 180, 180);
73
Specifying an Arc
Figure 1.8a
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
74
Specifying an Arc
Figure 1.8b
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
75
76
Summary
You have completed an overview of computer hardware and software. You have been introduced to program design and object-oriented programming. You have completed an overview of the Java programming language. You have been introduced to applets and graphics basics.
JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank M. Carrano. ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
77