Sunday, June 28, 2015

Fundamentals in Distributed Components Based Software Development.

Hi Guys,
This post regarding the lab session we have done in the DCBSD lab. Most of the student missed the steps, because of this reason lot of students faced troubles. Honestly, this lab session is not a big deal until you catch the each step clearly. Following steps described each important area we have done in DCBSD lab. I think this would be helpful to get some sense about DCBSD fundamentals.

1. Hello world ! – JAVA vs C# 
I think I don’t need to explain much detailed information about this step, but following screen shots will be getting some sense regarding basic configuration, what should be done first.

1.1.1 JAVA – Configurations in cmd
Set environment variable.
Just drag and drop folder inside to the command prompt
Change directory 
Just drag and drop folder inside to the command prompt

1.1.2 JAVA – ‘Hello World!’ application
In this level you don’t need to mention class access flag specially. Leave it as it is.

class helloWorld
{
    public static void main(String args[])
    {
        System.out.println("Hello World!");
    }
}
1.1.3 Compile
C:\Users\Denuwan Hettiarachch\Desktop\Lab 01>javac "Hello World !.java"

1.1.4 Run
C:\Users\Denuwan Hettiarachch\Desktop\Lab 01>java helloWorld

1.1.5 Disassemble Java class file 
When you have .class file of a project, it’s not readable. But we can read .class file thought the JAVA disassemble command in cmd.
C:\Users\Denuwan Hettiarachch\Desktop\Lab 01>javap -c helloWorld.class

1.2.1 C# - Configurations in cmd
You can use ‘Developer Command Prompt for VS2012’ , specially configured for C# as a developer tool. You don’t need to worry about set environment variable or anything about configuration. Just search it, open it & use it. It’s cool !
Search cmd and open Developer Command Prompt for VS2012

1.1.2 C# – Hello World ! application
In this level you don’t need to mention class access flag specially. Leave it as it is same as the JAVA.
using System;

class helloWorld
{
    public static void Main(String []args)
    {
        Console.WriteLine("Hello World !");
    }
}


1.1.3 Compile
C:\Program Files (x86)\Microsoft Visual Studio 11.0>cd "C:\Users\Denuwan Hettiarachch\Desktop\Lab 01" C:\Users\Denuwan Hettiarachch\Desktop\Lab 01>csc "Hello World !.cs"


1.1.4 Run
C:\Users\Denuwan Hettiarachch\Desktop\Lab 01>"Hello World !"

1.1.5 Disassemble C# class file
When you have .exe file of a project, it’s not readable. But we can read .exe file thought the C# disassemble command in cmd.

C:\Users\Denuwan Hettiarachch\Desktop\Lab 01>ILDASM "Hello World !.exe 
Disassembled code - C# GUI

2. Create a library & reusing JAVA
Here’s the deal, we create the class file without main method and compile it, you know if we doesn’t have main method we can’t run this class. It just a library file. We can reuse this library file in another application with the main method. But you should follow specified file structure you put all the libraries in the specified folder and import when you want to reuse it again.

2.1.1 Here’s the my file structure

2.1.2 Create a library file inside a ‘com’ folder
If you remember the first step which we created ‘Hello world!’ app, I mention you don’t need to worry about access flag, and we leave as it is by default. But when we create a library we can’t do it in that way. Because we decide to reuse it again, so we need to change the access flag as a ‘public’ because of the access flag change we should follow additional step, that is we must save the project file using class name. Otherwise we can’t reuse it again. 

package Code.com;

public class Item
{
    private int itemId;
    private String itemName;
    private double itemPrice;
  
    public Item(int itemId,String itemName,double itemPrice)
    {
        this.itemId = itemId;
        this.itemName = itemName;
        this.itemPrice = itemPrice;
    }

    public void printItemDetails()
    {
        System.out.println("Item ID : "+itemId);
        System.out.println("Item Name : "+itemName);
        System.out.println("Item Price : "+itemPrice);
    }
}
  
When I compile, I'm still in the root folder. no need to change directory. 
C:\Users\Denuwan Hettiarachch\Desktop\Lab 01>javac Code\com\*.java 


2.1.3 Create a project in root folder and reuse the library
We need to import library package top of the class.

import Code.com.*; //Import all libraries included in com folder

class itemApp
{
    public static void main(String args[])
    {
        Item firstItem = new Item(1,"Milk Powder",250.50);
        firstItem.printItemDetails();
    }

C:\Users\Denuwan Hettiarachch\Desktop\Lab 01>javac ItemApp.java

C:\Users\Denuwan Hettiarachch\Desktop\Lab 01>java itemApp

2.1.4 Create a library archive file and reuse it again.
We can also put all the libraries in the archive (.jar) file and reuse it when we want, Remember when we execute the command we must in the root folder in the cmd, and provide correct folder which libraries are included inside.  

2.1.4.1 Create an archive file
C:\Users\Denuwan Hettiarachch\Desktop\Lab 01>jar -cvf library.jar Code

2.1.4.2 Compile project using archive file 
C:\Users\Denuwan Hettiarachch\Desktop\Lab 01>javac -cp library.jar ItemApp.java

2.1.4.3 Execute the application which is using the archive file.
C:\Users\Denuwan Hettiarachch\Desktop\Lab 01>java -cp library.jar;. itemApp

3. Create a .dll library and reuse it in C# project
When we comparing the JAVA vs C# up to this level coding part is same except few things. But reusing libraries concept little bit change C# comparing with Java. C# doesn’t use folder structure we can simply name the namespace and recall it inside the runnable application without worrying the file structure.

using System;

namespace studentPackage
{
    public class student
    {
        private int studentId;
        private String studentName;
      
        public student(int Id, String Name)
        {
            studentId = Id;
            studentName = Name;
        }
       
        public void printStudentDetails()
        {
            Console.WriteLine("Id : "+studentId);
            Console.WriteLine("Name : "+studentName);
        }
    }
}

3.1 Create a .dll file
C:\Users\Denuwan Hettiarachch\Desktop\Lab 01>csc /target:library Student.cs

3.2 Compile project file, which is use .dll file


using System;
using studentPackage;

class StudentApp
{
    public static void Main(String []args)
    {
        student firstStudent = new student(1,"Kamal");
        firstStudent.printStudentDetails();
    }
}


C:\Users\Denuwan Hettiarachch\Desktop\Lab 01>csc /reference:Student.dll StudentApp.cs

3.3 Execute the runnable program which is use .dll file
C:\Users\Denuwan Hettiarachch\Desktop\Lab 01>StudentApp

[Download Lab Work]

Happy Coding.....

Regards,
Denuwan Himanga. 

Tuesday, June 2, 2015

Execute command prompt command through simple application


Hi all,

I think most of developers are familiar with more than one programing languages, But within these different choices you might choose one programming language as the best choice. Today, I’ll introduce my best choice. It’s C# in the other term C sharp. Actually, one of my favorite book series of Head First, clearly mention learning C# is not a learning Microsoft Visual Studio IDE. A lot of students think learning the Visual Studio IDE is more enough to deal with C#. But it is completely wrong. Because Visual Studio IDE provide lots of features in a very user-friendly manner, but the thing is you should know how to use these features in an optimal way. Otherwise, it might be a headache while implementing your program in practical world.

Within this post, I’ll explain step by steps about how to develop an application using Visual Studio, Actually I’ll develop a small application for executing command prompt command, but I’ll decide explain each step because of the beginners.

In C# we have different choices for building an application such as Windows Form, Windows Presentation Foundation (WPF), and Mobile Application Development likewise. For this demonstration I choose Windows Presentation Foundation (WPF). WPF has a lot of customizability because of the XAML interface formatting future, and also WPF support many of new features introduce by Microsoft with every new Visual Studio version release. Because of these kind of advantages my choice is always WPF.

Ok, let’s go through the demonstration. For this demo I used Visual Studio 2012 IDE. But it’s not a latest version. If you are a university student, you can easily get the Visual Studio latest version with lots of new festers through Dream Spark account. If you are using, these kind of genuine copy, you can build your projects with an aid of Microsoft Team Foundation Service, Team foundation is a Microsoft own version control system. Lots of companies in the modern world use this feature, it's also compatible with an Agile development methodology. Using Team Foundation, developers can deal with project sponsors in a very easy manner. 

Before we start this demo, I think most of you know hoe to shout down the PC to the given time, using command prompt. Because of the beginners I’ll explain how to shutdown PC through command prompt. User can set time period as the second for executing the shutdown command, I set 3600s (=1hr). That means my PC will shut down after 1hr.



Ok, I think now you have some idea about my application, I’ll develop an application for executing above command line through WPF application.  

         1. Start new project

          2. Select Project type as WPF/Set Project name as “ShutdownTimerApp”

           3. Here’s the main canvas, top of the canvas you can drag and drop Text Boxes, Labels, etc. from Toolbox.

         4. Design Graphical User Interface
a.      Drag and drop three Textboxes and one Button
       

b.      Click on the top of element and do the changes in properties window or in XAML design.
                                                              i.      Changing the name of the element
                                          


                                                            ii.      Changing the appearance of the element
                                          



                                                          iii.      Changing the contain and text format of the element
                                         

  
          5.Double click on the top of the Button, the IDE will automatically generate the code segment for you. Within the generated method you can define on click method.




using System.Diagnostics; //Add C# directive (C# Reference)

private void Button_Click_1(object sender, RoutedEventArgs e)
{

int hours = 3600*int.Parse(hoursTextBox.Text);
int minutes = 60*int.Parse(minutesTextBox.Text);
int seconds = int.Parse(secondsTextBox.Text);

int time = hours + minutes + seconds;

Process.Start("shutdown", "/s /t " + time);

}



Here’s my simple & smart app developed by using WPF, see how easy and its customizability. Software developing world becomes a fabulous place because of the combination of C# and this amazing Visual Studio IDE.

Happy Coding….

Best Regards, 
Denuwan Himanga.