what are applications and applets
TRANSCRIPT
8/4/2019 What Are Applications and Applets
http://slidepdf.com/reader/full/what-are-applications-and-applets 1/12
What are Applications and Applets
Applications, in short, are computer programs that you write that run on your
computer. You start from scratch, and you write the whole program; you have full
control. Applications start by you typing onto the screenjava NameOfClass
. The Javasoftware on your computer looks for the class that has the same name (in this case NameOfClass), looks to see if it has a main method. If it does it starts from the main
method, and continues until either the program finishes, or is terminated by the user
(i.e., by pressing control C).
Applets on the other hand are programs that you write, and put inside your web page. When someone opens up your web page, he automatically downloads any applet
that you have on your web page, just like an image. But by an applet, you can't always
control how much of the screen the applet takes up. The applet is shown with the web
page on the browser. Let's say that the user has an applet that is running on the browser, and he opens up a different window over your applet. Your applet is going to
have to know how to stop in middle of what it is doing, and restart when the user goes back to your applet. Let's say the user resizes the browser window, then your applet is
going to have to know how to redraw itself. That is a lot of work. The good news is
that you do not have to do the work. When you extend the Applet class (for now allthat means is you write extends applet after the name of the class), the browser does
most of the work for you. The browser supplies the "main" method, and around 200
methods that deal with the window resizing, starting, stopping, etc. The browser islike a container that has its own Java machine, and all that it needs is for you to fill in
mainly two spots; what do you want the applet to do when it starts and downloads theapplet for the first time, and what do you want for the applet to do when it needs toredraw itself every time. For example, let's say that your applet comes with an image
picture. You want to tell the browser to download the image only once, and save it
temporarily, while the user is on your site. However, you want the browser to redraw
the picture every time something blocks it. Therefore, there are two spots available,
one where you tell it that when the user starts the applet the first time download the
picture, and the second for when you want the applet to redraw the picture. If you told
it to download the picture every time it needed to be redrawn your applet would take
forever, and if you told it to draw the picture only once then if the user opened another
window on top of it and takes it off, your picture would not be redrawn. Thereforethere are two main spots to fill, and the browser does the rest of the work. Because the
browser runs its own Java software (which has to be this way, because the user might
not have any of its own Java software elsewhere), you have to follow the rules of the
browser that it is running on. Some browsers, especially the old ones, are unable torun Swing (advanced graphics), and therefore if you care you would choose to use
only the simple graphics that all browsers use. See the Downloading
8/4/2019 What Are Applications and Applets
http://slidepdf.com/reader/full/what-are-applications-and-applets 2/12
Browsers section. Another difference is that for security reasons applets are
sometimes restricted in creating it's own network connections, looking into your files,
etc.
Java on the Road to the Future
Servlets and JSP. These are advanced Java programs, that do most of the work on the computer or server of the one who wrote the Program. By applets, the whole
program is downloaded onto the user's computer. By servlets, the user asks the server
(the computer that holds the servlet) a question, the server works on it, and returns an
answer. For example, let's look at Ebay. When you tell it I want to look at all of your books on nose piercing, it makes a search and returns an answer. Obviously, the
whole of ebay isn't going to download onto your computer. Rather it works on its
computer, and returns just the answer. That is what servlets do, they create dynamic(meaning self creating) different pages based on what the user requests. (Ebay is justan example, and does not necessarily use servlet technology). JSP and servlets have
become extremely popular. For more information see my Servlet Homepage, and
check out my Servlet Training Coursestaught on-site at your location.
8/4/2019 What Are Applications and Applets
http://slidepdf.com/reader/full/what-are-applications-and-applets 3/12
What are control statements?
Whenever the computer runs a Java program, it goes straight from the first line of
code to the last. Control statements allow you to change the computer's control fromautomatically reading the next line of code to reading a different one. Lets say you
only want to run some code on condition. For example, let's say that you are making a
program for a bank. If someone wants to see his records, and gives his password, you
don't always want to let him see the records. First, you need to see if his password is
correct. You then create a control statement saying "if" the password is correct, then
run the code to let him see the records. "else" run the code for people who enter thewrong password. You can even put one control statement inside another. In our
example, the banking system has to worry about people trying to get someone else's
records. So, as the programmer, you give the user three shots. If he misses them, thenthe records are locked for a day. You can have code with control statements that look
like this (not real code, only meant to help you understand. also, Java doesn't have go back to earlier lines- again just trying to focus on the control part.)
1. set variable counter to 0;
2. show screen asking for password;
3. if password is correct, run the code that let's him see the records;
4. else add one to the counter;
5. if counter does not equal three, then go back to line two and reshow
the screen
6. else (meaning that this is the third time that he's messed up) then
lock the records and show screen "goodbye"
If you look at this psuedocode (code that is more understandable to a person, but thecomputer can not understand it) you will see that one if else, is placed inside the
next if else. The way you read the control is like this. If the password is correct thenskip lines 4-6 that deal with incorrect passwords only. Else, (if the password is
incorrect) skip the end of line three and read line 4. Then if we have not hit the thirdtime go back, and never run line 6. However, if this is the third time, then don't run
the end of line 5, but run line 6.
Control Statements in Java are very simple. In some languages they have
complex control statements, that do a lot. However, Java offers only simple control
statements. If you need a complex control statement, you can build it with a fewsimple ones. This makes control statements a lot easier to work with. Although Java
has a few different kinds, I will illustrate one more common one. The loop. A loop is
where you follow the same code over and over again, until some condition stops it.For example, what if you were writing a program that spell checks each word. You
don't know how many words there will be, as every document will be different. So
you create a while loop and tell it, while there are more words, keep on giving me the
8/4/2019 What Are Applications and Applets
http://slidepdf.com/reader/full/what-are-applications-and-applets 4/12
next word, I'll run the code to check if it is spelled correctly. Then I'll start the loop
again saying if there are more words give them to me. As long as there is one more,the code will start from the beginning of the loop every time.
1. while there are more words left to be checked
2. give me the next word
3. let me run some code to see if it is spelled correctly
4. check, if there are more words go back to line 1, else continue.
in short this is what a while loop looks like
1. while (some condition is true)
2. {
3. run this code again and again
4. }
What are Arrays and Data Structures?
You go to Ebay or an online store, and you buy a book, a computer, and a shirt.
How is the computer able to write a pointer and refer to that which you bought. Itdidn't know what you were planning to buy. There aren't hundreds of programmers
hired by ebay to quickly write up a code for each user, writing "create a variable for
shirt, etc.". It must be that it has the ability to create a reference (pointer) to eachindividual object. How? Besides the fact, how is it able to store your records. Ebay
has hundreds of people on record, and millions of pieces of merchandise. How is it
able to point to them all, and use each one individually?
Let's say that you have a hundred students in the class you are teaching. You have
a piece of code (method) that will calculate their grades for you. Are you going to
have to write the same code for each studentcalculate grades for john
calculate grades for sue
calculate grades for dave
etc.
That's ridiculous.
The answer to both of these questions are data structures. Data structures loosely
defined are lists, and containers that store similar things. For example, you can have a
data structure that stores all of the students of your class. Whenever you want to treat
the whole class as one, you make use of this "list". Also, every thing that you buygoes in a list, and therefore, ebay only needs to make one object for you. A list. Then,
whenever you buy something they add it to your list.
8/4/2019 What Are Applications and Applets
http://slidepdf.com/reader/full/what-are-applications-and-applets 5/12
There are three ways to create a data structure, and get this list. The first way is
known as an array. An array is a built-in data structure, that comes automatically withJava. It is the easiest to use, and the best kind if you want to pick out one individual
thing out of the list. An array can be made of anything. It can be made of primitives
(simple things in Java that are not an object. They can only do one thing. Like an int,
is a number, it can do nothing else), specific objects (like in my example, you canmake an array of students), generic objects (you can make an array of objects, and
store different objects in each one), or even of other arrays. You can refer to the list,or to a specific person in the list. For example, lets say that you have an array of
students called AllOfMyStudents. If you want to refer and use the whole array, you
refer to it as AllOfMyStudents. (An array is written also as AllOfMyStudents[]). But,
if you want to refer to John, and John is the 10th student in your array, then the wordAllOfMyStudents[10] is referring to John. (Actually Arrays start counting with
number zero, so in reality AllOfMyStudents[9] would be John). In my example where
you want to calculate the grades for all of your students in your class, and if all of your students are in an array, you can do it using a Loop (see Control Statements ),
and it will be easier, and only take a few lines. This is what the pseudo code looks
like.
1. start the loop to continue again and again until the condition stops
2. create a counter, start the counter at zero, and call the counter
studentNumber
3. while there are more students keep on doing this, when there are no more
students continue after loop
4. take the student[] (remember, that the arrayName with braces refer to
the whole array
5. take the student[studentNumber] (remember, that the arrayName with a
number in the braces refer to an individual student, and here studentNumberis a number variable, that keeps on going up by one)
6. do whatever code you need to do to calculate the grade of
student[studentNumber]
7. add one to the variable of studentNumber.
Notice, that it makes no difference if you have 10 students, or ten thousand students, if
they are all in an array this piece of code will run the same way.
Even though arrays are fast and easy to access and use, there's one big problem.
Once you create the array, it is technically impossible to add to the number, or insert
something in the middle. Therefore, there are two other kinds of data structuresolutions. One involves creating your own kind of data structure (remember, an array
is built into the Java language). This is an advanced topic, that has college classes, andmany different books just on it, but in short, think about a chain. A chain is one object
that is made up of many different links. Each link is a thing by itself, and it also
"points" to the next link. The same is true with a linked list. You create the list, and if you want to go through the list, you just run from one link to the next. There are many
8/4/2019 What Are Applications and Applets
http://slidepdf.com/reader/full/what-are-applications-and-applets 6/12
different kinds, and the "links" can hold whatever information you want, depending
on the way that you created it. Meaning, you can make a linked list of students, or of
numbers, since you write the code of how it works.
The third way involves using a premade list from the Java library. As Java holds
many premade classes, some of their premade classes are premade lists, that you can just bring in and use (see the Java API Librarysection). These are very easy to use, but
since they are made by someone else, you have to use them with the rules that they
come with.
What are Objects, Classes, and Methods, and what
does Object Orientation mean?
Java is an Object Oriented language, and understanding Object Orientation
(OO) is the backbone to understanding Java.
For many years, the advanced computer languages, amongst them C, were writtenwith a structured approach, and were known as structured languages. Structured
languages would group up a bunch of actions, and tie them together into a function.Then, whenever they needed a certain action to be used, they would call on that
function to do it for them. The reason for using this approach, was in order to help
shorten the amount of code. Lets look for example at the action game Prince of Persia.
(Games are easiest to illustrate with, as you can see and explain exactly what ishappening. I picked this game because it is relatively simple, although if you were to
use a more complex game like Diablo or Alice, then you would see the need for
Object Orientation even more.) There are hundreds of soldiers that the prince has tofight with, and many of the soldiers do exactly the same thing. Imagine writing the
same code for each soldier. That's no problem, you say, I'll just cut and paste the code
one hundred times, using "control C" and "control V". Yes it is a problem. Supposeyou finish writing the code for the game, and your boss tells you that you have to
change one detail in the soldiers. You're going to have to find each soldier in the code,and change it. Of course you're going to end up missing one, and your program is not
going to work well. Suppose he tells you to change all of the green soldiers. You're
really going to be messed up. This is true if you find an error, or for some other reason
need to restructure the whole game. The maintenance will become a nightmare.Imagine if this wouldn't be a game, but a major online store or banking system. Also,
imagine how much memory the code for hundreds of soldiers would be. Therefore,
8/4/2019 What Are Applications and Applets
http://slidepdf.com/reader/full/what-are-applications-and-applets 7/12
the programming language creators started structured language, where everything is a
function of actions. Therefore, in your Prince of Persia code, you only need to writethe actions of swinging the sword, jumping, attacking the prince, etc. once, tie each
action in a function and for each soldier call the same function. When the prince
reaches the second floor (for example), you will write the code for a soldier to come,
and call each function that the soldier needs. That would save you a lot of coding.Wait a second, you say, you can't do that. Not every soldier attacks in exactly the
same way. The yellow soldiers are fast at blocking, the tall guard in the seventh levelwaits for you to make the first move, etc. If you write the same code for each one,
then all of the soldiers would be the same, and if you write complete different code,
then your stuck with your old problem of maintenance. After all, there's usually only
one small difference between one soldier and the next. No problem, said thestructured programming people, we can fix that. We will have one function use most
of the code of a different function. For example the prince reaches the second floor,
and there's a blue soldier. You write all of the action for the blue, to attack, etc. Thecode to attack take up lines 150-200. You reach the third floor (line 250), and there's
another blue soldier. You tell this second blue soldier to just borrow all of the code of
the first soldier. You write in your code, goto line 150, continue till line 200, and thencome back here to line 260, instead of continuing at line 201. You reach the fourth
floor, and there's a yellow soldier. You write in your code goto line 150, but since the
yellow soldiers are better blockers, only use lines 150-180, and when you get to line180 come back to our code. And even when the prince reaches more different soldiers,
he can borrow some of the functions of one soldier, and some of the next. That way
everything works out well. The basic function code is never repeated, while we can
still be flexible.
However, people noticed that there were two major problems. The first problem
was that using a structured language caused many bugs to occur, and made it harder to
fix. For example, lets say the green soldier borrowed some code from the yellowsoldier on line 270, and some code from the red soldier on line 400, and adds some of
its own. However, when the prince goes to fight the green soldier, the green soldier
doesn't fight back. Why? You read and reread the added code, and it seems fine. Thenyou have to figure out, maybe when I went back and had a goto earlier line, there was
an error on the earlier line. But, why would it work there and not work here? Hence,
you would have a hard time finding where the cause of the error came from. Also,let's say you decide to take out the red soldier on line 400. Then without you realizingit, the code for the green soldier will not work. The code will say, use a function from
the red soldier at line 400, and there is no red soldier at line 400. You'll have a pointer
that is now pointing to something that isn't there anymore. However, the main
problem was that writing actions and grouping them into functions does not mimic
real life, and if you want to write real lifelike games, then this is a must. In the game,
8/4/2019 What Are Applications and Applets
http://slidepdf.com/reader/full/what-are-applications-and-applets 8/12
you wrote the code as a series of actions, where one action causes the next action. You
filled the action with data as necessary. Think of the Prince of Persia game as wedescribed it until now. When the prince, reaches the second floor, then the soldier
walks, then the soldier strikes, etc. You fill in the data of the soldier (what he looks
like) just as the side, and the main objective in the code is which action to do next.
Life is not a series of actions. When we bump into our friend John, it's not like Johnnever existed, and now he comes into play as a series of actions. John was always
there as a person, that also does certain actions. That bothered the programming people, because if you want to write really complicated games, then you can't just
have groups of actions. You would have to have everything as objects, where you
create objects and use them, and each object will contain the actions that are necessary
inside the object. Hence Object Orientation was born. (I don't think that it was theneed for having more realistic computer games that drove for the creation of Object
Orientation, but lets face it, that is what really counts.)
Object orientation means that everything in the program is an object, mimicking
real life. In reality by a car, for example, there doesn't exist a function to drive, andwhenever any car needs to it "borrows" and uses that function. Rather, there are many
objects of cars, and each one has its own individual function to drive. An object like a
car is composed of methods, like driving, braking, opening and locking a door, yellingat you when your seatbelt's off, etc, and data like the color, make, model, etc. When
you mimic this in a computer object, you also, create methods and data. The data of
an object is called fields or variables, and are usually lower cased. The methods ( seen
in the code as methodName()) contain actions, like openDoor(), drive(), and inside eachmethod are instructions on how to do that. Also, one method may divide itself into
smaller methods, and call each method one after another. For example,
method openDoor() might be made up of and call smaller methods like unlockDoor(), pushDoorOpen(), moveSeatBelt(), checkIfNightTime(), and then a turnLightsOn(), if it is night time. This helps you write the code, by breaking up everything into smaller
methods. If you are writing the code for a car, and you need the car to open its door,
you can write the words openDoor(), and fill in how to do it later. Whenever you needthe door to open, all it takes is one method call, and you can call it whenever and
wherever you want. In the Prince of Persia game, you can have for the prince amethod called jump(), swingSword(), and walkForward(). Method walkForward(),
will check what is in front of the Prince. If there is a plank, then it will call methodwalk(), if there is nothing it will call method fall(), and if there is a wall it will callmethod wallAhead(). This makes it very easy to break up a large problem into smaller
bits. When you put all of the pieces together you're able to ignore the details of the
methods on how they work, and when you work on each method's details, you canhide the big picture. This is necessary, as some games and programs are extremely
complicated, and it is impossible to see the big picture and all of the details at the
8/4/2019 What Are Applications and Applets
http://slidepdf.com/reader/full/what-are-applications-and-applets 9/12
same time. Another example of this in the Prince of Persia game is the object called
soldier. In that soldier class you will put methods and data. Some of the methods areattackPrince(), jump(), wasHit(), die(), etc, and some of the data will be color, amount
of time that he can be hit, etc. This data that we will call canBeHit will be a int, or
number, variable. When the soldier starts fighting, the variable data canBeHit may
start out as 5. In that case, when you start the code for that soldier, you might tell themain program, see how many hits he has, and put triangles for each hit that he can
receive. Every time the soldier is hit by the prince, the soldier's method of wasHit() iscalled. This method will start by calling a smaller method of showHit() that will show
on the screen the soldier being hit. Then it will subtract the hit variable by one. Then it
will check if canBeHit equals zero. If it does it will call the method die(), if it doesn't
it will call the method weakened(), which it will be that method's job to tell the main program to lessen one of the soldier's triangles. This is just an example, as I am trying
to show how one method uses the other methods and data.
There are two terms that seem confusing, and need to be clarified before I explain
more: classes and objects. Up until now I have been using these two termsinterchangeably, but they are different. A class is the code that you write up. There
will be only one soldier class (assuming that all of the soldiers are the same).
However, there may be many soldier objects. Each instance of the soldier class isanother object. For example, when the prince reaches the tower, your program creates
five soldiers to block him and try to kill him. Each one of those soldiers are an object
of class soldier. When you fight them, one soldier object might have five hits left, onemay have three, and one may be dead. Another example in the real world. There is
one class of Toyota Camry. However, there are thousands of Toyota Camry objects,
as I am currently driving one, my neighbor has one in his driveway, and I just saw onethat got into a crash down the block. Although we're all part of the Toyota Camry
class, we each have an independent object. Back to the Prince game. The game has a
main class, that runs the game. There is also a prince class and a soldier class. Lets sayin the game, when the prince reaches the tower, five soldiers come out (like before).
In the main class, you will write code that says if the prince reaches the tower,instantiate (meaning create an instance or Object of) one soldier calling it
soldierOne, a second soldier calling it soldierTwo, etc, and tell each one to
attack the prince. This helps writing your code in many ways. First of all it helpsmimic real life, enabling you better to visualize how to write it. Second, it makes
writing the code real easier. I need to only write the code for soldier once, with all of its complicated methods, and whenever I want one, I just tell the main program to
create a new object of that soldier, and put it here. I don't need to worry about tellingone soldier to now swing at the prince, one soldier to now jump, etc. The program will
do that automatically, as I had instructed in the soldier class. Each object of that
soldier will follow all of the rules that the soldier class has, all I need to do is tell the
main program, put one here, put one here.
8/4/2019 What Are Applications and Applets
http://slidepdf.com/reader/full/what-are-applications-and-applets 10/12
Great, you'll say, that helps me to visualize the program better, and I can even see
how this will help keep my errors down and make my debugging easier. But, it doesn'thelp with the problems that you started with. Your program works well if there is only
one kind of soldier. Lets say you want there to be different kinds of soldiers, and each
one does something slightly different. You're going to have to write many of the same
lines of code many times for each different soldier class, and your going to have all of the maintenance problems that you discussed in the beginning. The answer to that is
"No", Java and Object Orientation take care of that also. They do it with somethingknown as inheritance. Inheritance means that the child class can inherit, and get
everything that is in the parent class automatically. The most well used example is
that of a car. Almost every car is similar, with a few exceptions. If you were writing
code for a car, you would have methods like openDoor(), drive(), brake(),rollDownWindow(), etc. Lets say that this code has 200 methods, and took 1800 lines
of code (not to mention a week). Now, you have to write the code for a convertible.
The only difference between a convertible and a regular car is that the convertible'sroof can come down. If you had to cut and paste all of the code from the car class,
then yes you would have all of the problems. But, you can write in your code I want
the class of Convertible to extend and inherit everything from class Car, andautomatically class Convertible will have all of the methods and data from class Car.
The code for Convertible will have written inside of it only one method, openRoof(),will be only 100 lines of code, but will be able to do all 200 methods of class Car.
Even if you need a second car that drives differently you can say that the Car2 extends
everything from Car, and write one method called drive(). Since the child class(Class2) extends class Car, it will automatically have all 200 methods. However, since
it has one method with the same name as the parent class, the computer will ignore the parent's method of drive(), and only use Car2's new method of drive(). In our Prince
game, you can create a class called BadGuy, that has certain methods and data that
every single enemy in the game will have. Then you can have one class called
Skeleton that will have only the special and different methods that skeleton will have,
and one called Soldier, that will have its special methods. Then, both class Skeleton
and Soldier will automatically have every method that class BadGuy will have, and
whatever they added. You can even make two classes called StupidSoldier and
FastSoldier that have minor differences, and have each one inherit from class Soldier,and add their own minor differences. That way, if you have to change a piece, you
only need to change it in one place, and every class that depends on that piece willnow change. This is somewhat of an oversimplification, but the main purpose of this
page is to give an understanding of the topic, and not go into details of how it works.
So far we discussed two ways to have a class relationship. It can be a "stand alone"
class that doesn't inherit from any class, or you can have one class inherit fromanother. Another way, is for one class to contain inside it another class. For example,
You can have a Car class, a Wheel class, and a Door class. The Car class will have
8/4/2019 What Are Applications and Applets
http://slidepdf.com/reader/full/what-are-applications-and-applets 11/12
inside it four Wheel objects, and four Door objects. You can have a FramedWondow
Class that will have inside it a few Button classes. You write the code for a Buttonclass, and you tell the FramedWindow to contain that Button by instantiating and
placing inside that Button. In these examples, one class has a second class inside it.
I am now going to give a second example of inheritance. In programming, thereare many kinds of popup windows, each containing buttons and other stuff, that are
needed often. Therefore, Java created them for you automatically, and all that you
have to do is instantiate (tell the program, create an object of this kind of window
here) them. (See the API section for details.) In Java's graphic package there's a class
called Window, that when created pops up a simple window. There are two classes
that need everything that Window have, and more; Dialog and Frame.
DIALOG FR AME
Class Frame needs whatever Windows has, plus the icon, the ability to add amenu bar, the boxes in the corner to enlarge, make small, and close, etc. Class Dialog
only has the icon and the box to close. However, Dialog has something that Frame
doesn't, the ability to freeze every other window besides for itself. Therefore, you
don't want the Frame to inherit from Dialog because Dialog has the ability to freeze
the other windows, and you don't want Dialog to inherit from Frame, because Framehas a whole lot more than Dialog. However, most of the code is exactly the same, and
to cut and paste would be wasting a lot of code. The answer is simple. Create a class
that has whatever both Frame and Dialog both have in common, and each one willinherit that class and add what they have individually. This is what Java has done for
you already, by creating a Window class, and having both Frame and Dialog inherit
from Window, and add their individual methods. This is inheritance, as now bothFrame and Dialog are really a Window with added features.
Containment would be for class Frame to contain and have inside it other classes. Java
has premade classes like Button, Label, TextField. In the following picture, you see aFrame that has Buttons, Labels and TextFields. Just Like a Car has a Door, so too one
Java class can have inside it another Java class. And in reality, The Prince of Persia
8/4/2019 What Are Applications and Applets
http://slidepdf.com/reader/full/what-are-applications-and-applets 12/12
game uses containment, because the main program had inside it both the prince and
all of the soldiers. All the Prince of Persia had to do was write create a new soldier here, create a new soldier here, and the main program will now have inside it a new
soldier object.
One last example of inheritance. By Java's premade Frame class, if you hit the
little close box in the corner, nothing happens. Java did this so that you can have morecontrol of your Frame, but it is extremely annoying. All that it would have taken Java
to do in order to close the Frame would have been to add 5 lines of code, but now youare stuck with a Frame that doesn't close. But not to worry. You can inherit the Frame
class, and add those five lines. You can wrote a program that will now be only 10
lines which you can call ClosableFrame, which all that will be in the code
is ClosableFrame should inherit everything that is in Frame, but add that when
the close box in the corner is hit, the Frame will close, and your new class of ClosableFrame will have everything that its parent Frame class has, plus it will close,
with only a few lines of code.