1 - Computer Science and Engineering
Download
Report
Transcript 1 - Computer Science and Engineering
1
1
Introduction & The Java Virtual Machine
Small Java Chapter 1
2005 Pearson Education, Inc. All rights reserved.
Machine Languages, Assembly Languages and
High-Level Languages
•
Machine language
– 1’s and 0’s
– Machine dependent
•
Assembly language
– English-like abbreviations represent computer operations
– Translator programs convert to machine language
– ADD 12,14
•
High-level language
– Allows for writing more “English-like” instructions
– Contains commonly used mathematical operations
– Compiler converts to machine language
– If X < 12 THEN …
•
Interpreter
– Execute high-level language programs without compilation
2
2005 Pearson Education, Inc. All rights reserved.
FORTRAN, COBOL, Pascal and Ada
• FORTRAN
– FORmula TRANslator
• COBOL
– COmmon Business Oriented Language
• Pascal
– Structured programming
• Ada
– Multitasking
3
2005 Pearson Education, Inc. All rights reserved.
BASIC, Visual Basic, Visual C++, C# and .NET
• BASIC
– Beginner’s All-Purpose Symbolic Instruction Code
• .NET
– .NET platform
• Visual Basic .NET
– Based on BASIC
• Visual C++
– Based on C++
• C#
– Based on C++ and Java
4
2005 Pearson Education, Inc. All rights reserved.
History of Java
– Originally for intelligent consumer-electronic devices
– Then used for creating Web pages with dynamic content
– Now also used to:
• Develop large-scale enterprise applications
• Provide applications for consumer devices (cell
phones, etc.)
5
2005 Pearson Education, Inc. All rights reserved.
Java Is Not JavaScript!
• Java is a programming language. You write programs
in it and compile them.
• Javascript is imbedded in a web page and run by
Internet Explorer or Firefox
• Java applets are programs that run in a web browser
• We will only get to Java applications
6
2005 Pearson Education, Inc. All rights reserved.
With C++ and most compiled languages
Your Program
Source Code
If x = 5 then…
Compiler For Windows PC
Compiler For Mac
1011 0011
0011 1111
PC with Windows
Mac
7
2005 Pearson Education, Inc. All rights reserved.
Java Virtual Machine
Your Program
Source Code
If x = 5 then…
Java Compiler
ByteCode
Windows JVM
Mac JVM
1011 0011
0011 1111
PC with Windows
Mac
8
2005 Pearson Education, Inc. All rights reserved.
Java Virtual Machine
• Why and how is this different?
• Write once, run anywhere
– Bytecode is a ‘universal language’
• Why isn’t all software written like this?
– Xbox 360 games would work on PS 3…
9
2005 Pearson Education, Inc. All rights reserved.
Software Engineering Observation
Use a building-block approach to create
programs.
Avoid reinventing the wheel—use existing
pieces wherever possible.
Called software reuse, this practice is central
to object-oriented programming.
10
2005 Pearson Education, Inc. All rights reserved.
Java Class Libraries
• Classes
– Include methods (functions) that perform tasks
• Return information after task completion
– Used to build Java programs
• Java provides class libraries (of functions or methods)
– Known as Java APIs (Application Programming Interfaces)
– The stuff built-in to Java
– Don’t re-invent the wheel!
11
2005 Pearson Education, Inc. All rights reserved.
Software Engineering Observation
When programming in Java, you will typically use the
following building blocks:
Classes and methods from class libraries,
Classes and methods you create yourself
Classes and methods that others create and make
available to you.
12
2005 Pearson Education, Inc. All rights reserved.
Performance Tip
Using Java API classes and methods instead
of writing your own versions can improve
program performance, because they are
carefully written to perform efficiently.
This technique also shortens program
development time.
13
2005 Pearson Education, Inc. All rights reserved.
Typical Java Development Environment
• Java programs normally undergo five phases
– Edit
• Programmer writes program (and stores program on disk)
– Compile
• Compiler creates bytecodes from program
– Load
• Class loader stores bytecodes in memory
– Verify
• Bytecode Verifier confirms bytecodes do not violate security restrictions
– Execute
• JVM translates bytecodes into machine language
14
2005 Pearson Education, Inc. All rights reserved.
15
Fig. 1.1 | Typical Java development environment.
2005 Pearson Education, Inc. All rights reserved.
Common Programming Error
Errors like division by zero occur as a program runs, so
they are called runtime errors or execution-time errors.
Fatal runtime errors cause programs to terminate
immediately without having successfully performed their
jobs.
Nonfatal runtime errors allow programs to run to
completion, often producing incorrect results.
16
2005 Pearson Education, Inc. All rights reserved.
Good Programming Practice
Your computer and compiler are good
teachers.
Study each error or warning message you
get when you compile your programs (called
compile-time errors or compilation errors),
and correct the programs to eliminate these
messages.
17
2005 Pearson Education, Inc. All rights reserved.
Portability Tip
It is easier to write portable programs in Java than in other
programming languages
Portable to other platforms (from X-box to PS3)
However, differences between compilers, JVMs and
computers can make portability difficult to achieve.
Simply writing programs in Java does not guarantee
portability.
18
2005 Pearson Education, Inc. All rights reserved.
Error-Prevention Tip
Always test your Java programs on all
systems on which you intend to run them, to
ensure that they will work correctly for their
intended audiences.
19
2005 Pearson Education, Inc. All rights reserved.
Software Engineering Observation
The Java Development Kit comes with
source code for the Java API classes to
determine how the classes work and to
learn additional programming techniques.
20
2005 Pearson Education, Inc. All rights reserved.
21
Test-Driving a Java Application
Using Command Line
Using the cd command to
change directories
File location of the ATM application
Fig. 1.2 | Opening a Windows XP Command Prompt and changing directories.
2005 Pearson Education, Inc. All rights reserved.
22
Fig. 1.3 | Using the java command to execute the ATM application.
2005 Pearson Education, Inc. All rights reserved.
23
ATM welcome message
Enter account number prompt
Fig. 1.4 | Prompting the user for an account number.
2005 Pearson Education, Inc. All rights reserved.
24
Enter valid PIN
ATM main menu
Fig. 1.5 | Entering a valid PIN number and displaying the ATM application's main menu.
2005 Pearson Education, Inc. All rights reserved.
25
Account balance information
Fig. 1.6 | ATM application displaying user account balance information.
2005 Pearson Education, Inc. All rights reserved.
26
ATM withdrawal menu
Fig. 1.7 | Withdrawing money from the account and returning to the main menu.
2005 Pearson Education, Inc. All rights reserved.
27
Confirming updated account balance
information after withdrawal transaction
Fig. 1.8 | Checking new balance.
2005 Pearson Education, Inc. All rights reserved.
28
Account number prompt for next user
ATM goodbye message
Fig. 1.9 | Ending an ATM transaction session.
2005 Pearson Education, Inc. All rights reserved.
Programming Paradigms
• Traditional Programming vs.
Object Oriented Programming
Software objects attempt to model the real
world
Objects have data (attributes) and behaviors
(methods or functions)
29
2005 Pearson Education, Inc. All rights reserved.
Software Engineering Case Study: Introduction to Object
Technology and the UML
• Object orientation
• Unified Modeling Language (UML)
– Graphical language that uses common notation
– Allows developers to represent object-oriented
designs
30
2005 Pearson Education, Inc. All rights reserved.
Software Engineering Case Study (Cont.)
• Objects
– Reusable software components that model real-world
items
– People, animals, plants, cars, etc.
– Attributes
• Size, shape, color, weight, etc.
– Behaviors
• Babies cry, crawl, sleep, etc.
31
2005 Pearson Education, Inc. All rights reserved.
Software Engineering Case Study
• Object-oriented design (OOD)
– Models real-world objects
– Models communication among objects
– Encapsulates attributes and operations (behaviors)
• Information hiding
• Communication through well-defined interfaces
• Object-oriented language
– Programming in object-oriented languages is called
object-oriented programming (OOP)
– Java
32
2005 Pearson Education, Inc. All rights reserved.
Software Engineering Case Study
• Object-Oriented Analysis and Design (OOA/D)
– Essential for large programs
– Analyze program requirements, then develop
solution
– UML
• Unified Modeling Language
33
2005 Pearson Education, Inc. All rights reserved.
Software Engineering Case Study
Player
• UML
health
armor
Run (direction)
Fire (coordinates)
jump
– Graphical representation scheme
– Enables developers to model object-oriented systems
– Flexible and extensible
34
2005 Pearson Education, Inc. All rights reserved.