Download Chapter 1. An Introduction to Java

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
UNIVERSIDAD SALESIANA DE BOLIVIA
INGENIERIA DE SISTEMAS
DOSSIER
SIS-221
PROGRAMACION III
Lic. Omar Alavi Tambo
2011
La Paz - Bolivia
INDICE
Pág.
Chapter 1. An Introduction to Java................................................................................... 1
Chapter 2. The Java Programming Environment ............................................................. 2
Chapter 3. Fundamental Programming Structures in Java ............................................... 3
Chapter 4. Objects and Classes......................................................................................... 4
Chapter 5. Inheritance....................................................................................................... 5
Chapter 6. Interfaces and Inner Classes............................................................................ 6
Chapter 7. Graphics Programming ................................................................................... 7
Chapter 8. Event Handling ............................................................................................... 8
Chapter 9. User Interface Components with Swing ......................................................... 9
Chapter 10. Deploying Applets and Applications .......................................................... 10
Chapter 11. Exceptions and Debugging ......................................................................... 11
Dossier – Programación III
Chapter 1. An Introduction to Java





Java as a Programming Platform
The Java "White Paper" Buzzwords
Java and the Internet
A Short History of Java
Common Misconceptions About Java
The first release of Java in 1996 generated an incredible amount of excitement, not just
in the computer press, but in mainstream media such as The New York Times, The
Washington Post, and Business Week. Java has the distinction of being the first and
only programming language that had a 10-minute story on National Public Radio. A
$100,000,000 venture capital fund was set up solely for products produced by use of a
specific computer language. It is rather amusing to revisit those heady times, and we
give you a brief history of Java in this chapter.
1
Dossier – Programación III
Chapter 2. The Java Programming Environment







Installing the Java Development Kit
Choosing a Development Environment
Using the Command-Line Tools
Using an Integrated Development Environment
Compiling and Running Programs from a Text Editor
Running a Graphical Application
Building and Running Applets
In this chapter, you will learn how to install the Java Development Kit (JDK) and how
to compile and run various types of programs: console programs, graphical applications,
and applets. You run the JDK tools by typing commands in a shell window. However,
many programmers prefer the comfort of an integrated development environment. We
show you how to use a freely available development environment to compile and run
Java programs. Although easier to learn, integrated development environments can be
resource-hungry and tedious to use for small programs. As a middle ground, we show
you how to use a text editor that can call the Java compiler and run Java programs. Once
you have mastered the techniques in this chapter and picked your development tools,
you are ready to move on to Chapter 3, where you will begin exploring the Java
programming language.
2
Dossier – Programación III
Chapter 3. Fundamental Programming Structures in
Java










A Simple Java Program
Comments
Data Types
Variables
Operators
Strings
Input and Output
Control Flow
Big Numbers
Arrays
At this point, we are assuming that you successfully installed the JDK and were able to
run the sample programs that we showed you in Chapter 2. It's time to start
programming. This chapter shows you how the basic programming concepts such as
data types, branches, and loops are implemented in Java.
Unfortunately, in Java you can't easily write a program that uses a GUI—you need to
learn a fair amount of machinery to put up windows, add text boxes and buttons that
respond to them, and so on. Because introducing the techniques needed to write GUIbased Java programs would take us too far away from our goal of introducing the basic
programming concepts, the sample programs in this chapter are "toy" programs,
designed to illustrate a concept. All these examples simply use a shell window for input
and output.
Finally, if you are an experienced C++ programmer, you can get away with just
skimming this chapter: concentrate on the C/C++ notes that are interspersed throughout
the text. Programmers coming from another background, such as Visual Basic, will find
most of the concepts familiar and all of the syntax very different—you should read this
chapter very carefully.
3
Dossier – Programación III
Chapter 4. Objects and Classes









Introduction to Object-Oriented Programming
Using Predefined Classes
Defining Your Own Classes
Static Fields and Methods
Method Parameters
Object Construction
Packages
Documentation Comments
Class Design Hints
In this chapter, we



Introduce you to object-oriented programming;
Show you how you can create objects that belong to classes in the standard Java
library; and
Show you how to write your own classes.
If you do not have a background in object-oriented programming, you will want to read
this chapter carefully. Thinking about object-oriented programming requires a different
way of thinking than for procedural languages. The transition is not always easy, but
you do need some familiarity with object concepts to go further with Java.
For experienced C++ programmers, this chapter, like the previous chapter, presents
familiar information; however, there are enough differences between the two languages
that you should read the later sections of this chapter carefully. You'll find the C++
notes helpful for making the transition.
4
Dossier – Programación III
Chapter 5. Inheritance







Classes, Superclasses, and Subclasses
Object: The Cosmic Superclass
Generic Array Lists
Object Wrappers and Autoboxing
Reflection
Enumeration Classes
Design Hints for Inheritance
Chapter 4 introduced you to classes and objects. In this chapter, you learn about
inheritance, another fundamental concept of object-oriented programming. The idea
behind inheritance is that you can create new classes that are built on existing classes.
When you inherit from an existing class, you reuse (or inherit) its methods and fields
and you add new methods and fields to adapt your new class to new situations. This
technique is essential in Java programming.
As with the previous chapter, if you are coming from a procedure-oriented language like
C, Visual Basic, or COBOL, you will want to read this chapter carefully. For
experienced C++ programmers or those coming from another object-oriented language
like Smalltalk, this chapter will seem largely familiar, but there are many differences
between how inheritance is implemented in Java and how it is done in C++ or in other
object-oriented languages.
This chapter also covers reflection, the ability to find out more about classes and their
properties in a running program. Reflection is a powerful feature, but it is undeniably
complex. Because reflection is of greater interest to tool builders than to application
programmers, you can probably glance over that part of the chapter upon first reading
and come back to it later.
5
Dossier – Programación III
Chapter 6. Interfaces and Inner Classes





Interfaces
Object Cloning
Interfaces and Callbacks
Inner Classes
Proxies
You have now seen all the basic tools for object-oriented programming in Java. This
chapter shows you several advanced techniques that are commonly used. Despite their
less obvious nature, you will need to master them to complete your Java tool chest.
The first, called an interface, is a way of describing what classes should do, without
specifying how they should do it. A class can implement one or more interfaces. You
can then use objects of these implementing classes anytime that conformance to the
interface is required. After we cover interfaces, we take up cloning an object (or deep
copying, as it is sometimes called). A clone of an object is a new object that has the
same state as the original. In particular, you can modify the clone without affecting the
original.
Next, we move on to the mechanism of inner classes. Inner classes are technically
somewhat complex—they are defined inside other classes, and their methods can access
the fields of the surrounding class. Inner classes are useful when you design collections
of cooperating classes. In particular, inner classes enable you to write concise,
professional-looking code to handle GUI events.
This chapter concludes with a discussion of proxies, objects that implement arbitrary
interfaces. A proxy is a very specialized construct that is useful for building systemlevel tools. You can safely skip that section on first reading.
6
Dossier – Programación III
Chapter 7. Graphics Programming








Introducing Swing
Creating a Frame
Positioning a Frame
Displaying Information in a Panel
Working with 2D Shapes
Using Color
Using Special Fonts for Text
Doing More with Images
To this point, you have seen only how to write programs that take input from the
keyboard, fuss with it, and then display the results on a console screen. This is not what
most users want now. Modern programs don't work this way and neither do web pages.
This chapter starts you on the road to writing Java programs that use a graphical user
interface (GUI). In particular, you learn how to write programs that size and locate
windows on the screen, display text with multiple fonts in a window, display images,
and so on. This gives you a useful, valuable repertoire of skills that you will put to good
use in subsequent chapters as you write interesting programs.
The next two chapters show you how to process events, such as keystrokes and mouse
clicks, and how to add interface elements, such as menus and buttons, to your
applications. When you finish these three chapters, you will know the essentials for
writing stand-alone graphical applications. Chapter 10 shows how to program applets
that use these features and are embedded in web pages. For more sophisticated graphics
programming techniques, we refer you to Volume 2.
7
Dossier – Programación III
Chapter 8. Event Handling







Basics of Event Handling
The AWT Event Hierarchy
Semantic and Low-Level Events in the AWT
Low-Level Event Types
Actions
Multicasting
Implementing Event Sources
Event handling is of fundamental importance to programs with a graphical user
interface. To implement user interfaces, you must master the way in which Java handles
events. This chapter explains how the Java AWT event model works. You will see how
to capture events from the mouse and the keyboard. This chapter also shows you how to
use the simplest GUI elements, such as buttons. In particular, this chapter discusses how
to work with the basic events generated by these components. The next chapter shows
you how to put together the most common of the components that Swing offers, along
with a detailed coverage of the events they generate.
NOTE
Java 1.1 introduced a much improved event model for GUI
programming. We do not cover the deprecated 1.0 event model in this
chapter.
8
Dossier – Programación III
Chapter 9. User Interface Components with Swing







The Model-View-Controller Design Pattern
Introduction to Layout Management
Text Input
Choice Components
Menus
Sophisticated Layout Management
Dialog Boxes
The last chapter was primarily designed to show you how to use the event model in
Java. In the process you took the first steps toward learning how to build a graphical
user interface. This chapter shows you the most important tools you'll need to build
more full-featured GUIs.
We start out with a tour of the architectural underpinnings of Swing. Knowing what
goes on "under the hood" is important in understanding how to use some of the more
advanced components effectively. We then show you how to use the most common user
interface components in Swing such as text fields, radio buttons, and menus. Next, you
will learn how to use the nifty layout manager features of Java to arrange these
components in a window, regardless of the look and feel of a particular user interface.
Finally, you'll see how to implement dialog boxes in Swing.
This chapter covers basic Swing components such as text components, buttons, and
sliders. These are the essential user interface components that you will need most
frequently. We cover advanced Swing components in Volume 2. For an even more
comprehensive look into all details of the Swing framework, we recommend the books
Core JFC and Core Swing: Advanced Topics by Kim Topley (both published by
Prentice Hall PTR).
9
Dossier – Programación III
Chapter 10. Deploying Applets and Applications








Applet Basics
The Applet HTML Tags and Attributes
Multimedia
The Applet Context
JAR Files
Application Packaging
Java Web Start
Storage of Application Preferences
At this point, you should be comfortable with using most of the features of the Java
programming language, and you had a pretty thorough introduction to basic graphics
programming in Java. We hope that you agree with us that Java is a nice (albeit not
perfect), general-purpose OOP language, and the Swing user interface libraries are
flexible and useful. That's nice, but it isn't what created the original hype around Java.
The unbelievable hype during the first few years of Java's life (as mentioned in Chapter
1) stemmed from applets. An applet is a special kind of Java program that a Javaenabled browser can download from the Internet and then run. The hopes were that
users would be freed from the hassles of installing software and that they could access
their software from any Java-enabled computer or device with an Internet connection.
For a number of reasons, applets never quite lived up to these expectations. Recently,
Sun developed an alternative approach for Internet-based application delivery, called
Java Web Start, that fixes some of the problems of applets.
This chapter shows you how to write and deploy applets and Java Web Start
applications, how to package applets and applications for deployment, and how
applications access and store configuration information.
10
Dossier – Programación III
Chapter 11. Exceptions and Debugging







Dealing with Errors
Catching Exceptions
Tips for Using Exceptions
Logging
Using Assertions
Debugging Techniques
Using a Debugger
In a perfect world, users would never enter data in the wrong form, files they choose to
open would always exist, and code would never have bugs. So far, we have mostly
presented code as though we lived in this kind of perfect world. It is now time to turn to
the mechanisms the Java programming language has for dealing with the real world of
bad data and buggy code.
Encountering errors is unpleasant. If a user loses all the work he or she did during a
program session because of a programming mistake or some external circumstance, that
user may forever turn away from your program. At the very least, you must



Notify the user of an error;
Save all work;
Allow users to gracefully exit the program.
For exceptional situations, such as bad input data with the potential to bomb the
program, Java uses a form of error trapping called, naturally enough, exception
handling. Exception handling in Java is similar to that in C++ or Delphi. The first part
of this chapter covers Java's exceptions.
The second part of this chapter concerns finding bugs in your code before they cause
exceptions at run time. Unfortunately, if you use just the JDK, then bug detection is the
same as it was back in the Dark Ages. We give you some tips and a few tools to ease the
pain. Then, we explain how to use the command-line debugger as a tool of last resort.
For the serious Java developer, products such as Eclipse, NetBeans, and JBuilder have
quite useful debuggers. We introduce you to the Eclipse debugger.
11