What you need...
1) You need to download the JAVA SDK(Software Development Kit) - http://java.sun.com/j2se/1.3/
2) Download the Java Documentation
3) Install the SDK & documentation
4) A text editor - I use CodeWhiz by Incatec.
Another editor I like is TextPad
by Helios Inc.
5) Start writing programs AND EXPERIMENT.
I won't include the instructions for installing Java - it's included on the sun website that I gave above. If you do end up having a problem or just a question - e-mail me and I'll see if I can answer it. I'm gearing this tutorial toward Windows, please make the required directory and file name changes if you are using any other operating system.
Basically the installation will install the Java compiler and the JRE (Java Runtime Environment). One thing a lot of beginners don't realise is that the source code to the base Java objects is also included in various zip files. We will be going over this later on. This source code can give a lot of information on how the base objects work, how they can be used and how they can be expanded. It also is a good place to go to get some working sample code and how to develop in Java.
The compiler translates the code that you write into bytecode that the JRE runs. Unlike other languages - such as C/C++, PowerBuilder and others - Java is interpreted. For example - C++ code is compiled into an EXE or DLL file- code that is specific to the operating system of the compiler. The Java compiler translates the written code to a .class file. This code can be run on any machine, whether it's on Linux, Windows, MacOS or any other machine that has the Java Runtime Environment, without having to be recompiled.. There is one short coming to this - don't expect the speed of C with Java. So - if you're thinking of developing video games then you better start looking at C/C++ and assembly now. Even with this shortcoming - there are a lot of benefits to using Java over C++. One is it's heavy integration into the Web and another is it's network capibilities. Also with C, beginners can get into a lot of trouble. Java has a lot more checks to make sure that the developer doesn't do something that will crash the computer. We'll be getting into all these areas later.
Let's get started with the first program. Most tutorials start out with a Hello
World program - I've created a Smart Hello World program. The .java listing
is the file that you compile. The .txt file is the file that has all the comments.
The only reason I removed all the comments from the .java file is because I
felt it would be easier to read and analyze the code.
NOTE: Upper/Lower case and semicolons are important!
import java.util.*;
import java.text.*;
public class SmartHello
{ public static void main(String[] args)
{ SmartHello a_SmartHello=new SmartHello();
GregorianCalendar todaysDate=new GregorianCalendar();
int hour,
minute,
hour_of_day,
am_pm;
hour=todaysDate.get(Calendar.HOUR);
hour_of_day=todaysDate.get(Calendar.HOUR_OF_DAY);
minute=todaysDate.get(Calendar.MINUTE);
am_pm=todaysDate.get(Calendar.AM_PM);
a_SmartHello.sayTime(hour,minute,am_pm);
if (hour_of_day<12)
{ a_SmartHello.sayGoodMorning();
}
if (hour_of_day>11 && hour_of_day<17)
{ a_SmartHello.sayGoodAfternoon();
}
if (hour_of_day>=17 && hour_of_day<22)
a_SmartHello.sayGoodEvening();
if (hour_of_day>=22)
a_SmartHello.sayGoodNight();
}
private void sayGoodMorning()
{ System.out.println("Good Morning World!");
}
private void sayGoodAfternoon()
{ System.out.println("Good Afternoon
World!");
}
private void sayGoodEvening()
{ System.out.println("Good Evening World!");
}
private void sayGoodNight()
{ System.out.println("Good Night World!
I'm going to bed!!!!");
}
private void sayTime(int hour, int minute, int am_pm)
{ NumberFormat minFormat=NumberFormat.getNumberInstance();
minFormat.setMinimumIntegerDigits(2);
String am_pm_string;
if (am_pm==0)
{ am_pm_string="AM";
}
else
{ am_pm_string="PM";
}
System.out.println("The time
is - "+hour+":"+minFormat.format(minute)+am_pm_string);
}
}
The program is more advanced than what most beginner programs start out with - but all the concepts will be explained. Basically what it does is print the time and then based on the time it prints one of several messages.
This is example output -
The time is - 10:27PM
Good Night World! I'm going to bed!!!!You need to save it as SmartHello.java. You need to name the .java file the same as the public class - in this case SmartHello. Remember that the upper/lower case does matter.
I usually create a directory off the root Java directory and save my programs
there. In my case it's c:\jdk1.3\tutorial. In my c:\jdk1.3\bin
is where the compiler (javac) and jre (java) programs are. You may have different
directory names where your Java is stored. Throughout this tutorial I will be
using my directory structures. You will need to make the necessary changes.
DO NOT just rename the pre-installed Java subdirectories.
If you do, Java will not be able to find all of it's required files.
After you've saved the file - open an MSDos window and changed the directory
to .
c:\jdk1.3\tutorial
Compile the program using
..\bin\javac SmartHello.java
If there were no errors generated you should see a SmartHello.class
file contained in the directory.
Note: the most common errors that you will encounter starting out are misspellings, not paying attention to case and missing semicolons or braces.
Again to run the program make sure you're in the
C:\jdk1.3\tutorial.
Run the program using
..\bin\java SmartHello
Make sure you leave off the .java extension. This is actually running the .class file.
Look at the comments in the code below - this explains many of the basic concepts
of writing a Java program. It also explains how the program works. One of the
best ways of learing to program or another language is to examine other people's
code, making changes and, as I've said, EXPERIMENT. Even getting errors can
teach you about a language.
// double slashes
are used for single line comments
/* slash astrisk followed by an ending
astrisk slash are used for multi-line comments */
// Both these comment types correspond to C and C++ commenting
/* NOTE: Uppercase and lowercase are very important
in Java.
Java is case sensitive.
HelloWorld, helloworld and helloWorld are three different
items.
*/
/* NOTE: the semicolon is required to indicate
the end of a line
of code. Java is free form and ignores most whitespace.
You can see an example of this when we initialize the
integer
variables below - same as C/C++
*/
import java.util.*; /*internal Java Package
we need for
the
GregorianCalendar class */
import java.text.*; //internal Java Package we need
for the NumberFormat class
/* You can look at the packages that are supplied
with Java at
C:\jdk1.3\docs\api\overview-summary.html; this is only if
you installed the documentation. Your documents may be in
a different
subdirectory also. */
/* First we start out with the "main"
class. This name MUST correspond EXACTLY
to the name you give it when you save it - including case.
I saved this
file as "SmartHello.java". We'll go over some items
right now and others
we'll pick up on later such as static, void and public.
*/
public class SmartHello
{ public static void main(String[] args)
/* main is your first method (function)
you need to worry about. It can
appear anywhere in your "main"
(SmartHello) class.
It is the method that first gets run
and starts your application.
Usually you wouldn't have as much
code as I've put into it.
*/
{ SmartHello a_SmartHello=new SmartHello();
/* Here we Create
and Initialize a new class called a_SmartHello.
It is basically a copy
of the SmartHello class. I'll get into
why we need this when
I explain "static".
*/
GregorianCalendar todaysDate=new GregorianCalendar();
/* We create a variable
called todaysDate and initialize it to
the current date and time
using GregorianCalendar().
*/
// initialize
some integer variables.
int hour,
minute,
hour_of_day,
am_pm;
// get the current
hour and set it to the variable "hour"
hour=todaysDate.get(Calendar.HOUR);
/* get the current
hour_of_day (military time) and set it to the
variable "hour_of_day"
*/
hour_of_day=todaysDate.get(Calendar.HOUR_OF_DAY);
// get the current
minute and set to the variable "minute"
minute=todaysDate.get(Calendar.MINUTE);
/* get AM_PM
(this is an integer - 0 is AM; 1 is PM) and set it to
the variable am_pm */
am_pm=todaysDate.get(Calendar.AM_PM);
/* call the sayTime
method and pass it the hour, minute and am_pm
variables */
a_SmartHello.sayTime(hour,minute,am_pm);
/* NOTE:
braces are used to designate blocks of code. Sometimes they
are required, sometimes
they are optional (as is the case with these
if statements). If we
had more than one line of code that has to be
executed within the if
statement then the braces would be required.
*/
// Here we test
to see if it's the morning (midnight to 11:59am)
if (hour_of_day<12)
{ //beginning
brace (start of if statement)
//if the statement is
true then execute the sayGoodMorning method
a_SmartHello.sayGoodMorning();
} //ending brace
(end of if statement)
// Test to see
if hour is greater than 11 and less than 5pm
// NOTE: Hour
is designated in 24 hour time.
if (hour_of_day>11 && hour_of_day<17)
{ a_SmartHello.sayGoodAfternoon();
}
// Here are if
statements where I didn't use the braces.
// Test to see if hour greater than/equal
to 5PM and less than 10PM
if (hour_of_day>=17 && hour_of_day<22)
a_SmartHello.sayGoodEvening();
//execute sayGoodEvening method
// test to see
if hour is 10PM or later
if (hour_of_day>=22)
a_SmartHello.sayGoodNight();
//execute sayGoodNight method
}
//sayGoodMorning method
/* Mehtods must be deliniated by beginning and ending
braces. You can see
this in the main method above and also classes
need to be deliniated by
braces - you can see this with the SmartHello
class. */
private void sayGoodMorning()
{ //beginning brace
//Prints the string "Good Morning World!"
to the DOS window.
System.out.println("Good Morning World!");
} //closing brace
private void sayGoodAfternoon()
{ System.out.println("Good Afternoon World!");
}
private void sayGoodEvening()
{ System.out.println("Good Evening World!");
}
private void sayGoodNight()
{ System.out.println("Good Night World! I'm
going to bed!!!!");
}
/* sayTime contains the arguments
hour, minute and am_pm. These will
take on the values that are passed into
them. */
private void sayTime(int hour, int minute, int am_pm)
{ /*Initialize a number format
called minFormat (minute format). This will be
used to make sure that minutes
are always shown as two digits. */
NumberFormat minFormat=NumberFormat.getNumberInstance();
//Set the minimum
number of digits to 2 for minFormat.
minFormat.setMinimumIntegerDigits(2);
/* Create am_pm_string
as a string class. In Java, string is a
class, not a type as in
some other languages. */
String am_pm_string;
// Check if it's
AM or PM and assign am_pm_string to the correct value
/* I would generally leave these braces
off since it's a one line if
statement. I've included
them here just as an example. */
if (am_pm==0)
{ am_pm_string="AM";
}
else
{ am_pm_string="PM";
}
/* print out the time to the DOS window.
Notice that we use
"minFormat.format(minute)"
to format the "minute" variable properly.
It is actually passing
"minute" into minFormat's format method and then
returning the properly
formatted value (minimum of 2 digits)
as a string*/
/* NOTE:
Java automatically converts the numbers to strings when you
concatenate a number
to a string using the plus(+) sign. */
System.out.println("The time is - "+hour+":"+minFormat.format(minute)+am_pm_string);
}
}
I know that all this is probably confusing right now. This basic program actually contains quite a lot to digest, but it gives you an idea of what is involved in developing full fledged professional programs. We will be going over each part in detail shortly. At that time you will learn about variables, methods, if statements, boolean logic and objects. First, check out....
Again if you have any questions, suggestions or problems - e-mail me at rrosetta@hsonline.net.