There are two fundamental protocols in the transport layer 1. TCP Transmission Control Protocol-- connection-oriented protocol 2. UDP User Datagram Protocol -- connectionless protocol Connection-oriented communication : establishes a logical (virtual) connection prior to sending data. Connectionless communication : sends data right away without establishing a logical connection Qn why do we have transport layer protocal? IP provides a weak, but efficient service model (best-effort ) How should hosts send into the network? i.)Flow Control ii.) Too fast is bad; too slow is not efficient IP packets are addressed to a host How to decide which application gets which packets? NOTE TRASPORT LAYER IS RESPONSIBLE FOR PROCESS TO PROCESS DELIVERY PORTS port is a communication endpoints Since there are many applications running on a co...
An Introduction to
Java Programming
What Is Java?
Java is an object-oriented programming language developed by Sun Microsystems, a company
best known for its high-end Unix workstations. Modeled after C++, the Java language was
designed to be small, simple, and portable across platforms and operating systems, both at the
source and at the binary level (more about this later).
Java is often mentioned in the same breath as HotJava, a World Wide Web browser from Sun-like Netscape or Mosaic (see Figure 1.1). What makes HotJava different from most other
browsers is that, in addition to all its basic Web features, it can also download and play applets
on the reader’s system. Applets appear on a Web page much in the same way as images do, but
unlike images, applets are dynamic and interactive. Applets can be used to create animations,
figures, or areas that can respond to input from the reader, games or other interactive effects on
the same Web pages among the text and graphics.
Although HotJava was the first World Wide Web browser to be able to play Java applets, Java
support is rapidly becoming available in other browsers. Netscape 2.0 provides support for Java
applets, and other browser developers have also announced support for Java in forthcoming
products.
Why Learn Java?
At the moment, probably the most compelling reason to learn Java—and probably the reason
you bought this book—is that HotJava applets are written in Java. Even if that were not the case,
Java as a language has significant advantages over other languages and other programming
environments that make it suitable for just about any programming task. This section describes
some of those advantages
1.Java Is Platform-Independent
Platform independence is one of the most significant advantages that Java has over other
programming languages, particularly for systems that need to work on many different platforms.
Java is platform-independent at both the source and the binary level.
Platform-independence is a program’s capability of moving easily from one computer
system to another.
At the source level, Java’s primitive data types have consistent sizes across all development
platforms. Java’s foundation class libraries make it easy to write code that can be moved from
platform to platform without the need to rewrite it to work with that platform.
Platform-independence doesn’t stop at the source level, however. Java binary files are also
platform-independent and can run on multiple problems without the need to recompile the
source. How does this work? Java binary files are actually in a form called bytecodes.
Bytecodes are a set of instructions that looks a lot like some machine codes, but that is not
specific to any one processor.
Normally, when you compile a program written in C or in most other languages, the compiler
translates your program into machine codes or processor instructions. Those instructions are
specific to the processor your computer is running—so, for example, if you compile your code
on a Pentium system, the resulting program will run only on other Pentium systems. If you want
to use the same program on another system, you have to go back to your original source, get a
compiler for that system, and recompile your code. Figure 1.2 shows the result of this system:
multiple executable programs for multiple systems.
Things are different when you write code in Java. The Java development environment has two
parts: a Java compiler and a Java interpreter. The Java compiler takes your Java program and
instead of generating machine codes from your source files, it generates bytecodes.
To run a Java program, you run a program called a bytecode interpreter, which in turn executes
your Java program (see Figure 1.3). You can either run the interpreter by itself or—for applets—
there is a bytecode interpreter built into HotJava and other Java-capable browsers that runs the
applet for you
To some, the object-oriented programming (OOP) technique is merely a way of organizing
programs, and it can be accomplished using any language. Working with a real object-oriented
language and programming environment, however, enables you to take full advantage of object-oriented methodology and its capabilities of creating flexible, modular programs and reusing
code
Many of Java’s object-oriented concepts are inherited from C++, the language on which it is
based, but it borrows many concepts from other object-oriented languages as well. Like most
object-oriented programming languages, Java includes a set of class libraries that provide basic
data types, system input and output capabilities, and other utility functions. These basic classes
are part of the Java development kit, which also has classes to support networking, common
Internet protocols, and user interface toolkit functions. Because these class libraries are written
in Java, they are portable across platforms as all Java applications are.
Java Is Easy to Learn
In addition to its portability and object-orientation, one of Java’s initial design goals was to be
small and simple, and therefore easier to write, easier to compile, easier to debug, and, best of
all, easy to learn. Keeping the language small also makes it more robust because there are fewer
chances for programmers to make difficult-to-find mistakes. Despite its size and simple design,
however, Java still has a great deal of power and flexibility.
Java is modeled after C and C++, and much of the syntax and object-oriented structure is
borrowed from the latter. If you are familiar with C++, learning Java will be particularly easy for
you, because you have most of the foundation already
Although Java looks similar to C and C++, most of the more complex parts of those languages
have been excluded from Java, making the language simpler without sacrificing much of its
power. There are no pointers in Java, nor is there pointer arithmetic. Strings and arrays are real
objects in Java. Memory management is automatic. To an experienced programmer, these
omissions may be difficult to get used to, but to beginners or programmers who have worked
in other languages, they make the Java language far easier to learn.
Creating a Java Application
Let’s start by creating a simple Java application: the classic Hello World example that all language
books use to begin. Analysis
As with all programming languages, your Java source files are created in a plain text editor, or
in an editor that can save files in plain ASCII without any formatting characters. On Unix,
emacs, ped, or vi will work; on Windows, Notepad or DOS Edit are both text editors.
Fire up your editor of choice, and enter the Java program shown in Listing 1.1. Type this
program, as shown, in your text editor. Be careful that all the parentheses, braces, and quotes
are there
This program has two main parts:
a)All the program is enclosed in a class definition—here, a class called HelloWorld.
b) The body of the program (here, just the one line) is contained in a routine called
main(). In Java applications, as in a C or C++ program, main() is the first
routine that is run when the program is executed.
You’ll learn more about both these parts of a Java application as the book progresses.
Once you finish typing the program, save the file. Conventionally, Java source files are named
the same name as the class they define, with an extension of .java. This file should therefore be
called HelloWorld.java.
Now, let’s compile the source file using the Java compiler. In Sun’s JDK, the Java compiler is
called javac.
To compile your Java program, Make sure the javac program is in your execution path and type
javac followed by the name of your source file:
javac HelloWorld.java
Object-Oriented
Programming
and Java
Object-oriented programming (OOP) is one of the bigger programming buzzwords of recent
years, and you can spend years learning all about object-oriented programming methodologies
and how they can make your life easier than The Old Way of programming. It all comes down
to organizing your programs in ways that echo how things are put together in the real world.
Today, you’ll get an overview of object-oriented programming concepts in Java and how they
relate to how you structure your own programs:
a)What classes and objects are, and how they relate to each other
b)The two main parts of a class or object: its behaviors and its attributes
c)Class inheritance and how inheritance affects the way you design your programs
d)Some information about packages and interfaces
If you’re already familiar with object-oriented programming, much of today’s lesson will be old
hat to you. You may want to skim it and go to a movie today instead. Tomorrow, you’ll get into
more specific details.
Thinking in Objects: An Analogy
Consider, if you will, Legos. Legos, for those who do not spend much time with children, are
small plastic building blocks in various colors and sizes. They have small round bits on one side
that fit into small round holes on other Legos so that they fit together snugly to create larger
shapes. With different Lego bits (Lego wheels, Lego engines, Lego hinges, Lego pulleys), you can
put together castles, automobiles, giant robots that swallow cities, or just about anything else you
can create. Each Lego bit is a small object that fits together with other small objects in predefined
ways to create other larger objects.
Here’s another example. You can walk into a computer store and, with a little background and
often some help, assemble an entire PC computer system from various components: a
motherboard, a CPU chip, a video card, a hard disk, a keyboard, and so on. Ideally, when you
finish assembling all the various self-contained units, you have a system in which all the units
work together to create a larger system with which you can solve the problems you bought a computer in the first place
Internally, each of those components may be vastly complicated and engineered by different
companies with different methods of design. But you don’t need to know how the component
works, what every chip on the board does, or how, when you press the A key, an “A” gets sent
to your computer. As the assembler of the overall system, each component you use is a self-contained unit, and all you are interested in is how the units interact with each other. Will this
video card fit into the slots on the motherboard and will this monitor work with this video card?
Will each particular component speak the right commands to the other components it interacts
with so that each part of the computer is understood by every other part? Once you know what the interactions are between the components and can match the interactions, putting together
the overall system is easy.
What does this have to do with programming? Everything. Object-oriented programming
works in exactly this same way. Using object-oriented programming, your overall program is
made up of lots of different self-contained components (objects), each of which has a specific
role in the program and all of which can talk to each other in predefined ways.
Objects and Classes
Object-oriented programming is modeled on how, in the real world, objects are often made up
of many kinds of smaller objects. This capability of combining objects, however, is only one very
general aspect of object-oriented programming. Object-oriented programming provides several
other concepts and features to make creating and using objects easier and more flexible, and the
most important of these features is that of classes
A class is a template for multiple objects with similar features. Classes embody all the
features of a particular set of objects.
When you write a program in an object-oriented language, you don’t define actual objects. You
define classes of objects.
An instance of a class is another word for an actual object. If classes are an abstract
representation of an object, an instance is its concrete representation.
So what, precisely, is the difference between an instance and an object? Nothing, really. The object is the more general term, but both instances and objects are the concrete representation of a class.
In fact, the terms instance and object are often used interchangeably in OOP language. An
instance of a tree and a tree object are both the same thing.
In an example closer to the sort of things you might want to do in Java programming, you might
create a class for the user interface element called a button. The Button class defines the features
of a button (its label, its size, its appearance) and how it behaves (does it need a single click or
a double click to activate it, does it change color when it’s clicked, what does it do when it’s
activated?). Once you define the Button class, you can then easily create instances of that
button—that is, button objects—that all take on the basic features of the button as defined by the class, but may have different appearances and behavior based on what you want that
particular button to do. By creating a Button class, you don’t have to keep rewriting the code
for each individual button you want to use in your program, and you can reuse the Button class
to create different kinds of buttons as you need them in this program and in other programs
Creating a Class
Ready? Let’s start with a basic class definition. Open up that editor and enter the following:
class Motorcycle {
}
Congratulations! You’ve now created a class. Of course, it doesn’t do very much at the moment,
but that’s a Java class at its very simplest.
First, let’s create some instance variables for this class—three of them, to be specific. Just below
the first line, add the following three lines:
String make;
String color;
boolean engineState;
Here, you’ve created three instance variables: two, make and color, can contain String objects
(String is part of that standard class library mentioned earlier). The third, engineState, is a
boolean that refers to whether the engine is off or on.
Now let’s add some behavior (methods) to the class. There are all kinds of things a motorcycle
can do, but to keep things short, let’s add just one method—a method that starts the engine.
Add the following lines below the instance variables in your class definition:
void startEngine() {
if (engineState == true)
System.out.println(“The engine is already on.”);
else {
engineState = true;
System.out.println(“The engine is now on.”);
}
}
The startEngine method tests to see whether the engine is already running (in the line
engineState == true) and, if it is, merely prints a message to that effect. If the engine isn’t already
running, it changes the state of the engine to true and then prints a message.
With your methods and variables in place, save the program to a file called Motorcycle.java
(remember, you should always name your Java files the same names as the class they define).
Here’s what your program should look like so far:
Before you compile this class, let’s add one more method. The showAtts method prints the
current values of the instance variables in an instance of your Motorcycle class. Here’s what it
looks like:
The showAtts method prints two lines to the screen: the make and color of the motorcycle object,
and whether or not the engine is on or off.
Save that file again and compile it using javac:
What happens if you now use the Java interpreter to run this compiled class? Try it. Java assumes
that this class is an application and looks for a main method. This is just a class, however, so it
doesn’t have a main method. The Java interpreter (java) gives you an error like this one:
In class Motorcycle: void main(String argv[]) is not defined
To do something with the Motorcycle class—for example, to create instances of that class and
play with them—you’re going to need to create a Java application that uses this class or add a
main method to this one. For simplicity’s sake, let’s do the latter. Listing 2.1 shows the main()
method you’ll add to the Motorcycle class (you’ll go over what this does in a bit).
Comments