Indometric


Jun 24
Tuesday
Java Programming

Basics of Java Programming

  • Sharebar

Introduction

Before embarking on the road to Java programmer certification, it is vital to know the basic terminology and concepts in object-oriented programming (OOP). In this chapter, the accent is on providing an introduction very than an exhaustive coverage. In-deepness coverage of the concepts follows in due course in subsequent chapters of the book.

Java supports the writing of many different kinds of executables: applications, applets, and servlets. The basic fundamentals of a Java application are introduced in this chapter. The ancient adage that practice makes perfect is certainly right when culture a programming language. To encourage programming on the computer, the mechanics of compiling and running a Java application are outlined.

Classes

One of the fundamental ways in which we handle complexity is abstractions. An abstraction denotes the essential properties and behaviors of an object that differentiate it from other stuff. The essence of OOP is modelling abstractions, using classes and stuff. The hard part in this endeavour is finding the right abstractions.

A class denotes a category of stuff, and acts as a blueprint for making such stuff. A class models an abstraction by defining the properties and behaviors for the stuff representing the abstraction. An object exhibits the properties and behaviors defined by its class. The properties of an object of a class are also called attributes, and are defined by fields in Java. A field in a class definition is a variable which can store a value that represents a particular property. The behaviors of an object of a class are also known as operations, and are defined using methods in Java. Fields and methods in a class definition are collectively called members.

An vital distinction is made between the narrow and the implementation that a class provides for its stuff. The narrow defines what services, and the implementation defines how these services are provided by the class. Clients (i.e., other stuff) only need to know the narrow of an object, and not its implementation, in order to avail themselves of the object’s services.

As an example, we will implement different versions of a class that models the abstraction of a stack that can push and pop font. The stack will use an array of font to store the font, and a field to indicate the top element in the stack. Using Unified Modeling Language (UML) notation, a class called CharStack is graphically depicted in Figure 1.1, which models the abstraction. Both fields and method names are shown in Figure 1.1a.

Declaring Members: Fields and Methods

Example 1.1 shows the definition of the class CharStack depicted in Figure 1.1. Its intention is to illustrate the salient features of a class definition in Java, and not effective implementation of stacks.

A class definition consists of a series of member declarations. In the case of the class CharStack, it has two fields:

  • stackArray, which is an array to hold the fundamentals of the stack (in this case font)
  • topOfStack, which denotes the top element of the stack (i.e., index of the last character stored in the array)
  • The class CharStack has five methods that implement the essential operations on a stack:
  • push() pushes a character on to the stack
  • pop() removes and returns the top element of the stack
  • peek() returns the top element of the stack for inspection
  • isEmpty() determines whether the stack is empty
  • isFull() determines whether the stack is full

The class definition also has a method-like declaration with the same name as the class, (2). Such declarations are called constructors. As we shall see, a constructor is executed when an object is made from the class. But, the implementation details in the example are not vital for the present discussion.

Example 1.1 Basic Fundamentals of a Class Definition

// Source Filename: CharStack.java
public class CharStack { // Class name
// Class Declarations:

// (1) Fields:
private char[] stackArray; // The array implementing the stack.
private int topOfStack; // The top of the stack.

// (2) Constructor:
public CharStack(int n) { stackArray = new char[n]; topOfStack = -1; }

// (3) Methods:
public void push(char element) { stackArray[++topOfStack] = element; }
public char pop() { return stackArray[topOfStack--]; }
public char peek() { return stackArray[topOfStack]; }
public boolean isEmpty() { return topOfStack < 0; }
public boolean isFull() { return topOfStack == stackArray.length – 1; }
}


Post Tags: , , , , ,


Post a Comment

 


All content and source © 2010 Indometric. All rights reserved. See our Privacy Policy and DMCA Information