Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!

kumarjit banerjee

Ranch Hand
+ Follow
since Mar 27, 2011
kumarjit likes ...
Eclipse IDE Oracle Tomcat Server
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
3
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by kumarjit banerjee

Hi,

I was given a task to upload data into tables from files, call a pl/sql procedure, delete the records from the table. create a log file and a error file if any.
The file will come as
Header
Record:a1,a2,,,,,,,
Record:b1,b2,,,,,,,
Footer
Here the Record is the data I need to upload in the table.

I have implemented it in a java program. The java program will be invoked from a shell script. Upto this, it is fine and I am able to do the thing as expected. However the problem comes when there are more number of records. Also the time taken to connect to the database is considerable amount.

I am making the connection using the classes12.jar. For 4 records, the time taken for connection =2 secs(approx) and for processing = 2secs(approx), which mean half the time is taken for making the connection. Is there any way so the connection may be already available when the jvm is loaded when the shell script is run. Secondly, is there any way so that the performance may be improved. The insert I am using is of the form




As per my my understanding that, when control return from main, the jvm exits. In that case no objects could be present. But can there be any way round regarding the time taken to make the connection.

Jesper de Jong wrote:What do you mean by "it doesn't work"? Does it compile (when you correct the spelling error)? Does it run? Is the output different from what you expect?




Apart from the spelling, if you are working in jdk1.4 the String class would not contain the contains() method so your code will not compile. If you are using jdk1.5 then your code will compile but you try to run the class file in jdk1.4 it will not run.



So instead of writing

if(s.contains('-'))

you can write

if(s.indexOf(""+'-') > -1)
11 years ago
Question:

Problem : A room contains furniture of different weights. The furniture should be arranged from
least weight to the highest from the door of the room. Only swapping is allowed. The cost of
swapping is the sum of their weights. The swapping should be made such that the cost is least.

Sample Input #1 noOfFurnitures = 3 int[]{5,4,2}
Here the weights of the furniture are 5,4,2 units respectively where the index position is the
position of the furniture from the door.

Here the furniture nearest to the door is of weight 5

So swap position 1 and 3 to get {2,4,5} cost = 5+2=7
which is the desired result.

Sample Input #2 noOfFurnitures = 4 int[]{6,1,2,4}

swap position 2 and 3 {6,2,1,4} cost = 1+2 = 3
swap position 3 and 4 {6,2,4,1} cost = 1+4 = 5
swap position 1 and 4 {1,2,4,6} cost = 6+1 = 7

Total cost = 3+5+7 = 15

The code should be contained in the package com.adp.apartment

and should have the method signature

public int getFurnitureMovement(int noOfFurnitures , int[] weight)

You may add as many other methods in your class




I am not sure whether I have implemented it correctly. Even if so, it is not working for duplicates as it not mentioned in the question whether or not the array contains a duplicate. Please suggest me a way to compute in the situation of duplicates.
11 years ago

Dim Dioannou wrote:@kumarjit banerjee thank you
this is what i need

but can you explain the code in line 38, please?




Hi


as.add(String.copyValueOf(m.group().toCharArray(), 6, m.group().length()-7));


This line copies the matched string (m.group returns the total matched string) from the characters of at position 6 to total length-7, this because

xmlns: length=6
= length=1

So the total length =7

Hence the code.
11 years ago



I want to know in this situation which break statement will be ideal. For the labelled break statement, as I know is inferior because it acts somewhat like goto statement in other languages. May be I am wrong, both may perform the same way. Please correct me.
11 years ago

Dim Dioannou wrote:I want to find a word in a text file and then to take a part of it and to
save it in a String.
for example I have this part of text
<rdf:RDF
xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'

and when I find the word ''xmlns:'' I want to take the word after : till =
(rdf in this case) and to put it in a string.

Any idea how to do it?




You may try this.

11 years ago
Hi,

I am facing a problem in eclipse IDE.




In the above code, The exception is printed at different locations when run successively. The ideal output, which I got from running in command prompt, should be



But when run successively I got the output as above and also different output such as









11 years ago

Mohamed Sanaulla wrote:The array in your example is not public.

And this- 4. Methods in ClassA use public variables in ClassB - is the reason why C is true. For something to be public- it has to be declared as public.



Here the main method of Demo class which is public is using the public variable of the array of int that is length.
So the array class of int has weak encapsulation
I am preparing for the SCJP 6 and following the book of K&B and I encountered the following question.

Question

Given:
1. ClassA has a ClassD
2. Methods in ClassA use public methods in ClassB
3. Methods in ClassC use public methods in ClassA
4. Methods in ClassA use public variables in ClassB

Which is most likely true? (Choose all that apply)

A. ClassD has low cohesion
B. ClassA has weak encapsulation
C. ClassB has weak encapsulation
D. ClassB has strong encapsulation
E. ClassC is tightly coupled to ClassA

Ans. C

Then with respect to this question consider the following code



So in the class Demo the public variable length of the array class. So is the array class has weak encapsulation.

Jesper de Jong wrote:
What exactly is your question with Case 2?



I want to know that as I am facing OutOfMemoryError then instead of using clone(), do I have to set each element by using the loop or there are some other way when I only want a deep copy. Or there is some problem with my jvm and how to confirm.


Run the program with a profiler



Can you please suggest any such profiler and guide me where to find it.
11 years ago
Hi,

The basic syntax for for loop is

for(initialization;condition;increment){
Body
}


The initialization is computed once while entering the loop, and the condition and increment occurs at every iterations. In my application I have to use a huge number of data of the order of 6000000. In that case the time to compute is naturally large. I want to demonstrate a simple for loop for various conditions.


Case 1: Making a deep copy of array


The code in the above example as the size =1000, it is running but when the size = 6000000 the code throws Exception
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space.
So I go by the second option.



It runs fine. Please rectify my points if they are correct

1. I have made the size variable final so it will take less time.
2. I have used the clone() method when possible to reduce time.

Case 2: Making an arrray whose elements are the functions of the elements of another array

Please consider the following class Example3. In this class I have implemented that each element in the array will have the value as index position. This is just an example in general, I want to know whether my second implementation will work faster. This is because I have run the program but most of the time the second implementation is taking less time, but sometimes the reverse is also happening. The idea behind this is that, if we reduce the number of checking in the for loop, it will save computation time. The second implementation checks the value of j half number of times ie, 3000000 instead of 6000000.

I have also tried with nano seconds, there the second implementation is taking less time but, it is mentioned in the documentation that
This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time. The value returned represents nanoseconds since some fixed but arbitrary time (perhaps in the future, so values may be negative).





Please help me with this code and if I am wrong somewhere
11 years ago

Manjula Weerasinghe wrote:Hi Kumarjit,
Please read this article http://www.javaranch.com/journal/200409/ScjpTipLine-StringsLiterally.html. It has more information about String literal pool (String constant pool) and garbage collection with some descriptive images.

Each time you execute "java" command a new JVM instance is created for executing relevant class main method. Therefore you will get a new string literal pool for that JVM and will not be able to get the same reference. Since each JVM instance is a separate process and because one process cannot access the memory use by another process, you will get one String literal pool for each JVM independent from other instances.

Thanks & Regards,
Manjula




Thanks Manjula. This link describes the topic in great details. I am now able to understand it properly.

11 years ago

Jeanne Boyarsky wrote:Rakesh,
The point of such an interview question is to show you can write a simple algorithm. Forget about Java. If you were doing this by hand, what would you do? What would you write down or keep track of in your head? Try explaining it here and we can help you clarify your ideas.

kumarjit,
Can you think of a simpler way to do it? That approach is much more complicated than it needs to be. I'm not clear on why you need the Collections.sort or helper objects to solve the problem.




Hi Jeanne,

I agree with you that the code has turned out to be a bit complicated. But according to Rakesh if the following three lines as for example exists in the text file then

"Hi" is the min value and "This is a test line." is the max value for the below text in the file.

Hi
Hello
This is a test line.

But however I also considered that if "Hi" occurs twice or "Ha" occurs which consists of the same number of characters, then both the line numbers should be displayed. So is true for max value. If a blank line is encountered then depending upon the argument value true or false it should be ignored or considered. For this reason the if-else blocks have increased to some extent. The second reason is for formatting the output as mentioned by Rakesh. If I would have printed the maps directly then 40 lines of code may be reduced.

Yes I agree that instead of creating helper objects it may be implemented by array of String and int, but in that case we have to traverse through the whole array. So I chose Collections.sort() which uses the quick sort algorithm so is faster than traversing the whole array. But it obviously involves an overhead of creating the helper objects.

Please correct me if I am wrong and if array implementation will be more time effective.
11 years ago

Manjula Weerasinghe wrote:For reducing the memory usage, java uses something called "String Constant Pool" which is a part of JVM memory. It is a pool of currently using Strings of JVM. If you use,

Then JVM first check whether there is any String equals to "this is a test" exists in the String Constants pool.

*) If there is a string identical to "this is a test" exists in the String Constant pool, JVM get the reference to that String and assign that reference value to "s" reference variable without creating a new String object. So in this case, there is no call to constructor happens.

*) If there is no such string with the value "this is a test" exists in the string constant pool, JVM will create a new String object with the value "this is a test", then add the newly created String object to String constant pool, and assign the reference to newly created string object to "s" variable. Therefore in this occasion, Constructor is called by the JVM to create new String object.





Thanks Manjula.

But I still have one doubt. When I run a program containing the main method, then one instance of the jvm is present. Correct me if I am wrong. So when the program exits the String will be present in the String pool or it will be collected by the garbage collector.
If I again run the same program will the same reference of the previous String will be returned.

If the above statement is false then when the class is loaded then the string pool of the jvm is empty.
Please clarify my doubt and if I am wrong.

Say for example:




In this case when line 1 is executed then constructor of String class is invoked. For line 2 same reference is returned.
For the next time I execute the class, will there be a second constructor call.
11 years ago
Hi,

String objects can be created as literal and also with the new operator. I want to know whether the constructor of the String class is invoked during creation of a String object by literal.

Suppose we write

String s = "abc";

Is the constructor of the String class is invoked?
11 years ago