Published: May 28, 2008
By Richard G. Baldwin
Homeschool Programming Notes # 300
This tutorial lesson is part of a continuing series that is designed specifically for teaching computer programming to homeschool students and their parents. Even though the series is designed for homeschool students, everyone is welcome to use the lessons to learn computer programming.
A lofty goal
At the beginning of this series, I established the very lofty goal of providing a set of programming tutorials that are appropriate for students as young as eight years of age (and their parents) and as old as college age or older. I promised to begin with a programming language named Scratch for the younger students; work through a programming language named Alice for middle school, high school, and college students, and then progress into the programming language named Java for high school and college students. This tutorial marks the beginning of the Java phase.
Scratch
The tutorials using Scratch were designed to teach a few fundamental programming concepts while engaging young students in a sensory way to help them develop an interest in computer programming. That is exactly what Scratch was designed to do -- make it easy for beginning programmers to achieve sensory results involving sight and sound in order to capture their interest and to keep them engaged.
|
Alice 2.0
The tutorials using Alice version 2.0 were designed to teach more serious programming concepts, once again using a programming environment designed to keep students engaged by making it relatively easy to create 3D animations and simple games. Although Alice version 2.0 provides an excellent teaching environment, it is not suitable for use as a serious programming language. By that, I mean that it is not suitable for use in developing large-scale software systems.
Java
Java is a serious object-oriented programming language suitable for use in the development of large-scale software systems.
Presumably you have worked your way through the Scratch and Alice tutorials by now and are ready to embark on the Java phase of your programming education. In this lesson, I will help you get started programming using Java. You need to be aware, however, that compared to Scratch and Alice, Java may seem to be a bit boring. Unlike Scratch and Alice, Java was not designed to make it easy to obtain sensory feedback such as 2D or 3D animation and sound. Instead, Java was designed to provide you with the programming tools necessary to develop large-scale, fully object-oriented software systems of the type that you would find in commercial software products.
Along with C++ (see Table of Contents in Resources), Java is what you will need to learn in order to become a professional programmer and/or to obtain advanced placement (see Resources) in a Computer Science curriculum at a college or university.
It's time to get down in the trenches, roll up your sleeves, and start dealing with the gritty details of serious computer programming.
I recommend that you open another copy of this document in a separate browser window and use the following links to easily find and view the figures and listings while you are reading about them.
I recommend that you also study the other lessons in my extensive collection of online programming tutorials. You will find a consolidated index at www.DickBaldwin.com.
Unfortunately, many colleges, including the one where I teach, use a programming concept from the 1980's known as procedural programming for their beginning programming students. Those students are later expected to unlearn much of what they were originally taught about procedural programming and to relearn how to program using a much newer concept known as object-oriented programming.
I disagree with that approach and do not intend to follow it here. The demand in the marketplace is for object-oriented programmers, not for procedural programmers.
Objects from the beginning
From the beginning, your instruction in this series of Java programming lessons will be based on object-oriented concepts. I won't waste your time teaching you procedural programming and then tell you that no one cares about that anyway.
To learn more about the differences between procedural and object-oriented programming, see the link titled Object-Oriented Programming in Resources.
Presumably you are ready to start learning how to write Java programs. However, we aren't to that point yet. I will get to that in the next lesson. First I will teach you with some of the mechanics involved in writing, compiling, and running Java programs.
Three steps are required
The process of writing and running Java programs consists of three steps:
Get an editor
The first step is to get an editor that you can use to prepare your source code. You can use any editor that can produce a plain text file, such as Windows Notepad, for example. You can probably even use your favorite word processing program if you make certain that your output is saved as plain text but I recommend against doing that. Editors that are designed for writing programs often have advantages such as the following:
Numerous editors are available
Numerous editors suitable for use in Java programming are available free of charge. Links to several of them are provided in Resources and you can probably find others if you search the web. Of the editors listed in Resources, the one named jGrasp is probably the best for writing, debugging, and testing Java programs. It comes with an instruction manual in a separate zip file, so you should be sure to also download the instruction manual.
NetBeans
You can also download an editor named NetBeans from Sun as discussed below. However, it is much more than an editor. It is a fully-integrated Java development environment, and the learning curve for using it is rather steep. It is probably overkill for your needs so early in your Java programming education, but you should keep it in mind for later when your programming projects become more complex,
Eclipse
Another development environment that is favored by many professional Java programmers is named Eclipse (see Resources). However, as with NetBeans, it is probably overkill until you start writing large Java projects.
Download the development tools
The next step is to download the Java development tools from Sun's website (see Java Standard Edition (SE) Downloads in Resources). The download choices on Sun's website tend to change as new versions are released, so I can't tell you exactly what you will find once you get there. As of May 2008, you will need to download the item on that webpage identified as follows, but things may change by the time you are ready to download the tools. (The update level will almost certainly have changed, and the version may have changed as well.)
"JDK 6 Update 6 - The Java SE Development Kit (JDK) includes the Java Runtime Environment (JRE) and command-line development tools that are useful for developing applets and applications."
Download the latest version that is available for your operating system.
Don't forget the documentation
You should also download the item that is currently identified as Java SE 6 Documentation.
Installation instructions
As of May 2008, there are also links to installation instructions for the JDK and the documentation. You should probably print and save the installation instructions to use while you are installing the JDK and the documentation.
Should be all you need
These tools, in addition to the editor discussed earlier, provide everything that you will need to compile and execute Java programs for a long time to come. In addition to the Standard Edition (SE), there is also an Enterprise Edition and a Micro Edition available, but you won't need them until you are prepared to do some rather advanced programming.
NetBeans
If you elect to use the NetBeans IDE instead of another editor, you should download the item that includes NetBeans instead of the JDK identified above. As of May 2008, this item is identified as:
"JDK 6 Update 6 with NetBeans 6.1 - This distribution of the Java SE Development Kit (JDK) includes NetBeans IDE, which is a powerful integrated development environment for developing applications on the Java platform."
Install and use your development tools
The next step is to install and use the editor and the development software that you downloaded from Sun and other sources. If you are new to computers as well being new to computer programming, you may need to get some assistance with the installation process from someone who has more experience in installing software.
Compiling and executing a Java application
Figure 1 explains how to compile and execute a Java application from a command prompt. If you compile and execute from within an IDE, you will need to follow the instructions that apply to that particular IDE.
Figure 1. Compiling and executing a Java application.
The following steps assume that you are running under Windows. If you are running under some other operating system, you will need to translate these instructions to that OS. 1. Download and install the Java development tools from Sun. Follow the installation instructions available from Sun. Be sure to also download and install the documentation. 2. Using the editor of your choice, create a source code file with the extension on the file name being .java This file contains your actual Java instructions. (You can copy the sample program from Listing 1 to test your installation.) 3. Open a command-prompt screen and make your current directory to be the directory containing your source file. It doesn't matter which directory the source file is in, but I normally put my files for each Java program in a directory all its own. 4. Assume that the name of the source file is Hello.java. To compile the file, enter the following command at the prompt: javac Hello.java 5. Correct any compiler errors that show up. Once you have corrected all compiler errors, the javac program will execute and return immediately to the command prompt with no output. At that point, the directory should also contain a file named Hello.class. This is the compiled file. 6. To execute the program, enter the following command: java Hello |
A sample program
To give you something to work with, Listing 1 contains the source code for a very simple Java program. Copy this Java source code into your text editor and save it in a file named Hello.java (note that the file name is case sensitive). Then compile and execute this program according to the instructions in Figure 1. When you do, if Java has been properly installed on your system, the program should display the words Hello World on your computer screen.
Listing 1. Source code for a simple Java application.
class Hello{ public static void main(String[] args){ System.out.println("Hello World"); }//end main }//End Hello class |
In case of problems
I can't sit here and tell you what is wrong if your system isn't producing the correct results. However, I can provide some troubleshooting tips that may help you to identify and fix the problem.
Proper output for a java command
If the required output is not produced, go back and check to make sure that you have followed all of the installation and programming instructions correctly. If you can't find anything wrong by reviewing the instructions, enter the command shown in Figure 2. That should produce an output similar to the output shown in Figure 2.
Figure 2. Proper output for a java command.
C:\jnk>java Usage: java [-options] class [args...] (to execute a class) or java [-options] -jar jarfile [args...] (to execute a jar file) ... output deleted for brevity |
Note that I deleted quite a lot of output from Figure 2 for brevity.
Proper output for a javac command
If you get an output similar to Figure 2, enter the command shown in Figure 3.
Figure 3. Proper output for a javac command.
C:\jnk>javac Usage: javac |
That command should produce an output similar to the output shown in Figure 3 where I also deleted a large amount of output for brevity.
Indication of a problem with the path environment variable
If either of those commands produces an output similar to Figure 4, you may have a problem with your path environment variable.
Figure 4. Indication of a problem with the path environment variable.
C:\jnk>java 'java' is not recognized as an internal or external command, operable program or batch file. |
If you don't know what a path environment variable is, you should probably find someone who does and ask them to help you fix the problem. If you do know how to repair a defective path, update it to make certain that it points to the files named java.exe and javac.exe in your Java installation.
Possible classpath problem
If you get an output similar to that shown in Figure 5, that may indicate that you have a problem with your classpath environment variable.
Figure 5. Indication of a possible classpath environment variable problem.
C:\jnk>java Hello Exception in thread "main" java.lang.NoClassDefFoundError: Hello |
Dealing with path and/or classpath problems
If you get an indication of problems with either the path or the classpath, go back and review the installation instructions to see what, if anything, they have to say about those two environment variables. If you don't find a solution there, take a look at this set of installation instructions from a much earlier version to see if the information provided there will help you solve your problem. Pay particular attention to the section that reads "Should I modify the CLASSPATH variable?" Within that section, pay attention to the discussion involving the current directory.
Write, compile, and test a Java program named MyName that displays your name on the command-line screen.
I began by discussing procedural versus object-oriented programming. Then I explained the mechanics of preparing your system for the writing, compiling, testing, and debugging of Java applications.
In the next lesson, you will begin down the path of learning how to write, compile, test, and debug Java applications.
Copyright 2008, Richard G. Baldwin. Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.
Richard has participated in numerous consulting projects and he frequently provides onsite training at the high-tech companies located in and around Austin, Texas. He is the author of Baldwin's Programming Tutorials, which have gained a worldwide following among experienced and aspiring programmers. He has also published articles in JavaPro magazine.
In addition to his programming expertise, Richard has many years of practical experience in Digital Signal Processing (DSP). His first job after he earned his Bachelor's degree was doing DSP in the Seismic Research Department of Texas Instruments. (TI is still a world leader in DSP.) In the following years, he applied his programming and DSP expertise to other interesting areas including sonar and underwater acoustics.
Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.
-end-