Monday, January 25, 2010

Android Graphics

I was curious about the graphical programming in Android... Fortunately i have found the library aChartEngine... As i was trying to play around with it, i came up with few trigonometric graphs... Hope this would give some pointers to the new comers about graphical programming in Android...

The application looks like the following...



And the graphs are like the following:

Sin Curve



Cosine Curve



Tangent Curve



Sinc Curve



Damped Sin Curve

Tuesday, January 19, 2010

Android HomeScreen Management

As i was trying to play around with Android HomeScreen, i came up with this application to add any application to the HomeScreen. I think it can work as a good project for the beginner of Android programming.

The home screen looks like the following after i added two of the applications to the HomeScreen -one is my own KeyPadDialer and the other is the AlarmClock.



And the HomeScreen Management Application looks like the following in the beginning:



And after clicking the Spinner it looks this:



The complete source code for my application can be found here.

The manifest file for this application can be found here.

And the layout of the application is given here.

Hope this idea helps others...

Saturday, January 2, 2010

My first experience with Java

As most of my experience lies in C++, when i started gaining interest in Java/J2SE i started looking at it from a C++ programmers point of view. Hence the first thing i googled was how to create a canonical class in Java. And i was astonished to find out that the way we do Assignment operator in C++ is not the same in Java. After some more googling i came to know about the clonable interface in Java. And it gave me the right direction...

Then i googled about the String class in Java and found out that a String object in Java is immutable... i became curious and opened the String class of Java... And voila... no wonder... the character array that holds the contents of the String object is final... hence it is immutable... So i asked myself what happens when we do String newString = oldString in Java. And i got the answer... As the String class is not implementing the Clonable interface, when we do String newString = oldString, the oldString's contents get a new reference in newString... We loose any reference to oldString.

But then i wondered how String S1 = oldString + "abc" would work. Because as i was googling i found out that Java does not support operator overloading... so how come the + operator is working fine for the String class? my first stumbling block... fortunately i found out the document at http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.18.1 and my doubts were gone...

So i wondered if Strings are immutable in Java, are not there any way to change the contents of a String object without creating a new object? i googled a little more and came to know about StringBuffer and StringBuilder classes and their mutable char array which will store that data... i delved into the classes a little more and found out about their append function.

So far my investigation into the Java source code is fruitful...

Hopefully i will be able to delve more into it and find out the nitty-gritties about the Java language...

Tuesday, November 17, 2009

Experimentation with Design Pattern

I am yet to study the book Pattern Hatching. However, i tried to accumulate all my knowledge on Design Pattern and solved a problem of Accountancy. i would like to share it with you. Let me, first of all, state the problem.

The Problem :

Basic sales tax is applicable at a rate of 10% on all goods, except books, food, and medical products that are exempt. Import duty is an additional sales tax applicable on all imported goods at a rate of 5%, with no exemptions. Also the Sales Tax should be rounded off to the nearest 0.05.

This being the problem, we need to come out with a solution for this problem.

Now let me tell you how my thought process went into it to think it at an abstract level.

First of all, i thought to make two top level classes. One that will define the Items having different benchmarks for Sales Tax and Import Duty; i.e.

1. Food, Medical and Book items which are not imported and hence exempted from both sales tax and import duty
2. Non Food Book and Medical items which are not imported and hence will have just the Sales Tax
3. Food Book and Medical items which are imported and hence will have only import duty
4. Non food book and medical items which are imported and hence will have both Sales Tax and Import Duty

And the other for the Tax Calculation algorithm for different items.

Then i thought this is a perfect match for Strategy Pattern that i had studied in the GoF book. I thought about that pattern keeping in mind for the future expansion of the Tax Calculation Algorithm. What i mean is that for the present problem, the calculation is simple. And hence it does not need any other Strategy. However, for the future purpose if the Tax Calculation Algorithm is changed to some complicated one, then we can just Subclass the Tax Calculation class and attach that strategy to the Item Class.

Next i thought, won't it be nice to get a Factory Class through which the client can create different Items on the fly. Hence i have decided to create an ItemCreator class which is nothing but a parameterized factory class for creating different Items on the fly.

And i came out with the following solution.

The Item class Hierarchy:


File: Item.h

#ifndef ITEM_H
#define ITEM_H

class SalesTax;

//This represents the Items which don't have an Import duty or any sales tax
class Item
{
public:

//Constructors
Item();
Item (SalesTax* aSalesTax);

//Interface Functions for Item

//To calculate the price after tax and import duty
virtual void CalculateTotalPrice();

//To calculate the total tax and import duty
virtual void CalculateTotalTax();

//To set the price of the Items
void SetPrice(double aPrice);

//To get the price of the Items before tax
double getPrice();

//To get the price of the items after tax
double getTotalPrice();

//To get the total tax and import duty of the items
double getTax();

//Data
protected:
//Works as the Strategy of the Sales Tax problem.
//If in future the tax calculation becomes more complicated for different Items
//we will just have to change this Strategy. We can also subclass this Strategy class
//for future expansion of the tax calculation strategy
SalesTax* iSalesTax;
//Data
protected:

//These are the basic properties of any Item.
//Hence these are made protected members so that the subclasses of Item can inherit
//these properties
double iPrice;
double iTotalPrice;
double iTotalTax;
};

//This class represents the Items which have only Import Duty
class ImportedItem : virtual public Item
{
public:
//Constructors
ImportedItem();

//This constructor helps to create Items having only Import duty
ImportedItem(SalesTax* aSalesTax, double aImportDuty);

//Override
virtual void CalculateTotalTax();

protected:
double iImportDuty;
};

//This class represents the Items which have only Sales Tax but no Import Duty

class NonFoodBookMedicalItem : virtual public Item
{
public:
//Constructors
NonFoodBookMedicalItem();

//This constructor helps to create Items having only Sales tax
NonFoodBookMedicalItem(SalesTax* aSalesTax, double aRate);
//Override
virtual void CalculateTotalTax();

protected:
double iRate;
};



//This class represents the Items which have got both Import Duty as well as sales Tax
class NormalItem: public ImportedItem, public NonFoodBookMedicalItem
{
public:
NormalItem();
//This constructor helps to create Items having both Sales tax and Import duty
NormalItem(SalesTax* aSalesTax, double aRate, double aImportDuty);

//Override
virtual void CalculateTotalTax();
};

#endif

As you can see the four classes solve the hierarchy for different items having different benchmark for Sales Tax and import Duty.

The Item.cpp file looks like the following:

File: Item.cpp
#include "SalesTax.h"
#include "Item.h"

Item::Item(){}

Item::Item(SalesTax* aSalesTax):iSalesTax(aSalesTax),iPrice(0),iTotalPrice(0),iTotalTax(0)
{
}

void Item::CalculateTotalPrice()
{
iTotalPrice = iPrice + iTotalTax;
}

double Item::getTotalPrice()
{
return iTotalPrice;
}


void Item::CalculateTotalTax()
{
iTotalTax = iSalesTax->ComputeSalesTax(iPrice, 0, 0);
}

void Item::SetPrice(double aPrice)
{
iPrice = aPrice;
}

double Item::getPrice()
{
return iPrice;
}

double Item::getTax()
{
return iTotalTax;
}
ImportedItem::ImportedItem(){}

ImportedItem::ImportedItem(SalesTax* aSalesTax, double aImportDuty):Item(aSalesTax)
{
iImportDuty = aImportDuty;
}
void ImportedItem::CalculateTotalTax()
{
iTotalTax = iSalesTax->ComputeSalesTax(iPrice, 0, iImportDuty);

}
NonFoodBookMedicalItem::NonFoodBookMedicalItem(){}

NonFoodBookMedicalItem::NonFoodBookMedicalItem(SalesTax* aSalesTax, double aRate):Item(aSalesTax)
{
iRate = aRate;
}

void NonFoodBookMedicalItem::CalculateTotalTax()
{
iTotalTax = iSalesTax->ComputeSalesTax(iPrice, iRate, 0);

}
NormalItem::NormalItem() {}

NormalItem::NormalItem(SalesTax* aSalesTax, double aRate, double aImportDuty):Item(aSalesTax)
{
iRate = aRate;
iImportDuty = aImportDuty;
}
void NormalItem::CalculateTotalTax()
{
iTotalTax = iSalesTax->ComputeSalesTax(iPrice, iRate, iImportDuty);
}

Now let us concentrate on the Sales Tax class

file: SalesTax.h

//This class works as the Strategy of the Sales tax problem
class SalesTax
{
public:

//Default constructor
SalesTax();

//This function helps to compute the Sales Tax
virtual double ComputeSalesTax(double aPrice, double aRate, double aImportduty);

private:
//This is an helper function which will round off the sales tax
double RoundOff(double aTax);
};


And the implementation of the Sales tax is as follow:

file: SalesTax.cpp

#include "SalesTax.h"

SalesTax::SalesTax(){}

double SalesTax::ComputeSalesTax(double aPrice, double aRate, double aImportduty)
{
double tx = (aPrice*aRate/(double(100))) + (aPrice*aImportduty/(double(100)));
return RoundOff(tx);
}
//private:
double SalesTax::RoundOff(double aTax)
{
int taxTemp = (int)aTax;

double decimaltaxTemp = (double)(aTax - (int)taxTemp);

int tempy = (int)(1000*decimaltaxTemp)/100;

int tempz = (int)(1000*decimaltaxTemp - tempy*100);

int temp = (int)(tempz/10);

int t = tempz%10;

if (t >= 5)
temp+=1;

return (double)(taxTemp + tempy*(0.1) + temp*(0.01));
}

As you can see the the data for calculation are being passed from the Item class.

Hence we can say this is in abstract form an implementation of the Strategy Pattern where the Item class is working as the Context and the Sales Tax class is acting as the sole Strategy interface.

Now lets concentrate on the creation of the Items.

As i have already mentioned, this is done through a Parameterized factory class called ItemCreator.

This class looks like the following:

File: ItemCreator.h

#include "Item.h"

const int ITEM_WITH_NOSALESTAX_AND_IMPORTDUTY = 1;
const int ITEM_WITH_NOSALESTAX_ONLY_IMPORTDUTY = 2;
const int ITEM_WITH_ONLY_SALESTAX_AND_NOIMPORTDUTY = 3;
const int ITEM_WITH_BOTH_SALESTAX_AND_IMPORTDUTY = 4;

const double SALES_TAX_RATE = 10;
const double IMPORT_DUTY_RATE = 5;

class Not_A_Standard_Item_Type_Exception
{
public:
void printerrormsg();
};
class ItemCreator
{
public:
virtual Item* Create(int aItemId);
};

And the implementation of this ItemCreator is as follow:

file: ItemCreator.cpp


#include "ItemCreator.h"
#include "Item.h"
#include "SalesTax.h"

using namespace std;

void Not_A_Standard_Item_Type_Exception::printerrormsg()
{
cout <<"Not the right Item Type..." <<endl;
}

Item* ItemCreator::Create(int aItemId)
{
SalesTax* st = new SalesTax();

switch(aItemId)
{
case ITEM_WITH_NOSALESTAX_AND_IMPORTDUTY:
return new Item(st);
break;

case ITEM_WITH_NOSALESTAX_ONLY_IMPORTDUTY:
return new ImportedItem(st,IMPORT_DUTY_RATE);
break;

case ITEM_WITH_ONLY_SALESTAX_AND_NOIMPORTDUTY:
return new NonFoodBookMedicalItem(st,SALES_TAX_RATE);
break;

case ITEM_WITH_BOTH_SALESTAX_AND_IMPORTDUTY:
return new NormalItem(st,SALES_TAX_RATE,IMPORT_DUTY_RATE);
break;

default:
throw Not_A_Standard_Item_Type_Exception();
}
}

And the client program will look like the following:



#include "SalesTax.h"
#include "Item.h"
#include "ItemCreator.h"

#include <vector>

using namespace std;

void main()
{
typedef vector&lt;Item*&gt;  listOfItem; 
listOfItem::iterator theIterator;

listOfItem Basket;
char answer = 'n';

double totalprice = 0;
double totaltax = 0;

do
{
int type_of_item;

cout <<"Enter the type of Item...1,2,3,4" <<endl;

cout <<"1 for ITEM_WITH_NOSALESTAX_AND_NOIMPORTDUTY"  <<endl;

cout <<"2 for ITEM_WITH_NOSALESTAX_ONLY_IMPORTDUTY"<<endl;

cout <<"3 for ITEM_WITH_ONLY_SALESTAX_AND_NOIMPORTDUTY"<<endl;

cout<<"4 for ITEM_WITH_BOTH_SALESTAX_AND_IMPORTDUTY" <<endl;

cin>>type_of_item;

ItemCreator* itemCreator = new ItemCreator();

try 
{
Item* item = itemCreator->;Create(type_of_item);

cout <<"Enter the price of the Item" <<endl;

double price;

cin >>price;

item->SetPrice(price);

Basket.push_back(item);
}

catch(Not_A_Standard_Item_Type_Exception&amp; e)
{
e.printerrormsg();
}

cout<<"Do you want to continue... Y/N" <<endl;
cin>>answer;
}
while (answer =='y');

theIterator = Basket.begin();

int pos = 0;
while (theIterator != Basket.end())
{
Basket.at(pos)->CalculateTotalTax();
totaltax+=Basket.at(pos)->getTax();

Basket.at(pos)->CalculateTotalPrice();

double price = Basket.at(pos)->getPrice();

double price_after_tax = Basket.at(pos)->getTotalPrice();

totalprice+=price_after_tax;
cout<<"Item"  <<pos+1 <<" price " <<price  <<endl;
theIterator++;
pos++; 
}
cout<<"------------" <<endl;
cout<<"Toal tax " <<totaltax <<endl;
cout<<"Total price "<<totalprice<<endl;
}



 Fig : The Class Diagram
Thus the problem is solved using two common design pattern concepts - Strategy Pattern and Parameterized Factory Pattern.

This is the way i am trying to move from the problem domain to the solution domain using design pattern concepts.

Hope this helps others who are studying Design Pattern.

Monday, November 9, 2009

My first experience with Ubuntu Karmic kola



I felt very excited after I read the review comments about Ubuntu 9.1, the Karmic Kola distribution of Ubuntu... However, with over-enthusiasm, I could not wait for the official release... I downloaded the release candidate a few days before the official release of Ubuntu 9.1...

As I have used only Windows so far, I was a bit worried about my transition to Ubuntu... However, so far it was smooth...

After downloading the release candidate of the Ubuntu 9.1, i had to burn the CD... I used ImgBurn to do that... What one needs is a blank 700 MB writable CD... The burning process did not take much time...

Then with caution, I inserted the CD for the installation... The installation got started... And it hardly took 20 minutes to completely install it...

I restarted my computer... There were two options to boot up - one for my old Windows XP and the other was Ubuntu... I chose the second one... The login screen came in no time... I entered my password... And voila... The system came up within a flash... I thought I needed to do a lot of configuration for my internet connection... To test it I just clicked the Firefox menu on the top of the desktop... And no wonder, the Ubuntu Google came up in a flash...

I was delighted...

With a little help through Googling, I found that for a developer, the build-essential package is a must... After that, I did some research and found the Synaptic Package Manager Under System... I opened it... Searched for build-essential... Marked it... And installed it...

Then I installed Git... And Eclipse... And yesterday I installed QTCreator IDE... And the development environment is ready...

So far my experience with Ubuntu is very good... its update manager is working for me...The synaptic package manager is fantastic...

I can say that for me it's been a smooth transition to Ubuntu...And the best part is that neither I had to install SpyBot... nor do I have to scan with SpyWare Doctor every time I start my PC... of course, I don't have to care about Malwarebytes to fight against Virtumunde...

In 2022, in Bharat, the share of Linux desktop was 8.05% and in 2024 it is 15%.
See below...
Linux desktop share in Bharat in 2022



Linux desktop share in Bharat in 2024



Once the Government of Bharat adopts it everywhere, the ecosystem will be built around Linux.

I am happy to see that Bharat has indigenously developed #maya OS, a Linux-based OS  (DRDO project - 2021) and the Government of Bharat is eagerly supporting the deployment of this OS across defense and government offices. Once the schools and colleges adopt #Linux and the government offers a green signal for public use of Linux, there won't be any looking back. 

#JaiHind...

Enjoy...



Saturday, October 24, 2009

Observer Pattern in MFC

As I walk through the different design concepts of the Design Pattern book by GoF, I usually try to find from my past experience about any practical implementation of these patterns. This way I found out one practical implementation of the Observer Pattern in microsoft foundation class (MFC) library which I would like to share with you.

Before explaining the application of Observer Pattern in MFC, let me give you a brief introduction of this pattern. Observer pattern helps you in notifying and updating all the dependent objects, if one object changes state. This pattern has two main objects – Subject and Observer. Multiple number of observers can be attached to a particular Subject. All the observers are notified and in turn gotten updated if the Subject changes its state. This is also known as the publish-subscribe. The subject is the publisher of changes, and the Observers are the subscribers to those changes.

The class diagram will look like the following.



And the sequence diagram of this pattern is like this.



What these two diagrams essentially depict is that in the Observer Pattern, we have a Subject, which can attach one or more Observers through its Attach() function. Whenever it changes its state it Notifies all the attached Observers through its Notify function. The observers in turn synchronize their states with that of the Subject through the GetSubjectState function.

Now let me dissect the MFC's Document-View architecture and explain how the Observer Pattern has been implemented there. In MFC, the Document acts as the Subject and the views attached to it act as the observers. The Document (Subject) has to notify and update the views (observers) whenever it changes its state. This is done through the function UpdateAllViews () of the CDocument class.

Let me show you the actual code of the UpdateAllViews from doccore.cpp under the MFC folder.

void
CDocument :: UpdateAllViews(CView* pSender, LPARAM lHint, CObject* pHint)
{
ASSERT(pSender == NULL || !m_viewList.IsEmpty());

POSITION pos = GetFirstViewPosition();
while (pos != NULL)
{
CView* pView = GetNextView(pos);
ASSERT_VALID(pView);
if (pView != pSender)
pView->OnUpdate(pSender, lHint, pHint);
}
}


As you can see from the code that UpdateAllViews function actually traverses through all the views attached through it and call the view's OnUpdate function. Hence UpdateAllViews acts as the Notify function of the Observer pattern.

Let me show you the actual code of OnUpdate from viewcore.cpp. It goes like this:

void
CView :: OnUpdate(CView* pSender,LPARAM, CObject*)
{
ASSERT(pSender != this);
UNUSED(pSender);
Invalidate(TRUE);
}

As its is clear from the above code, that the OnUpdate function will actually invalidate the view area, which will force the views to redraw themselves synchronizing their states with the current state of the Document.

This is all about the notification from Document to View. But what about the Attach and Detach function of the Subject. We have similar functions in the Document class which are as follows:

void CDocument :: AddView(CView* pView)
{
ASSERT_VALID(pView);
ASSERT(pView->m_pDocument == NULL);
ASSERT(m_viewList.Find(pView, NULL) == NULL);
m_viewList.AddTail(pView);
ASSERT(pView->m_pDocument == NULL);
pView->m_pDocument = this;

OnChangedViewList();
}

void CDocument :: RemoveView(CView* pView)
{
ASSERT_VALID(pView);
ASSERT(pView->m_pDocument == this);

m_viewList.RemoveAt(m_viewList.Find(pView));
pView->m_pDocument = NULL;

OnChangedViewList();
}

As this is clear from the above listings that CDocument :: AddView is similar to the Attach function of the Subject, and RemoveView is like the Detach function as in the Observer Pattern.

Similar to the Observer Pattern, the Document has a list of its attached views through its m_viewList member and each view has a reference to its document through its m_pDocument member.

From the above discussion, it has become clear that the MFC's Document- View architecture is one of the applications of the Observer Pattern.

Saturday, October 10, 2009

WiFi Subsystem in Android

As i was trying to understand the WiFi subsystem of Android i came out with a diagram to depict the flow of events between different functional blocks of it. I would like to share it with you.

It goes like this:



Hope it will help the Android developers' community.