Introduction to Objects, Arrays, and Java


Introduction to Objects

An object is a representation of an person, place or thing. Think of it as a noun.

An object has attributes (properties) that describe it. Think of these as adjectives.

An object has methods, things you can do with it. Think of these as verbs.

A class is the code file created to represent the object, including its attributes and methods.

Two critical concepts in object-oriented programming are Extends and Implements

Introduction to Arrays

An array is a collection of items of the same type.

Think of a top ten list, or the multiplication table.  Each position in the list or table is referenced by one or two indexes.

Introduction to Java

Installing the Java Developers Kit (JDK)

  1. Locate the "Java2 SDK\Win32" folder on the CD-ROM.
  2. Locate the "jdk1_2_1-win.exe" file.
  3. Double-click on it to run the installation program.
  4. Make a note of the folder the jdk is installed in.

Using the JavaDocs

The JavaDocs are the documentation for the Java class that come with the JDK. They list all classes, and tell you all attributes and methods for each.

  1. Locate the "Java2 SDK\Win32\jdk1.2.1\docs\api" folder on the CD-ROM.
  2. Locate the "index.html" file.
  3. Double click on it to open the JavaDocs in your browser

The JavaDocs page is divided into three frames:

  1. The upper-left frame shows a listing of all packages.
  2. The lower-left frame shows a listing of all classes for the selected package.
  3. The right frame shows the details for the attributes and methods of the selected class.

Setting up the Java Prompt

Refer to javaPrompt.html.

Java is not a Windows program.  Therefore, we will using some rather primitive methods. We'll be using NotePad/TextPad for our editor.  We'll be compiling and running from a DOS window.

The DOS window needs to know where to look for the Java programs.  It does this by setting a PATH variable.

When you open the DOS window, immediately check for this error:

"Out of Environment Space"

If you get this error (we got it a lot last semester), that means the computer may not have set the PATH variable.  You can do this yourself by typing the following in the DOS window, then pressing <ENTER>:

PATH=%PATH%;c:\jdk1.2.1\bin

A caution when setting paths:  Sometimes, the path to a program may contain spaces or be longer than 8 characters.  For example, Windows often installs programs in the "Program Files" folder.  Under Windows 98, it was necessary to enclose the specific path in quotes.  Windows 2000 relaxes the restriction, but does not recognize a path with quotes.

Basic Java Architecture

The basic unit is the Java class file, which contains attributes and methods.

Class files are grouped together based on function. These groups are called packages.

Writing Your First Java Program

The class signature

public class myClass {}

Java is CASE SENSISTIVE The class name must be exactly the same as the name of the file, and the extension must be "java".

Compiling Your Java Program

  1. Open the Java Prompt.
  2. Change to the folder where your Java file is located.
  3. Type "javac" followed by the full name of your code file, then press <ENTER>

You must SAVE and RECOMPILE every time you make a change to the source code.

Errors and Problems

"Bad command or file name"

This happens because the "javac.exe" program can't be found. This has to do with your PATH environment variable. It must contain the path to your java bin folder.

Compile Errors

These errors are syntax errors, you didn't follow the rules of the language. Your program will not compile.

Run-Time Errors

These errors occur when something happens in the system environment which causes the program to fail. This could be hardware or software failures.

Logical Errors

These errors occur when your program does something unexpected. A computer is very literal, it may not be doing what you thought you asked it to do.

Types of Java Programs

Basically, three types of Java Programs. Each has their own unique method signature.

Applications

public static void main(String args[]) {}

Applets

public void init() {}

Components

Components use a special method called a constructor. We'll get into more detail on this topic later in the course.

Standard Input and Output

Standard input is usually the keyboard, and output the screen. For most of our programs, we will be passing information in from the command line, and sending output to the screen. We will be writing Applications, requiring the "main" method indicated above. The input to the program will be stored in the "args[]" parameter. Input is passed on the command line when you run the program:

java MyClass hello

The word "hello" is a command line argument passed into the main method. The output will be sent to the screen using System.out.println().

Coding Standards

Indenting

Commenting

Naming Conventions

About the Homework