Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

How Computer Programs Work

  Date: Nov 24    Category: Java    Views: 565
  

Downloading the Java Compiler
In order to get a Java development environment (you "develop" (write) computer programs using a "development environment") set up on your machine, you will have to complete the following steps:

Download a large file containing the Java development environment (the compiler and other tools).
Download a large file containing the Java documentation.
If you do not already have WinZip (or an equivalent) on your machine, you will need to download a large file containing WinZip and install it.
Install the Java development environment.
Install the documentation.
Adjust several environment variables.
Test everything out.
Here are the steps. Before getting started it would make things easier if you create a new directory in your temp directory to hold the files we are about to download. We will call this the download directory

Step 1 - Download the Java Development Environment

Go to the page http://java.sun.com/j2se/1.3/. Download the JDK 1.3 software by selecting your operating system and clicking the Continue button on the next page. You will be shown a licensing agreement. Click Accept. Upon clicking the Accept button a page will appear that contains a series of download sites. Download the file to your download directory. This is a huge file - almost 35 megabytes - and it will take several hours to download over a normal modem. The next two files are also large.

Step 2 - Download the Java Documentation

The documentation is also reached by going to http://java.sun.com/j2se/1.3/. Download the documentation by selecting your operating system and clicking the JDK 1.3 documentation link on the next page.

Step 3 - Download and install WinZip

If you do not have a version of WinZip or an equivalent on your machine, go to the page http://www.winzip.com/ and download an evaluation copy of WinZip. Run the EXE you get to install it. We will use it in a moment to install the documentation.

Step 4 - Install the Development Kit

Run the j2sdk-1_3_1-*.exe file that you downloaded in step 1. It will unpack and install the development kit automatically.

Step 5 - Install the Documentation

Read the installation instructions for the documentation. They will instruct you to move the documentation file to same directory as that containing the development kit you just installed. Unzip the documentation and it will drop into the proper place.

Step 6 - Adjust Your Environment

If you read , they will tell you to change your path variable by modifying your autoexec.bat file (in Win95) to modify your PATH environment variable. This is most easily done by opening an MS-DOS prompt and typing PATH to see what the path is set to currently. Then open autoexec.bat in Notepad and make the changes to PATH specified in the instructions.

Step 7 - Test

Now you should be able to open another MS-DOS window and type javac. If everything is set up properly then you should see a 2-line blob of text come out that tells you how to use javac. That means you are ready to go. If you see the message "Bad Command or File Name" it means you are not ready to go. Figure out what you did wrong by rereading the installation instructions. Make sure the PATH is set properly and working. Go back and reread the Programmer's Creed above and be persistent until the problem is resolved.

You are now the proud owner of a machine that can compile Java programs. You are ready to start writing software! By the way, one of the things you just unpacked is a demo directory full of neat examples. All of the examples are ready to run, so you might want to find the directory and play with some of the samples. Many of them make sounds, so be sure to turn on your speakers. To run the examples, find pages with names like example1.html and load them into your normal Web browser

Share: 

 

3 Answers Found

 
Answer #1    Answered On: Nov 24    

Your First Program
Your first program will be short and sweet. It is going to create a drawing area and draw a diagonal line across it. To create this program you will need to:

Open notepad and type in (or cut and paste) the program
Save the program
Compile the program with the Java compiler to create a Java Applet
Fix any problems
Create an HTML web page to "hold" the Java Applet you created
Run the Java applet
Here is the program we will use for this demonstration:

import java.awt.Graphics;
public class FirstApplet extends java.applet.Applet
{
public void paint(Graphics g)
{
g.drawLine(0, 0, 200, 200);
}
}
Step 1 - Type in the Program

Create a new directory to hold your program. Open up Notepad (or any other text editor that can create TXT files). Type or cut and paste the program into the Notepad window. This is important: when you type the program in , CASE MATTERS. That means that you must type the upper and lower case characters exactly as they appear in the program. Review the programmer's creed above. If you do not type it EXACTLY as shown, it is not going to work.
Step 2 - Save the File

Save the file to the filename FirstApplet.java in the directory that you created in step 1. CASE MATTERS in the filename. Make sure the 'F' and 'A' are upper case and all other characters are lower case, as shown.
Step 3 - Compile the Program

Open an MS-DOS window. Change directory ("cd") to the directory containing FirstApplet.java. type:

javac FirstApplet.java
CASE MATTERS. Either it will work, and in that case nothing will be printed to the window, or there will be errors. If there are no errors a file named FirstApplet.class will be created in the directory right next to FirstApplet.java.

[Make sure that the file saved to the name FirstApplet.java and not FirstApplet.java.txt. This is most easily done by typing dir in the MS-DOS window and looking at the file name. If it has a .txt extension, remove it by renaming the file. Or run the Windows Explorer and select the Options option in the View menu. Make sure that the "Hide MD-DOS File Extensions for file types that are registered" check box is NOT checked, and then look at the filename with the explorer. Change it if necessary.]

Step 4 - Fix Any Problems

If there are errors fix them. Compare your program to the program above and get them to match exactly. Keep recompiling until you see no errors. If javac seems to not be working, look back at the previous section and fix your installation.
Step 5 - Create an HTML Page

Create an HTML page to hold the applet. Open another Notepad window. Type into it the following:

<html>
<body>
<applet code=FirstApplet.class width=200 height=200> </applet>
</body>
</html>
Save this file in the same directory with the name applet.htm.

Step 6 - Run the Applet

In your MS-DOS window type


appletviewer applet.htm


You should see a diagonal line running from the upper left corner to the lower right corner:

Pull the applet viewer a little bigger to see the whole line. You should also be able to load the HTML page into any modern browser like Netscape Navigator or Microsoft Internet Explorer and see approximately the same thing.

You have successfully created your first program!!!

 
Answer #2    Answered On: Nov 24    

Understanding What Just Happened

So what just happened? First, you wrote a piece of code for an extremely simple Java applet. An applet is a Java program that can run within a Web browser, as opposed to a Java Application which is a stand-alone program that runs on your local machine (Java applications are slightly more complicated and somewhat less popular, so we will start with applets). We compiled the applet using javac. We then created an extremely simple web page to "hold" the applet. We ran the applet using appletviewer, but you can just as easily run it in a browser.

The program itself is about 10 lines long:


import java.awt.Graphics;
public class FirstApplet extends java.applet.Applet
{
public void paint(Graphics g)
{
g.drawLine(0, 0, 200, 200);
}
}
This is about the simplest Java applet you can create. To fully understand it you will have to learn a fair amount, particularly in the area of object oriented programming techniques. Since I am assuming that you have zero programming experience, what I would like you to do is focus your attention on just one line in this program for the moment:


g.drawLine(0, 0, 200, 200);
This is the line in this program that does the work. It draws the diagonal line. The rest of the program is scaffolding that supports that one line, and we can ignore the scaffolding for the moment. What happened here was that we told the computer to draw one line from the upper left hand corner (0,0) to the bottom right hand corner (200, 200). The computer drew it just like we told it to. That is the essence of computer programming!

[Note also that in the HTML page we set the size of the applet's window in step 5 above to have a width of 200 and a height of 200.]

In this program we called a method (a.k.a. a function) called drawLine and we passed it 4 parameters (0, 0, 200, 200). The line ends in a semicolon. The semicolon acts like the period at the end of the sentence. The line begins with g., signifying that we want to call the method named drawLine on the specific object named g (which you can see one line up is of the class Graphics - we will get into classes and methods of classes in much more detail later in this article).

A method is simply a command - it tells the computer to do something. In this case, drawLine tells the computer to draw a line between the points specified: (0, 0) and (200, 200). You can think of the window as having its 0,0 coordinate in the upper left corner, with positive X and Y axes extending to the right and down. Each dot on the screen (a pixel) is 1 increment on the scale.

Try experimenting by using different numbers for the 4 parameters. Change a number or two, save your changes, recompile with javac and rerun after each change in appletviewer, and see what you discover.

What other functions are available besides drawLine? You find this out by looking at the documentation for the Graphics class. When you installed the Java development kit and unpacked the documentation, one of the files unloaded in the process is called java.awt.Graphics.html, and it is on your machine. This is the file that explains the Graphics class. On my machine the exact path to this file is D:\jdk1.1.7\docs\api\java.awt.Graphics.html. On your machine the path is likely to be slightly different but close - it depends on exactly where you installed things. Find the file and open it. Up toward the top of this file there is a section called "Method Index". This is a list of all of the methods this class supports. The drawLine method is one of them but you can see MANY others. You can draw:

Lines
Arcs
Ovals
Polygons
Rectangles
Strings
Characters
etc.
Read about and try experimenting with some of these different methods to discover what is possible. For example, try this code:


g.drawLine(0, 0, 200, 200);
g.drawRect(0, 0, 200, 200);
g.drawLine(200, 0, 0, 200);


It will draw a box with 2 diagonals (be sure to pull the window big enough to see the whole thing). Try drawing other shapes. Read about and try changing the color with the setColor method. For example:


import java.awt.Graphics;
import java.awt.Color;
public class FirstApplet extends java.applet.Applet
{
public void paint(Graphics g)
{
g.setColor(Color.red);
g.fillRect(0, 0, 200, 200);
g.setColor(Color.black);
g.drawLine(0, 0, 200, 200);
g.drawLine(200, 0, 0, 200);
}
}


Note the addition of the new import line in the 2nd line of the program. The output of this program looks like this:

One thing that might be going through your head right now is, "How did he know to use Color.red rather than simply red, and how did he know to add the second import line?" You learn things like that by example. Because I just showed you an example of how to call the setColor method, you now know that whenever you want to change the color you will use Color. followed by a color name as a parameter to the setColor method, and you will add the appropriate import line at the top of the program. If you look up setColor it has a link that will tell you about the Color class, and in it is a list of all the valid color names along with techniques for creating new (unnamed) colors. You read that information, you store it in your head and now you know how to change colors in Java. That is the essence of becoming a computer programmer - you learn techniques and remember them for the next program you write. You learn the techniques either by reading an example (as you did here) or by reading through the documentation or by looking at example code (as in the demo directory). If you have a brain that likes exploring and learning and remembering things, then you will love programming!

In this section you have learned how to write linear, sequential code - blocks of code that consist of method calls starting at the top and working toward the bottom (try drawing one of the lines BEFORE you draw the red rectangle and watch what happens - it will be covered over by the rectangle and made invisible. The order of lines in the code sequence is important). Sequential lines of code form the basic core of any computer program. Experiment with all the different drawing methods and see what you can discover.

 
Answer #3    Answered On: Nov 24    

Bugs and debugging

One thing that you are going to notice as you learn about programming is that you tend to make a fair number of mistakes and assumptions that cause your program either: 1) to not compile, or 2) to produce output that you don't expect when it executes. These problems are referred to as bugs and the act of removing them is called debugging. About half of the time of any programmer is spent debugging.

You will have plenty of time and opportunity to create your own bugs, but to get more familiar with the possibilities let's create a few. In your program, try erasing one of the semicolons at the end of a line and try compiling the program with javac. The compiler will give you an error message. This is called a compiler error, and you have to eliminate all of them before you can execute your program. Try mispelling a function name, leaving out a "{", eliminating one of the import lines, etc. to get used to different compiler errors. The first time you see a certain type of compiler error it can be frustrating, but by experimenting like this with known errors that you create on purpose you can get familiar with many of the common errors.

A bug, also known as an execution (or run-time) error, occurs when the program compiles fine and runs, but then does not produce the output you planned on it producing. For example, this code produces a red rectangle with two diagonal lines across it:


g.setColor(Color.red);
g.fillRect(0, 0, 200, 200);
g.setColor(Color.black);
g.drawLine(0, 0, 200, 200);
g.drawLine(200, 0, 0, 200);
The following code, on the other hand, produces just the red rectangle (which covers over the two lines):


g.setColor(Color.black);
g.drawLine(0, 0, 200, 200);
g.drawLine(200, 0, 0, 200);
g.setColor(Color.red);
g.fillRect(0, 0, 200, 200);
The code is almost exactly the same but looks completely different when it executes. If you are expecting to see two diagonal lines then the code in the second case contains a bug.

Here's another example:


g.drawLine(0, 0, 200, 200);
g.drawRect(0, 0, 200, 200);
g.drawLine(200, 0, 0, 200);
This code produces a black outlined box and two diagonals. This next piece of code produces only one diagonal:


g.drawLine(0, 0, 200, 200);
g.drawRect(0, 0, 200, 200);
g.drawLine(0, 200, 0, 200);
Again, if you expected to see two diagonals then the second piece of code contains a bug (look at the second piece of code until you understand what went wrong). This sort of bug can take a long time to find because it is subtle.

You will have plenty of time to practice finding your own bugs. As I say, the average programmer spends about half of his or her time tracking down, finding and eliminating bugs. Try not to get frustrated when they occur - they are a normal part of programming life.

 
Didn't find what you were looking for? Find more on How Computer Programs Work Or get search suggestion and latest updates.




Tagged: