Saturday 31 August 2013

Top Most How to Convert Double to String to Double in Java Program with Example

Many scenarios come in day to day Java programming when we need to convert a Double value to String or vice versa. In my earlier article we have seen how to convert String to Integer and in this article we will first see how to convert double to String and later opposite of  that from String to double. One important thing to note is Autoboxing which automatically converts primitive type to Object type and only available from Java 5 onwards. These conversion example assumes code is running above Java 5 version and actually tested in JDK 1.6, which makes it enable to pass Double object when method is expecting double primitive value e.g. String.valueOf(double d) which expect a double value.
Read more »

Top Most Caching Framework - Sample Cache Implementation


I was asked in an interview to design a cache framework.
The problem statement was:
Develop a caching framework that provides the following:
1) Load data from file, database or any other source
2) Refreshes the data after a configurable interval if it is not a one time load
3) Invalidates the cached objects after a configurable interval.

Few questions that come to mind while designing is what should be cache.
And If I assume it is going to be Map, then what would be key and value.
Second critical thing is how to expose the Cache to clients.
What operations to support.

I decided for using ConcurrentHashMap as my cache.
There are several benefits of using this Map over others like:
Operations are thread safe supporting full concurrency and highly efficient.

Clients would access the Cache through a CacheProvider.
I created an interface named CacheProvider.
This is the starting point of my Cache Implementation.
Below is the code for CacheProvider:

public interface CacheProvider {
        Object getAndSet( String key);
Object setAndGet( String key );
void refresh();
void invalidateObj();
void cancel();
}


I had an initial requirement where I can invalidate objects in Cache after certain interval if there is no access.
I also decided that,
If a particular data is retrieved then its there should be fresh start of invalidation time, i.e. if I am going to configure the invalidation time as 5 min.
and If some object is not accessed for 5 mins, it would be removed from Cache.
And if some object is not accessed for 4 mins, and some client asks for this object, then there should be fresh start, means it would be invalidated after 5 mins from now, if not accessed in next 5 mins.
I would also be configuring an refresh policy, and refreshing all objects in the cache at particular interval.

Regarding data. I used Derby for testing.
I designed Cache to support data from Database, or from file, or to test simple data can be provided as command line arguments.
I know this is too early to talk about this.
But this is going to be initial requirement of where data would be stored and how MyCacheProvider would load data from data provider.

I created three classes for handling data.
CollectionManager - for managing data from command line
FileManager - for managing data from File
DBManager - for managing data from Database.

I am providing the code for all the data managers.
But if one is not interested, then one can simple ignore the below three classes.


CollectonManager:


FileManager:

DBManager:


DBManager is a class which manages data in database.  I have used Derby.  Using derby is very easy, and its useful in applications like this.  You don't need database, its just one jar. Evrything inside the jar, database, and drivers.


My DBManager class is below:


DBmanager has code for initialization, where it reads config values from a file and intializes a DB connection. 
Then, DBManager has code to create the table, populate the table with data. 
Remember, this data would be used to cache. 
Then code to retrieve all data from the database, this method would be called to refresh the whole cache with data from database. 
And method to retrieve a particular data from database. 




I started my design with a CacheProvider interface. 
Now I would extend that clas and create MyCacheProvider, also I would create a CustomKey to store in Cache.
MyCacheProvider would internally use a ConcurrentHashMap for storing data.


Below is the code of CustomCacheKey, instance of this class would act as key in the Cache, or ConcurrentHashMap inside the MyCacheProvider.

CustomCacheKey:


MyCacheProvider:
MyCacheProvider extends CacheProvider, implements getAndSet() method retrieves the value from Cache, put the same key again in the cahce with updated timestamp.
Whenever a key is retrieved from Cache, its timestamp should be updated, so we either update the timestamp of the key(CustomKey), or put a new key with same value and new timestamp in the Cache.
We have to maintain the timestamp, in order to invalidate inactive objects in Cache.

We also have methods to refresh the cache and append a user provided hashmap to cache.

MyCacheProvider should also contain methods to invalidate a particular object, and provide method to cancel or clear whole cache.


Now, I decided that clients should not directly use the MyCacheProvider, because in future I would be creating some more implementation of CacheProvider, and making it configurable.

I made a CacheHandler, and clients would use this handler, with limited operations exposed.

CacheHandler:


public class CacheHandler {
static MyCacheProvider myCacheProvider;

public static void start(){
myCacheProvider = MyCacheProvider.getCacheProvider();

}
public static Object get(String key){
Object myCachedObject = myCacheProvider.getAndSet(key);
if (myCachedObject == null) {
myCachedObject = myCacheProvider.setAndGet(key);
}
return myCachedObject;
}

public static void refreshCache(){
myCacheProvider.refresh();
}

public static void invalidateObj(){
myCacheProvider.invalidateObj();
}

public static void cancel(){
myCacheProvider.cancel();
myCacheProvider = null;
}

public static Map mapStatus(){
return myCacheProvider.mapStatus();
}
}

For simplicity I made above class a static class.

Now I am done with initial design of Cache with three classes.
MyCacheProvider, CustomCacheKey, and CacheHandler.
CacheProvider is a super interface.
And I created three classes for handling persistent data.


I have to run two tasks periodically:
Invalidate objects after specific interval
Refresh the whole cache.
I decided of one more task, which prints the Cache status.

SO, I am going to create three tasks:
CacheInvalidateObjTask - for invalidating the Cache objects.
CacheMapStatus - for printing the Cache status.
CacheRefreshTask - for refreshing the cache.

CacheInvalidateObjTask:

public class CacheInvalidateObjTask implements Runnable{

public void run(){
CacheHandler.invalidateObj();
}
}


CacheMapStatus:

public class CacheMapStatus implements Runnable{
Map map;

public void run(){
map = CacheHandler.mapStatus();
long time = System.currentTimeMillis();
System.out.println("Map Status.. at.." + time/1000 + " secs..");
System.out.println(map);
}

}


CacheRefreshTask:

public class CacheRefreshTask implements Runnable{

public void run(){
CacheHandler.refreshCache();
}

}


So I created three tasks.

I also created two classes, which can be skipped:

CacheConstants:

public interface CacheConstants {

// Resource provider Constants
String RESOURCE_PROVIDER_COLLECTION = "collection";
String RESOURCE_PROVIDER_DATABASE = "database";
String RESOURCE_PROVIDER_FILE = "file";
}


CacheProperties:

public class CacheProperties {

public static int CACHE_SIZE = 0;

public static long INVALIDATE_OBJ_TIMER_INTERVAL = 1000L;
public static long REFRESH_TIMER_INTERVAL =1000L;
public static long PRINT_MAP_TIMER= 1000L;

public static String RESOURCE_PROVIDER = "";

public static String RESOURCE_FILE_PATH= "";

public static String DATABASE_DRIVER = "";
public static String DATABASE_PROTOCOL = "";
public static String DATABASE_NAME = "";
public static String DATABASE_USERID = "";
public static String DATABASE_PWD = "";
public static String DATABASE_TABLE_NAME = "";
public static Boolean DATABASE_CREATE = true;
public static Boolean DATABASE_CREATE_TABLE = true;
}


To start the application, I have created a class CacheService.
CacheService uses CacheServiceHelper, and delegates the call to CacheServiceHelper.


CacheService:

CacheServiceHelper:


CacheServiceHelper contains methods to schedule the three tasks. And method to set the Env.
And a cleanup method to clear the cache:

void cleanUp(){
CacheHandler.cancel();
}



And our cache is ready.


Summary:

CacheService is the main class to start the program.
CahceServiceHelper is for handling all the startup tasks.
CacheService calls the CacheServiceHelper for initializing the environment, and starting the service.
In initialization, CacheServiceHelper sets the properties, and the resource provider.
Resource provider will provide data to store in the cache, it can be either database, or file, or command line arguments.
CacheServiceHelper, starts the service by creating three tasks and submitting them to a ScheduledthreadPool.
Tasks are:
1. Task to refresh the cache map, periodically, CacheRefreshTask
2. Task to invalidate objects inside cache map after specified time, CacheInvalidateObjTask
3. Task to print the map status to console, CacheMapStatus
Each of these tasks call CacheHandler, which is a façade for all Cache related operations.
CacheHandler, depends on MyCacheProvider for all its operations.
MyCacheProvider is the core class for handling the cache.
MyCacheProvider has a map to hold data. It holds data in a ConcurrentHashMap.

Application needs a cache.properties file with following configurable properties:
intial.cache.size - Initial cache Size.
invalidate.object.interval - Interval to invalidate the object, or remove o0bject from cache.
refresh.cache.interval - Interval for Refreshing the cache.
print.map.timer - Interval to print the contents of cache map.
source - source of the data provider.
Valied values are :
collection – if data is provided via
command Line arguments
database - if data provider is database
file - if data provider is file.


#Database properties
#if database as resource, below properties are mandatory
db.driver=
db.userId=
db.pwd=
db.protocol=
db.dbname=
#optional Database properties
db.createdatabase=
db.createTable=

# File properties.. if “file” is data provider, i.e. source property is file
file.path=C:\\test.txt

Below is the package Structure and UML class diagrams:
Classes:












!!!Any Comments would be really appreciated!!!

Top Most Oracle Freshers Interview Question Paper - 2


1.There are six steps that lead from the first to the second floor. No two people can be on the same step
Mr. A is two steps below Mr. C
Mr. B is a step next to Mr. D
Only one step is vacant ( No one standing on that step )
Denote the first step as step1 and second step as step2 etc.

Which of the following is false

i. B&D can be both on odd-numbered steps in one configuration
ii. In a particular configuration A and C must either both an odd numbered steps or both an even-numbered steps
iii. A person E can be on a step next to the vacant step.

(A) i only
(B) ii only
(C) iii only
(D) both i and iii


2. If a boat is moving in upstream with velocity of 14 km/hr and goes downstream with a velocity of 40 km/hr, then what is the speed of the stream ?

(A) 13 km/hr
(B) 26 km/hr
(C) 34 km/hr
(D) none of these


3. Find the value of ( 0.75 * 0.75 * 0.75 - 0.001 ) / ( 0.75 * 0.75 - 0.075 + 0.01)

(A) 0.845
(B) 1.908
(C) 2.312
(D) 0.001


4. A can have a piece of work done in 8 days, B can work three times faster than the A, C can work five times faster than A. How many days will they take to do the work together ?

(A) 3 days
(B) 8/9 days
(C) 4 days
(D) can't say

5. A car travels a certain distance taking 7 hrs in forward journey, during the return journey increased speed 12km/hr takes the times 5 hrs.What is the distance travelled

(A) 210 kms
(B) 30 kms
(C) 20 kms
(D) none of these


6. Find (7x + 4y ) / (x-2y) if x/2y = 3/2 ?

(A) 6
(B) 8
(C) 7
(D) data insufficient


7. If on an item a company gives 25% discount, they earn 25% profit. If they now give 10% discount then what is the profit percentage.

(A) 40%
(B) 55%
(C) 35%
(D) 30%


8. What does the following piece of code do ?
sprintf(retbuf, "%d", n);

(A) Print the Integer value of n
(B)  Copy the string representation of the integer variable n into the buffer retbuf
(C) Print the Float value of n.
(D) Print the string representation of the integer variable n.


       9. What is wrong with the program
double d;
scanf("%f", &d);

(A) Instead of %f , %lf should be used for formatting
(B)   Instead of %f , %d should be used for formatting
(C)  Instead of %f , %D should be used for formatting
(D) Instead of %f , %n should be used for formatting





10. What fucntion will read a specified number of elements from a file?

(A) readfile()
(B) fread()
(C) fileread()
(D) getline()


11.
    #include <stdio.h>
    void func()
     {
     int x = 0;
     static int y = 0;
     x++; y++;
     printf( "%d -- %d\n", x, y );
     }
    int main()
    {
    func();
    func();
    return 0;
    }
   
What will the code above print when it is executed?
(A) 1-- 1    
1 – 1
(B) 1 -- 1   
1 -- 2   
(C) 1 -- 1  
2 -- 1 
(D) 1 -- 0   
      1 -- 0   


12. What is the output of the following loop:
for(I=0, j=I++; j>I; j++, I++)
{
printf(“%d%d”, I, j);
}

(A) 0,1
(B) 0,0
(C) Infinite loop
(D) No output


13. Consider the following structure
        struct
        {
              int data;
              struct node *prev;
             struct node *next;
        }node;
       
NULL ß                5                à               8                 à              10               à  NULL
ß                                    ß            
                                p                                  q                                     r

What will be the value of r à prev à next à data ?

(A) 10
(B)   8
(C) 5
(D) NULL


14. What is the output :
       void  main()
       {
           int a,b=5,c=10;
           a = (b-c) > (c-b) ? b : c;
           printf(“%d”,a);
        }
(A) 10
(B)  5
(C) 0
(D) Error


15. Which section of a PL/SQL block would most likely contain a RAISE statement?

(A) Header
(B) Declarative
(C) Executable
(D) Exception


16. Select the VALID trigger type(s)?

(A) AFTER statement trigger
(B) INSERT row trigger
(C) DELETE row trigger
(D) All of the above




17.  Which section of a PL/SQL block would most likely contain a RETURN statement?

(A) Header
(B) Declarative
(C) Executable
(D) Exception


18. Select the non valid  PL/SQL Data Type(s)?

(A) BOOLEAN
(B) LONG
(C) STRING
(D) DATE


19. Which function below can best be categorized as similar in function to an IF-THEN-ELSE statement?

(A) SQRT
(B) DECODE 
(C) NEW_TIME
(D) ROWIDTOCHAR


20. Which one  of the following does not require a number parameter?

(A) sinh
(B) to_number
(C)  SQRT
(D) round  


21. The user issues the following statement. What will be displayed if the EMPID selected is 60494?

SELECT DECODE(empid,38475, "Terminated",60494, "Recruited",
 "Not Recruited")  FROM emp;

(A) 60494
(B) 38475
(C) Terminated
(D) Recruited




22. In order to perform an inner join, which criteria must be true?

(A) The common columns in the join do not need to have shared values.                                                                         
(B) The tables in the join need to have common columns.
(C) The common columns in the join may or may not have shared values.                                                                         
(D) The common columns in the join must have shared values.    


23. Once defined, how long will a variable remain so in SQL*Plus?

(A) Until the database is shut down
(B) Until the instance is shut down
(C) Until the statement completes
(D) Until the session completes


24. The default character for specifying runtime variables in SELECT statements is

(A) Ampersand
(B) Colon
(C) Hash
(D) Astreik  


25. A user is setting up a join operation between tables EMP and DEPT. There are some employees in the EMP table that the user wants returned by the query, but the employees are not assigned to departments yet. Which SELECT statement is most appropriate for this user?

(A) select e.empid, d.head from emp e, dept d;
(B) select e.empid, d.head from emp e, dept d where e.dept# = d.dept#;
(C) select e.empid, d.head from emp e, dept d where e.dept# = d.dept# (+);
(D) select e.empid, d.head from emp e, dept d where e.dept# (+) = d.dept#;


26. For avoiding a Cartesian product of 4 tables, the minimum no: of Joins required after WHERE clause is:

(A) 2
(B) 3
(C) 4
(D) 5




27. Which one of the following uses of the HAVING clause is inappropriate?

(A) To put returned data into sorted order    
(B) To exclude certain data based on known criteria
(C) To include certain data based on unknown criteria
(D) To include certain data based on known criteria                                                                         
      
  
28. The “emp” table contains 14 rows. How many rows will the following query return?
SQL> Select * from Emp where rownum > 5;

(A) 9
(B) 10
(C) 0
(D) Error


29. Which line in the following SELECT statement will produce an error?

Line1: SELECT dept, AVG(salary)                                                                       
Line2: FROM emp                                                                         
Line3: GROUP BY empid;
 
(A) Line 1 and Line 2
(B) Line 3
(C) Only Line 1                                                                       
(D) There are no errors in this statement.


30. Which of the following integrity constraints automatically create an index when defined?

(A) Foreign keys                                                                          
(B) Unique constraints and Primary Keys   
(C) NOT NULL constraints 
(D) Both a and b.

LinkWithin

Related Posts Plugin for WordPress, Blogger...

Labels

Core Java programming core java interview question Core Java Faq's Servlets coding database jsp-servlet spring Java linux unix interview questions java investment bank Web Services Interview investment bank mysql Senior java developer interviews best practices java collection tutorial RMI SQL Eclipse FIX protocol tutorial tibco J2EE groovy java questions SCJP grails java 5 tutorial jdbc beginner error and exception Design Patterns Java Programming Tutorials fundamentals general object oriented programming xml Java Programs Hibernate Examples Flex JAMon Java xml tutorial logging Jsp Struts 2.0 Sybase and SQL Server debugging java interviews performance FIX Protocol interview questions JUnit testing WebSphere date and time tutorial experienced java IO tutorial java concurrency thread Ejb Freshers Papers IT Management Java Exapmle Java Script SQL and database tutorial examples Scwcd ant tutorials concurrency example and tutorial future state homework java changes java threading tricky Agile Business of IT Development JSTL Java JSON tutorial Java multithreading Tutorials PM Scrum data structure and algorithm java puzzles java tips testing tips windows 8 5 way to create Singleton Object Architect Interview Questions and Answers Architecture Architecure Bluetooth server as swing application that searches bluetooth device in 10 meter circle and show all devices. You can send file to any bluetooth device. C Programming CIO Callable Statement in Java Circular dependency of Objects in Java Comparable Example in Collection Custom annotation in Java Developer Interview Divide and rule example in java Drupal Example of Singleton Pattern FIX protocol ForkJoin Example in Java 7 Get data from dynamic table with Java Script Git HTML and JavaScript Health Hello World TCP Client Server Networking Program Hibernate Basics Hibernate Interview Question Answer J2EE Interview Question And Answers J2ME GUI Program JEE Interview QA JMS interview question Java J2EE Hibernate Spring Struts Interview Question Java System Property Java Threads Manager Portlets Provident Fund Read data from any file in same location and give the required result. Reading Properties File in Java Redpoint Rest WebService Client Rest Webservice Test SAL join with ven diagram SCP UNIX COMMAND SSL Singleton Pattern in Java Spring Bean Initialization methods and their order Spring Interview Questions Struts Struts 2.0 Basics Struts 2.0 Design Pattern Submit Html Form With Java Script On The Fly Unix executable For Java Program XOM DOM SAX XP books computers core java; core java; object oriented programming data structure; java investment bank; design pattern dtd duplicate rows in table get browser name with jquery grails podcast inner class java beginners tutorial java cache java networking tutorial java spring java util; java collections; java questions java.java1.5 linked list mailto function with all browser oracle database oracle duplicate rows orm schema social spring mvc questions struts transaction tricks tweet windows xslt