Sunday, January 4, 2009

Happy New Year Everyone

To all my students and readers

Happy New Year to all of you and may this year will fill your lives with challenges that will bring you to success....

Tuesday, December 16, 2008

C#.Net vs. JAVA

hello readers!

as a computer instructor, i'm always asked by my students this question -"why C#.Net and not Java?" of course i do have answers but sometimes i become so subjective-meaning it depends on the application that one will be developing...

This article compares the same program written in the C# and Java languages and then compares the dissembled code of both languages.

Java Hello Program:


class Hello
{

public static void main(String args[])
{
System.out.println("Hello");
}

}


Disassembled Java Hello Program:


class Hello
{

Hello()
{
// 0 0:aload_0
// 1 1:invokespecial #1
// 2 4:return
}

public static void main(String args[])
{
System.out.println("Hello");
// 0 0:getstatic #2
// 1 3:ldc1 #3
// 2 5:invokevirtual #4
// 3 8:return
}

}


Explanation of Java program:

To understand this you must have some knowledge of computer internals concepts for eg. Opcodes, instruction templates etc. I assume that you already know them.

As usual this code will also start with main method. The first line tells that print ?Hello? it is a normal print statement. A specific instruction, with type information, is built by replacing the ?T? in the instruction template in the opcode column by the letter in the type column. In this case it is a load instruction for type reference.

Invokespecial instruction must name an instance initialization method, a method in the current class, or a method in a superclass of the current class. Class and interface initialization methods are invoked implicitly by the Java virtual machine; they are never invoked directly from any Java virtual machine instruction, but are invoked only indirectly as part of the class initialization process.

invokevirtual or invokespecial is used to access a protected method of a superclass, then the type of the class instance being accessed must be the same as or a subclass of the current class.

The Java virtual machine uses local variables to pass parameters on method invocation. On class method invocation any parameters are passed in consecutive local variables starting from local variable 0.

C# Hello Program:


using System;

class Hello
{

public static void Main(string[] args)
{
Console.WriteLine("Hello");
}

}


Disassembled C# Hello Program :


.method private hidebysig static void Main() cil managed
{
.entrypoint
// Code size 11 (0xb)
.maxstack 8
IL_0000: ldstr "Hello"
IL_0005: call void [mscorlib]System.Console::WriteLine(class System.String)
IL_000a: ret
} // end of method Hello::Main


Explanation of C# .Net Program:

The first line defines the Main method by using the .method MSIL keyword. Note that the method is defined as being public and static, which are the default modifiers for the Main method. Also note that this method is defined as managed.

The next line of code uses the MSIL .entrypoint keyword to designate this particular method as the entry point to the application. When the .NET runtime executes this application, this is where control will be passed to the program.

Next, look at the MSIL opcodes on lines IL_0000 and IL_0005. The first uses the the ldstr (Load String) opcode to load a hard-coded literal ("Hello") onto the stack.

The next line of code calls the System.Console.WriteLine method. Notice that the MSIL prefixes the method name with the name of the assembly that defines the method. This line also tells us the number of arguments (and their types) that are expected by the method. Here, the method will expect a System.String object to be on the stack when it's called. Finally, line IL_000a is a simple ret MSIL opcode to return from the method.


Monday, December 15, 2008

Number Quiz


hi there....

if you're fond of numbers i have here a quiz that will somehow make you think....it's all about numbers that i got from www.funtrivia.com

kindly check this out...


1. There are only two numbers that are twice the sum of their individual digits. One of them 0 and I am the other one. I am a multiple of 9. Which number am I?

  1. You may have heard of the famous Fibonacci numbers. (1, 1, 2, 3, 5... where the next number is the sum of its previous two numbers) Well, I am one of them. What makes me more special is that I am the largest cube in this sequence. Which number am I?

  1. No other numbers are more special than me. I am the only number that is located between a square and a cube. More hints for you; I am smaller than 50 and I am an even number. Which number am I?

  1. I know number 10 is special, because we use it in our number system. However, I am not talking about number 10. I am the smallest number with 10 divisors. More clues to help you; I am smaller than 50 and a multiple of 12. Which number am I?

  1. A bit of thinking here for this question. I am the largest four-digit number with different digits, each greater than the one before it. Which number am I?

  1. . I am the age of the oldest person in the Bible - Methuselah when he died. I am a three-digit odd palindromic number that ends in 9. The sum of my individual digits is 24. Which number am I?

  1. . If you have a calculator with you now, you can find me. I am the answer for 1^7 + 2^6 + 3^5 + 4^4 + 5^3 + 6^2 + 7^1. But if you do not have one, never mind. I end in 2 and the sum of my individual digits is 12. I am greater than 700 but smaller than 800. Which number am I?

  1. Another palindromic number for you. I am the smallest five-digit palindromic number that ends in 9. Which number am I?

  1. If you are obsessed with Fibonacci numbers (1, 1, 2, 3, 5...) then this is the question for you. I am the largest square in the Fibonacci sequence. I am smaller than 150 and a multiple of 12. Which number am I?

. You may think I am a joke, but I am the smallest number with three different digits. Which number am I

Thursday, December 11, 2008

Design Pattern Classification

To my students:

There are the different design patterns in their respective classes. Please take note...

Behavioral design patterns

Chain of Responsibility Define a method of passing a request among a chain of objects.
Command Encapsulate a command request in an object.
Interpreter Allow inclusion of language elements in an application.
Iterator Enable sequential access to collection elements.
Mediator Define simplified communication between classes.
Memento Save and restore the internal state of an object.
Observer Define a scheme for notifying objects of changes to another object.
State Alter the behavior of an object when its state changes.
Strategy Encapsulate an algorithm inside a class.
Template Method Allow subclasses to redefine the steps of an algorithm.
Visitor Define a new operation on a class without changint it.

Creational design patterns

Abstract Factory Encapsulate a set of analogous factories that produce families of objects.
Builder Encapsulate the construction of complex objects from their representation; so, the same building process can create various representations by specifying only type and content.
Factory Method Allow subclasses to "decide" which class to instantiate.
Prototype Create an initialized instance for cloning or copying.
Singleton Ensure that only a single instance of a class exists and provide a single method for gaining access to it.


Structural design patterns

Adapter Adapt an interface to an expected interface.
Bridge Decouple an interface from its implementation.
Composite Create a tree structure for part-whole hierarchies.
Decorator Extend functionality dynamically.
Façade (Facade) Simplify usage by defining a high-level interface.
Flyweight Support fine-grained objects efficiently by sharing.
Proxy Represent an object with another object for access control.

Wednesday, December 10, 2008

some programming issues

Design patterns have picked up a lot of importance off late and rightfully so. To define design patterns in simple words they are "popular solutions for common design problems".
As an individual involved in the academe, I firmly believe that students must have background in this aspect to make them ready when they face the actual IT world...

I include in this article a very simple creational design pattern - the Factory Design pattern. I hope this can give some knowledge to you guys out there....

The factory method pattern is an object-oriented design pattern. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. The factory method design pattern handles this problem by defining a separate method for creating the objects, whose subclasses can then override to specify the derived type of product that will be created. More generally, the term factory method is often used to refer to any method whose main purpose is creation of objects.

Check this very simple example...

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class MainFactory
{
public BaseFactory GetObject(int x)
{
BaseFactory b = null;
switch (x)
{
case 1: b = new FirstObject();
break;
case 2: b = new SecondObject();
break;
default: Console.WriteLine("invalid");
break;
}
return b;
}
}

interface BaseFactory
{
void CreateObject();
}
public class FirstObject: BaseFactory
{
public void CreateObject()
{
Console.WriteLine("First object created...");
}
}
public class SecondObject : BaseFactory
{
public void CreateObject()
{
Console.WriteLine("Second object created...");
}
}
class Program
{
static void Main(string[] args)
{

MainFactory f = new MainFactory();
BaseFactory bf = f.GetObject(1);
bf.CreateObject();
Console.ReadKey();
}
}
}

Note: Depending on the integer value of the GetObject argument, the MainFactory class will create the object...

How to know the number of ticks from 1970 to 2007?

There are situations wherein you need to know the time interval in a split second. The DateTime.Now.Ticks is very useful in this said situation.

Here's a sample code to display how many ticks there are from 1970 to 2007.

using System;


namespace Ticks
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(DateTime.Now.Ticks );
Console.ReadKey();
}
}
}


Output: 633645793615625000