phone: 016-6933793
e-mail: darknessofsorrow@hotmail.com

Sunday 16 December 2012

ARRAYS


Arrays in Java
We looked at the basics of Java's built-in arrays, starting from creating an array to iterating over the elements of an array, as well as at a few typical array manipulations in class today. See the link to a complete implementation for you to play with at the end of this document.

Topics, in no particular order:

1. Creating an array
2. Iterating over the elements of an array
3. Copying an array
4. Resizing an array
5. Reversing an array
6. Shifing an array left
7. Shifing an array right
8. Inserting an element into an array
9. Removing an element from an array
10. Rotating an array left
11. Rotating an array right

Creating an array
In Java, you create a new array in the following:

Foo[] x = new Foo[100];

Now x refers to an array of 100 Foo references. Note that the array doesn't actually contain the instances of Foo (the objects that is), but rather hold the references to the objects. We can assign other references to this array:

Foo[] y = x;

Now y is just another reference to the same array, and any change made through y will change the array referred to by x.

A Java array has a single attribute called length that gives you the capacity of the array; x.length for example would produce the value 100 in this case. The capacity of an array is fixed, and once created, cannot be changed. We "resize" and array by creating a new one with higher capacity, and copying the elements to the new one. See resizing an array for details.

Iterating over the elements of an array
Iterating over the elements of an array is the same as the following: for each element v in the array x, do something with v. There are two traditional patterns for iterating over the elements of an array: using a while or a for loop. Let's print the elements of the array x using a while loop (here the doing something is printing the element):

int i = 0;
while (i < x.length) {
      System.out.println(x[i]);
      i++;
}

And then using for loop:

for (int i = 0; i < x.length; i++)
    System.out.println(x[i]);

Java has another form of the for loop to implement this "foreach" pattern:

for (Foo v : x)
    System.out.println(v);

This version of the for has one advantage: it does exactly what it says - for each element v in the array x, it prints the element v! No indexing needed at all. It also has a disadvantage: there is no way to iterate over a part of the array, which is important when the size is not equal to the capacity of the array.

Copying an array
Copying the elements of a source array to destination array is simply a matter of copying  the array element by element using an iterator.

public static Object[] copyArray(Object[] source) {
    Object[] copy = new Object[source.length];
    for (int i = 0; i < source.length; i++)
        copy[i] = source[i];
    return copy;
}

For you information, the java.util.Arrays class provides a set of methods to do just this for you in a very efficient way. Here's how:

public static Object[] copyArray(Object[] source) {
    Object[] copy = java.util.Arrays.copyOf(source, source.length);
    return copy;
}

Resizing an array
There is the classic problem of arrays - once created, it cannot change its capacity! The only way to "resize" an array is to first create a new and larger array, and copy the existing elements to the new array. The following static method resizes oldArray to have a capacity of newCapacity, and returns a reference to the resized array.

static Object[] resize(Object[] oldArray, int newCapacity) {
    Object[] newArray = new Object[newCapacity];
    for (int i = 0; i < oldArray.length; i++)
        newArray[i] = oldArray[i];
    return newArray;
}

We can use this method in the following way:

Object[] data = new Object[10];
for (int i = 0; i < 10; i++)
     data[i] = new String(String.valueOf(i));

// The array "data" is now full, so need to resize before we can
// add more elements to it.
data = resize(data, 20);
for (int i = 10; i < 20; i++)
    data[i] = new String(String.valueOf(i));

Reversing an array
The simplest way of reversing an array is to first copy the elements to another array in the reverse order, and then copy the elements back to the original array. This out-of-place method is rather inefficient, but it's simple and it works.

public static void reverse(Object[] array) {
    Object[] tmpArray = new Object[array.length];
    int i = 0;                         // index into array
    int j = tmpArray.length - 1;        // index into the reverse copy
    while (i < array.length) {
        tmpArray[j] = array[i];
        i++;
        j--;
   }
   // Now copy the elements in tmpArray back into the original array.
   for (int i = 0; i < array.length; i++)
       array[i] = tmpArray[i];

  // NOTE: the following DOES NOT work! Why?
  // array = tmparray;
}

Fortunately, there is an in-place method that is far more efficient!

public static void reverse(Object[] array) {
    int i = 0;                          // forward index into left half
    int j = array.length - 1;           // backward index into right half
    while (i < j) {
        // Exchange array[i] with array[j]
        Object tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
                   
        i++;
        j--;
    }
}

Shifing an array left
Shifting an entire array left moves each element one (or more, depending how the shift amount) position to the left. Obviously, the first element in the array will fall off the beginning and be lost forever. The last slot of the array before the shift (ie., the slot where where the last element was until the shift) is now unused (we can put a null there to signify that). The size of the array remains the same however, because the assumption is that you would something in the now-unused slot. For example, shifing the array [5, 3, 9, 13, 2] left by one position will result in the array [3, 9, 13, 2, -]. Note how the array[0] element with value of 5 is now lost, and there is an empty slot at the end (shown as - above).

public static void shiftLeft(Object array[]) {
    for (int i = 1; i < array.length; i++)
        array[i - 1] = array[i];
    array[a.length - 1] = null;        // Now empty
}

What would happen if this were a circular or cyclic array? See show to rotate an array left.

Shifing an array right
Shifting an entire array right moves each element one (or more, depending how the shift amount) position to the right. Obviously, the last element in the array will fall off the end and be lost forever. The first slot of the array before the shift (ie., the slot where where the 1st element was until the shift) is now unused (we can put a null there to signify that). The size of the array remains the same however, because the assumption is that you would something in the now-unused slot. For example, shifing the array [5, 3, 9, 13, 2] right by one position will result in the array [-, 5, 3, 9, 13]. Note how the array[4] element with value of 2 is now lost, and there is an empty slot in the beginning (shown as - above).

public static void shiftRight(Object array[]) {
    for (int i = array.length - 1; i > 0; i--)
        array[i] = array[i - 1];
    array[0] = null;                    // Now empty.
}

What would happen if this were a circular or cyclic array? See show to rotate an array right.

Inserting an element into an array
Inserting an element into any slot in an array requires that we first make room for it by shifting some of the elements to the right, and then insert the new element in the newly formed gap. The only time we don't have to shift is when we insert in the next available empty slot in the array (the one after the last element). The insertion also assumes that there is at least one empty slot in the array, or else it must be resized as we had done earlier (or, if it's non-resizable by policy, then we can throw an exception). For example, inserting the value 7 in the slot with index 2 in the array [3, 9, 12, 5] produces the array [3, 9, 7, 12, 5].

// Insert the given element at the given index in the non-resizable array
// with size elements.
public static void insert(Object[] array, int size, Object elem, int index) {
    if (size == array.length)
        throw new RuntimeException("no space left");
    else {
        // make a hole by shifting elements to the right.
        for (int i = index; i < size; i++)
            array[i + 1] = array[i];

        // now fill the hole/gap with the new element.
        array[index] = elem;
   }
}

Removing an element from an array
After removing the element at a given index, we need to plug the hole by shifting all the elements to its right one position to the left.

// Removes the element at the given index from the array with size elements.
public static void remove(Object[] array, int size, int index) {
    // Shift all elements [index+1 ... size-1] one position to the
    // left.
    for (int i = index + 1; i < size; i++)
        array[i - 1] = array[i];

    // Now nullify the unused slot at the end.
    array[size - 1] = null;
}

Rotating an array left
Rotating an array left is equivalent to shifting a circular or cyclic array left — the 1st element will not be lost, but rather move to the last slot. Rotating the array [5, 3, 9, 13, 2] left by one position will result in the array [3, 9, 13, 2, 5].

public static void rotateLeft(Object array[]) {
    Object firstElement = array[0];
    for (int i = 1; i < array.length; i++)
        array[i - 1] = array[i];
    array[a.length - 1] = firstElement;
}

There is a much more elegant solution that we'll see when we discuss circular or cyclic arrays (wait till we study the Queue ADT).

Rotating an array right
Rotating an array right is equivalent to shifting a circular or cyclic array right — the last element will not be lost, but rather move to the 1st slot. Rotating the array [5, 3, 9, 13, 2] right by one position will result in the array [2, 5, 3, 9, 13].

public static void rotateRight(Object array[]) {
    Object lastElement = array[array.length - 1];
    for (int i = array.length - 1; i > 0; i--)
        array[i] = array[i - 1];
    array[0] = lastElement;
}

There is a much more elegant solution that we'll see when we discuss circular or cyclic arrays (wait till we study the Queue ADT).

You can look at the example ArrayExamples.java class to see how these can be implemented. Run the ArrayExamples.main() to see the output.

Friday 2 November 2012

Computer System Organization



                                                HARDWARE                                       SOFTWARE        

Examples:CD-ROM, monitor, printervideo card, scanners , label makers, routers , and modems.Quickbooks, Adobe Acrobat, Winoms-Cs, Internet Explorer ,Microsoft Word , Microsoft Excel
Types:Input,storage,processing,control, and output devices.System software, Programming software, and Application software.
Inter dependency:Hardware starts functioning once software is loaded.To deliver its set of instructions, Software is installed on hardware.
Function:Hardware serve as the delivery system for software solutions. The hardware of a computer is infrequently changed, in comparison with software and data, which are “soft” in the sense that they are readily created, modified, or erased on the computTo perform the specific task you need to complete. Software is generally not needed to for the hardware to perform its basic level tasks such as turning on and reponding to input.
Reliability:Hardware stays at steady reliability level in useful life.Software needs constant testing after upgrades.
Failure:Hardware failure is random. Hardware does have increasing failure at the last stage.Software failure is systematic. Software does not have an increasing failure rate.
Fault:Hardware faults is physical.Software faults are not.
Lifetime:Hardware wears out over time.Software does not wear out over time.
Nature:It is physical in natureIt is logical in nature
Definition:Devices required to store and execute (or run) the software.Collection of instructions that enables a user to interact with the computer. Software is a program that enables a computer to perform a specific task, as opposed to the physical components of the system (hardware).
Simple small definition:Anything which we can see when the computer is off is hardware.Anything which we can see on the screen when the computer is on is software.

                                                               HARDWARE



     SOFTWARE

Thursday 1 November 2012

Hardware

COMPUTER







MOTHERBOARD


The main circuit board of a microcomputer. the motherboard contains the connectors for attaching additional boards. Typically, the motherboard contains the CPU, BIOS, memory, mass storage interfaces, serial and parallel ports, expansion slots, and all the controllers required to control all the standard peripheral devices, such as the display screen, keyboard, and disk drive; sometimes you will find that the motherboard has other integrated devices such as an audio card and video card. The motherboard is the largest circuit board in most computers and is held by several screws.


CPU

CPU (Central Processing Unit) is the 'brain' that control the internal activities of a computer. Also know as the 'microprocessor', it processes everything from basic instructions to complex functions using its components such as control unit, arithmetic logic unit and register. It is the most expensive part of a computer and the reliability of a computer system is determined by the quality of its processor.


ALU (Arithmetic Logic Unit) - This unit performs the computing functions involving integers (whole numbers).

   # Arithmetic operations which include addition, subtraction, multiplication     and division.

   # All logic operations which involve comparisons such as less than, greater than, or equal to.


All information in a CPU is reduced to a numeric function; therefore, the ALU is constantly handling arithmetic and logic operations.

The results of these operations are stored in the registers or in memory or sent to output devices.

Registers - All data is temporarily stored in registers during the execution of programs and the size of the internal register determines how much information the CPU can process at one time.

Control Unit - This unit handles the timing and control signals to all the operations in the system. You can compare the control unit to a police officer directing traffic at a busy intersection; but, instead of directing cars, the control unit directs data flow between the computer processor, memory and peripherals.

InputThis is the process by which external data is received into the computer. This could either be running a program or getting keyboard responses. Common inputs include a keyboard, mouse, modem, scanner, etc. The computer system analyzes the input data.


Output - Output is the process by which the CPU sends data to devices such as the monitor, printer, disk drive, etc. Output takes the results of the processing and sends them to be stored in memory or printed or displayed. Output is the final result of the processing of the data that was input and used by the computer system.

Data must be stored either temporarily or permanently. Therefore, this is where memory is required. The computer needs a way to hold onto data as the processing is being performed.

Memory - The computer stores data in memory and retrieves the data it needs from memory. There are two kinds of memory - ROM and RAM.

ROM (Read-Only Memory) is permanent and will be retained even when the computer is turned off - eg. BIOS.

RAM (Random Access Memory) is volatile and data stored in RAM will be erased when the computer is turned off. The processor uses RAM to store data and retrieves data from RAM as it's needed. The instructions of a program, for example, would be stored in RAM. RAM will be lost if the computer loses power.


RAM (PRIMARY STORAGE)

RAM (random access memory) is the place in a computer where the operating system, application programs, and data in current use are kept so that they can be quickly reached by the computer's processor. RAM is much faster to read from and write to than the other kinds of storage in a computer, the hard disk, floppy disk, and CD-ROM. However, the data in RAM stays there only as long as your computer is running. When you turn the computer off, RAM loses its data. When you turn your computer on again, your operating system and other files are once again loaded into RAM, usually from your hard disk.




SECONDARY STORAGE

Secondary storage is commonly referred to as hard disk. It is a non-volatile storage and is capable of storing large amounts of data. As secondary storage is non-volatile, it is used for safe or offline storage of data and all the data can be stored permanently.


INPUT DEVICE

Keyboard
Mouse
Joystick
Scanner
Light pen
Voice recorder
Graphic tablet
Webcam

OUTPUT DEVICE

Monitor 
Speaker
Printer
Plotter
Projector








Wednesday 31 October 2012

Software

Software consists of programs written to perform specific tasks. There are 2 types of software - SYSTEM SOFTWARE and APPLICATION SOFTWARE.




SYSTEM SOFTWARE

# System programs that control the computer.
# The operating system is an example of system software. It is the first to load when turning on a computer.
# It handles memory management, input and output activitities, storage management.

Example:
Microsoft Windows, Apple's macintosh, Linux, UNIX, Mac OS

APPLICATION SOFTWARE

* Perform a specific task such as using computer word processors, games, java.....
* All application programs are written using computer programming language.

Example:
Microsoft Word, Notepad, Windows Media Player, MP3 Converter, Adobe Photoshop

Tuesday 30 October 2012

Programming Languages


The Different Generations of Languages


There are currently five generations of computer programming languages. In each generation, the languages syntax has become easier to understand and more human-readable.

First generation languages (abbreviated as 1GL)
Represent the very early, primitive computer languages that consisted entirely of 1's and 0's - the actual language that the computer understands (machine language). Also known as low level language.

Examples: 011100001, 1000001

Second generation languages (2GL)
Represent a step up from from the first generation languages. Allow for the use of symbolic names instead of just numbers. Second generation languages are known as assembly languages. Code written in an assembly language is converted into machine language (1GL).

Examples: LOAD 3, STOR 4, ADD

Third generation languages (3GL)
With the languages introduced by the third generation of computer programming, words and commands (instead of just symbols and numbers) were being used. These languages therefore, had syntax that was much easier to understand. Third generation languages are known as "high level languages" and include C, C++, Java, and Javascript, among others.

Examples: Marks = assignment + exam

Fourth generation languages (4GL)
The syntax used in 4GL is very close to human language, an improvement from the previous generation of languages. 4GL languages are typically used to access databases and include SQL and ColdFusion, among others.

Fifth generation languages (5GL)
Fifth generation languages are currently being used for neural networks. A neural network is a form of artificial intelligence that attempts to imitate how the human mind works.

Monday 29 October 2012

Java





Java is a high-level programming language developed by Sun Microsystems. Java was originally called OAK, and was designed for handheld devices and set-top boxes. Oak was unsuccessful so in 1995 Sun changed the name to Java and modified the language to take advantage of the burgeoning World Wide Web. 

Java is an object-oriented language similar to C++, but simplified to eliminate language features that cause common programming errors. Java source code files (files with a .java extension) are compiled into a format called bytecode (files with a .class extension), which can then be executed by a Java interpreter. Compiled Java code can run on most computers because Java interpreters and runtime environments, known as Java Virtual Machines (VMs), exist for most operating systems, including UNIX, the Macintosh OS, and Windows.




PROCESSING A JAVA PROGRAM

 # Using editor, write Java code using Java syntax. This is what we called the source code.

 # Compile the source code for correctness of syntax and translate into bytecode.

 # Run the program. A program in JDK(Java Development Kit) library which called Linker links the bytecode with necessary code residing in the library.

 # Loader : Transfers the compiled code(bytecode) into main memory.

 # Interpreter : Reads and translates each bytecode instruction into machine language and then executes the statements in a program written in a high-level language.