Powerpoint Slides - Purdue University

Download Report

Transcript Powerpoint Slides - Purdue University

Introduction to Computers and
Java
Recitation - 01/11/2008
CS 180
Department of Computer Science,
Purdue University
Recitation
•
•
•
•
Review of important material or relevant material
not yet covered
Introduction to projects
A chance to ask questions
 Come prepared and participate
A quiz every week
2
Announcements

Class website:





http://www.cs.purdue.edu/~cs180/
Syllabus
Lecture and recitation slides are available
Assignments
Course policies (note the integrity policy)
©The McGraw-Hill Companies, Inc. Permission required for reproduction
or display.
3
Announcements

Class newsgroup:
purdue.class.cs180
 Project information and answers to questions
will be posted to the newsgroup; posts by grad
TAs are official
You are responsible for knowing all information
posted, including changes to assignments



A newsgroup client such as Mozilla Thunderbird or
Microsoft Outlook can access the class
newsgroup.
©The McGraw-Hill Companies, Inc. Permission required for reproduction
or display.
4
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.
5
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 (from a .java
file) into byte-code (a .class file).
–
•
Byte-code is the machine language for a hypothetical
computer (or interpreter) called the Java Virtual
Machine.
A byte-code program is easy to translate into
machine language for any particular computer.
6
Compiling, Interpreting and Running
•
Compile command
•
•
•
The compiler translates the Java source program
(.java) into the Java byte-code (.class).
javac Program1.java
creates Program1.class
Run command
•
•
The byte-code interpreter for your computer translates
each byte-code instruction into machine language and
runs the resulting machine-language instructions.
java Program1
runs the program
7
Portability
•
After compiling a Java program into byte-code,
that byte-code (a .class file) 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.
8
Compiling and running a Java Program
Object-Oriented Programming
•
Our world consists of objects (people, trees,
cars, etc.).
•
•
•
Represents a discrete concept
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.
10
OOP Design Principles
•
OOP adheres to three primary design principles:
•
•
•
Encapsulation
Polymorphism
Inheritance.
11
Encapsulation
•
Encapsulation refers to the clubbing of data with
the methods that operate on that data.
•
•
•
Encapsulation provides a means of using the class, but
it omits the details of how the class works
Encapsulation often is called information hiding.
An object of type ‘Car’ can have several properties
(carburettor, engine, fuel etc) and methods (start the
engine, brake etc) clubbed together.
12
Encapsulation 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 are not important to the driver.
13
Inheritance
•
Inheritance is a way to form new classes by
inheriting properties from existing classes.
•
•
•
The new class is also referred to as a derived
class or sub-class of the superclass.
Advantage – reusability of code. Inherited
characteristics do not need to be repeated in
the sub-class.
At each level, classifications become more
specialized by adding other characteristics.
14
Inheritance, contd.
•
Example of inheritance hierarchy –
More about inheritance in chapter 7
15
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.
Example – A bank account will compute monthly
interest depending on which form (or which type of
account - saving, checking etc) it is.
more about polymorphism in Chapter 7
16
Reusable Components
•
•
•
Most programs are created by combining
components that exist already.
Reusing components saves time and increases
reliability.
New components should be designed to be
reusable by other applications.
17
Making Components Reusable
•
To make components reusable•
•
Specify exactly how objects of the class
interact with other objects.
Design a class so that objects are general,
rather than unique to a particular application.
•
The more general the class is, the more reusable it
is.
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
19
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 semi-colon
20
Runtime Errors
•
Errors that are detected when your program is
running, but not during compilation
•
Run-time errors occur when an unnatural issue is
experienced at run-time. They may not occur
every time a program is run.
•
When the computer detects an error, it terminates
the program and prints an error message.
•
example: attempting to divide by 0
21
Logic Errors
•
Errors that are not detected during compilation or
while running, but which cause the program to
produce incorrect results.
•
•
•
•
because of incorrect logic used in the program.
difficult to catch as there is no error message
Example: an attempt to calculate a Fahrenheit
temperature from a Celsius temperature by
multiplying by 9/5 and adding 23 instead of 32.
A logic error can be detected by comparing the
expected result with the actual result.
22
Quiz
What are the three types of errors
and when are they reported?