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

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

0 comments:

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.

0 comments:

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.




0 comments:

Sunday, 28 October 2012

Class and Object

 
   ~ Object - represents either objects in the real world like automobiles, houses, and employee records or abstractions like colors, shapes, and words.
   ~ Objects have state, behaviour and identity.
      State : The condition of an object at any moment, affecting how
                  it can behave
      Behaviour : Define by a set of methods, invoking a method on an
                  object means that asking the object to perform a task.

  Identity : Each object is unique.
~ Objects are categorized into classes.
~ Each individual object is an instance of a class.


~Class - the definition of a kind of object. It is like a plan or a blueprint for constructing specific objects.


A Class as a blueprint











 Objects that are instantiations of the class Automobile.


~ Class is a caterogy of objects that share the same :
  • Attribute
  •    + A named property of a class that describes a range of values
          that instances of the attribute might hold.
       + Attribute are the way classes encapsulate data.
       + Example :
              Class : Employee
              Attributes : employee_name, employee_id,
                                 position, department etc...
  • Operation
  •    + A behavior of an object
       + Implemented in classes are methods
       + Methods are the identified and invoked by their signature,
          including name, parameters, and return type
       + Example :
               Class : Employee
               Attributes : employee_name, employee_id,
                                  position, department, salary etc...
               Operations : setEmployeeDetails
                                   calculateSalary
                                   printEmployeeDetails




    ~ Programmers often use a simpler graphical notation to summarize some of the main properties of a class. This notation is called a UML class diagram or simply a class diagram.
    *UML - Universal Modeling Lamguage

    A Class Outline as a UML Class Diagram









    ~ Declaration of object & assigning of an object reference to the variable.
                ClassName objectRefVar = new ClassName ( ) ;   
             Example :   Vehicle car = new Vehicle ( ) ;  
    ~After an object is created, its data can be accessed and its methods invoked using the dot operator (.). 
      + objectRefVar . dataField - References a data field in the object
      + objectRefVar . method (arguments) - Invokes a method on the object
         Example : + Car . colour - References the colour in Vehicle
                            class
                         + Car . printColour ( ) ;
                         - Invokes the printColour() method on Car .
                             Methods are invoked as operations on objects.

    0 comments:

    Saturday, 27 October 2012

    Methods

    ~ When using a method, are said to invoke or call it.
    ~ Java has two kinds of methods :
       + Methods that return a single item. Example: method nextInt
       + Methods that perform some action other than returning an item.
          Example: method println
    ~ Method that perform some action other than returning a value are
       called void methods.
    ~ A method defined in a class is usually invoked using an object of that class.

    Defining void Methods
    ~ A method definition consists of a heading (1st part of the method
       definition) and a body (the rest of the method definition).

    ~ Normally, the heading is written on a single line, but if it is too
       long for 1 line, it can be broken into 2 or more lines.
    ~ The statements in the body are enclosed between braces {}.
    ~ Instance variables can be use within the body. Such variables are
       called local variables.
    *Local variable is confined to the method containing its declaration.

    Blocks
    ~ Is a compound statement that declares a local variable.
    ~ Same as a compound statement that is a group of
       Java statements enclosed in braces {}.
    ~ However, the 2 terms tend to be used in different contexts.
    ~ The variables declared in a block are local to the block and these
       variables disappear when the execution of the block ends.

    Parameters of a Primitive Type
    ~ A formal parameter in a method definition represents the argument.
    ~ It is given in the heading, within the parentheses right after the
       method name.
    ~ A formal parameter of a primitive type, such as int, double or
       char is a local variable.
    ~ Example: public int predictPopulation (int years)
       + The word years is a formal parameter.
       + It is a stand-in value that will plugged in when thr method is called.
       + The item that is plugged in is called an argument or in some
          other books called actual parameters.
    ~ The way of plugging in arguments (the value of the argument) for
       formal parameters is known as the call-by-value mechanism.
    ~ This is the only mechanism used for parameters of a primitive
       type in Java.
    ~ The data type of a formal parameter in a method heading is
       written before the parameter.

    Void methods with parameters
    modifier (s) void methodName (formal parameter list) 
    {                                                                                   
           statements                                                            
    }                                                                                   

    Methods Calling Methods
    ~ A method body can contain an invocation of another method.
    ~ If the called method is in the same class, it is typically invoked
       without writing any receiving object.

    0 comments:

    Friday, 26 October 2012

    Basic Computationc( Variables and Expressions )

    ~VARIABLES~


    *Variables is a storage location to hold a temporary values.
    *It used to store data such as number and letters.
    *They are implemented as memory locations.
    *The data stored by a variable is called its value.
    *The value is stored in the memory location.
    *Its value can be changed.
    *All variables must be declared before they can be used.


    #The type of variable:



    
    
    #SIMPLE OF DECLARATION VARIABLE JAVA PROGRAM
    
    
    
    
    ~CONSTANTS~
    *Similar to variables but once initialized, their contents may 
    NOT be changed.
    *Declared with the reserved word final
    *Syntax:
       final datatype CONSTANTNAME = value
    *Example:
       final double CENTIMETERS_PER_INCH =2.54;
     final int NO_OF_STUDENTS = 20;
     final char BLANK = ' ';
       final double PAY_RATE =15.75;
    ~DATA TYPE~
    
    
    *A data type specifies a set of values and their operations.
    *Has two main kinds of data:1)class type  2)primitive types.
    *A class type is used for a class of objects and has both data and methods.
    ="Java is fun" is a value of class type String
    *A primitive type is used for simple,nondecomposable value such as an 
    individual number or individual character.
    =int, double, and char are primitive types.
    # PRIMITIVE TYPES
    example for primitive type
    
    
    *Four type for integers,namely, byte, short, int, long.The only 
    differences among the various integer types are the range of integers they
     represent and the amount of computer memory they used.
    *A number having a fraction part such as the number 9.99, 3.14159,
    -5.67 and 5.0 is called a floating point number.Java have two data 
    types for floating point numbers,float and double.
    *Char is used for single characters such as letters, digits, or punctuation.
    *Boolean has two values, true and false.
    EXAMPLE



    ~JAVA IDENTIFIER~
    *An identifier is a name such as the name of a variable.
    *Identifiers may contain only
         =Letters
         =Digit (0 through 9)
         =The underscore character ( _ )
         =And the dollar sign symbol ( $ ) which has a special meaning
    *The first character cannot be a digit.
    *Identifiers may not contain any spaces, dots (.),asterisks (*), or other
     characters.
    *Since Java is case sensitive,stuff ,Stuff  and STUFF are
    different identifiers.
    
    
    #LEGAL AND ILLEGAL VARIABLE NAMES
    
    
    #Assignment Statements
    *An assignment statement is used to assign a value to a variable
           answer = 42;
    *The "equal sign" is called the assignment operator.
    *We say, "The variable named answer is assigned a value of 42," or
     more simply,"answer is assigned 42."
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    0 comments:

    Thursday, 25 October 2012

    Flow of Control : Loops

    Java Loop Statements: Outline
    • The while statement
    • The do-while statement
    • The for Statement

    Java Loop Statements
    • A portion of a program that repeats a statement or a group of statements is called a loop.
    • The statement or group of statements to be repeated is called the body of the loop.
    • A loop could be used to compute grades for each student in a class.
    • There must be a means of exiting the loop.

    The while Statement
    • Also called a while loop
    • A while statement repeats while a controlling boolean expression remains true
    • The loop body typically contains an action that ultimately causes the controlling boolean expression to become false.
    • Syntax

    while (Boolean_Expression)
    {
           First_Statement
           Second_Statement
           …
    }


    Download, compile, and run WhileDemo


    The do-while Statement
    • Also called a do-while loop
    • Similar to a while statement, except that the loop body is executed at least once
    • Syntax

    do
    {
          Body_Statement(s)
    }    while (Boolean_Expression);


    • Don’t forget the semicolon!
    • Figure 4.3 The action of the do-while loop in DoWhileDemo

    • First, the loop body is executed.
    • Then the boolean expression is checked.
    -  As long as it is true, the loop is executed again.
    -  If it is false, the loop is exited.
    • Equivalent while statement

    Statement(s)_S1
    while (Boolean_Condition) {
         Statement(s)_S1
    }


    Using while to Read Input
    • Download WhileWithScanner.java from the Examples link on the course webpage
    • Compile and run a few times until you are comfortable with what the program does and
    how it does it.

    Infinite Loops
    • A loop which repeats without ever ending is called an infinite loop.
    • If the controlling boolean expression never becomes false, a while loop or a do-while loop will repeat without ending.
    • Make sure a loop contains a statement which causes the boolean expression to become false eventually.

    Nested Loops
    • The body of a loop can contain any kind of statements, including another loop.

    The for Statement
    • A for statement executes the body of a loop a fixed number of times.
    • Example

    for (count = 1; count < 3; count++)
    {
           System.out.println(count);
    }


    • Syntax

    for (Initialization; Condition; Update)
    Body_Statement


    • Body_Statement can be either a simple statement or a compound statement in {}.
    • Corresponding while statement

    Initialization
    while (Condition)
    Body_Statement_Including_Update


    • Download ForDemo

      


    • Possible to declare variables within a for statement
    int sum = 0;
    for (int n = 1 ; n <= 10 ; n++)
    {
          sum = sum + n;
    }

    • Note that variable n is local to the loop

    Controlling Number of Loop Iterations
    • If the number of iterations is known before the
    loop starts, the loop is called a count-controlled
    loop.
    -  Use a for loop.
    • Asking the user before each iteration if it is time
    to end the loop is called the ask-before-iterating
    technique.
    - Appropriate for a small number of iterations
    -  Use a while loop or a do-while loop.

    The break Statement in Loops
    • A break statement can be used to end a loop immediately.
    • The break statement ends only the innermost loop or switch statement that contains the break statement.
    • break statements make loops more difficult to understand.
    • Use break statements sparingly (if ever).
    • Note program fragment, ending a loop with a break statement, listing 4.8


    The continue Statement in Loops
    • A continue statement
    - Ends current loop iteration
    - Begins the next one
    • Text recommends avoiding use
    - Introduces unneeded complications

    Tracing Variables
    • Tracing variables means watching the variables change while the program is running.
     - Simply insert temporary output statements in your program to print of the values of variables of interest
    - Or, learn to use the debugging facility that may be provided by your system.

    Tracing Variables with Drjava
    • Open WhileWithScanner.java
    • Choose Debugger->Debug Mode
    - A new panel is added with Stack and Thread tabs
    • Right-click on the code sum += num; and choose Toggle Breakpoint
    - That line now has a red background
    • Click on the left-right arrows to the left of the Stack tab
     - A new panel is added with a Watches tab
    • Type num into the Name field and hit return
    • Type sum into the next Name field, then return
    • Run the program and type some numbers when promted to do so
    • The program will stop at the breakpoint and show the values of num and sum
    • Click the Step Over button to move to the next statement
    • Click the Resume button to move to the next breakpoint
    • To exit the debugger and the program, click the Reset button located on the top panel

    Loop Bugs
    • Common loop bugs
    - Unintended infinite loops
    - Off-by-one errors
    - Testing equality of floating-point numbers
    • Subtle infinite loops
    - The loop may terminate for some input values, but not for others.
    - Example: you can’t get out of debt when the monthly penalty exceeds the monthly payment.

    0 comments:

    While and Do-While Statements


    The while and do-while Statements

    The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as:
    
    
    while (expression) {
         statement(s)
    }
    

    The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes thestatement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false. Using thewhile statement to print the values from 1 through 10 can be accomplished as in the following WhileDemo program:
    
    
    class WhileDemo {
        public static void main(String[] args){
            int count = 1;
            while (count < 11) {
                System.out.println("Count is: "
                                   + count);
                count++;
            }
        }
    }
    

    You can implement an infinite loop using the while statement as follows:
    
    
    while (true){
        // your code goes here
    }
    

    The Java programming language also provides a do-while statement, which can be expressed as follows:
    
    
    
    
    do {
         statement(s)
    } while (expression);
    
    The difference between do-while and while is that do-while evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once, as shown in the following DoWhileDemo program:
    class DoWhileDemo {
        public static void main(String[] args){
            int count = 1;
            do {
                System.out.println("Count is: "
                                   + count);
                count++;
            } while (count < 11);
        }
    }

    0 comments:

    Wednesday, 24 October 2012

    The For Statement


    The for Statement

    The for statement provides a compact way to iterate over a range of values. Programmers often refer to it as the "for loop" because of the way in which it repeatedly loops until a particular condition is satisfied. The general form of the for statement can be expressed as follows:
    
    
    for (initialization; termination;
         increment) {
        statement(s)
    }
    

    When using this version of the for statement, keep in mind that:
    • The initialization expression initializes the loop; it's executed once, as the loop begins.
    • When the termination expression evaluates to false, the loop terminates.
    • The increment expression is invoked after each iteration through the loop; it is perfectly acceptable for this expression to increment or decrement a value.
    The following program, ForDemo, uses the general form of the for statement to print the numbers 1 through 10 to standard output:
    
    
    class ForDemo {
        public static void main(String[] args){
             for(int i=1; i<11; i++){
                  System.out.println("Count is: "
                                     + i);
             }
        }
    }
    

    The output of this program is:
    Count is: 1
    Count is: 2
    Count is: 3
    Count is: 4
    Count is: 5
    Count is: 6
    Count is: 7
    Count is: 8
    Count is: 9
    Count is: 10
    

    Notice how the code declares a variable within the initialization expression. The scope of this variable extends from its declaration to the end of the block governed by the for statement, so it can be used in the termination and increment expressions as well. If the variable that controls a for statement is not needed outside of the loop, it's best to declare the variable in the initialization expression. The names ij, and k are often used to control for loops; declaring them within the initialization expression limits their life span and reduces errors.
    The three expressions of the for loop are optional; an infinite loop can be created as follows:
    
    
    // infinite loop
    for ( ; ; ) {
        
        // your code goes here
    }
    
    The for statement also has another form designed for iteration through Collections and arrays This form is sometimes referred to as the enhanced for statement, and can be used to make your loops more compact and easy to read. To demonstrate, consider the following array, which holds the numbers 1 through 10:
    
    
    int[] numbers = {1,2,3,4,5,6,7,8,9,10};
    

    The following program, EnhancedForDemo, uses the enhanced for to loop through the array:
    
    
    class EnhancedForDemo {
        public static void main(String[] args){
             int[] numbers = 
                 {1,2,3,4,5,6,7,8,9,10};
             for (int item : numbers) {
                 System.out.println("Count is: "
                                    + item);
             }
        }
    }
    

    In this example, the variable item holds the current value from the numbers array. The output from this program is the same as before:
    
    
    Count is: 1
    Count is: 2
    Count is: 3
    Count is: 4
    Count is: 5
    Count is: 6
    Count is: 7
    Count is: 8
    Count is: 9
    Count is: 10
    
    
    
    
    
    
    
    
    
    

    0 comments:

    Tuesday, 23 October 2012

    The For-Each Loop




    Iterating over a collection is uglier than it needs to be. Consider the following method, which takes a collection of timer tasks and cancels them:
    void cancelAll(Collection<TimerTask> c) {
        for (Iterator<TimerTask> i = c.iterator(); i.hasNext(); )
            i.next().cancel();
    }
    
    The iterator is just clutter. Furthermore, it is an opportunity for error. The iterator variable occurs three times in each loop: that is two chances to get it wrong. The for-each construct gets rid of the clutter and the opportunity for error. Here is how the example looks with the for-each construct:
    void cancelAll(Collection<TimerTask> c) {
        for (TimerTask t : c)
            t.cancel();
    }
    
    When you see the colon (:) read it as “in.” The loop above reads as “for each TimerTask t in c.” As you can see, the for-each construct combines beautifully with generics. It preserves all of the type safety, while removing the remaining clutter. Because you don't have to declare the iterator, you don't have to provide a generic declaration for it. (The compiler does this for you behind your back, but you need not concern yourself with it.)
    Here is a common mistake people make when they are trying to do nested iteration over two collections:
    List suits = ...;
    List ranks = ...;
    List sortedDeck = new ArrayList();
    
    // BROKEN - throws NoSuchElementException!
    for (Iterator i = suits.iterator(); i.hasNext(); )
        for (Iterator j = ranks.iterator(); j.hasNext(); )
            sortedDeck.add(new Card(i.next(), j.next()));
    

    Can you spot the bug? Don't feel bad if you can't. Many expert programmers have made this mistake at one time or another. The problem is that the next method is being called too many times on the “outer” collection (suits). It is being called in the inner loop for both the outer and inner collections, which is wrong. In order to fix it, you have to add a variable in the scope of the outer loop to hold the suit:
    // Fixed, though a bit ugly
    for (Iterator i = suits.iterator(); i.hasNext(); ) {
        Suit suit = (Suit) i.next();
        for (Iterator j = ranks.iterator(); j.hasNext(); )
            sortedDeck.add(new Card(suit, j.next()));
    }
    
    So what does all this have to do with the for-each construct? It is tailor-made for nested iteration! Feast your eyes:
    for (Suit suit : suits)
        for (Rank rank : ranks)
            sortedDeck.add(new Card(suit, rank));
    
    The for-each construct is also applicable to arrays, where it hides the index variable rather than the iterator. The following method returns the sum of the values in an int array:
    // Returns the sum of the elements of a
    int sum(int[] a) {
        int result = 0;
        for (int i : a)
            result += i;
        return result;
    }
    
    So when should you use the for-each loop? Any time you can. It really beautifies your code. Unfortunately, you cannot use it everywhere. Consider, for example, the expurgate method. The program needs access to the iterator in order to remove the current element. The for-each loop hides the iterator, so you cannot call remove. Therefore, the for-each loop is not usable for filtering. Similarly it is not usable for loops where you need to replace elements in a list or array as you traverse it. Finally, it is not usable for loops that must iterate over multiple collections in parallel. These shortcomings were known by the designers, who made a conscious decision to go with a clean, simple construct that would cover the great majority of cases.

    0 comments: