Sebanti Sanyal

Ranch Hand
+ Follow
since Nov 07, 2011
Sebanti likes ...
Eclipse IDE C++ Java
Merit badge: grant badges
For More
United States
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
9
Received in last 30 days
0
Total given
4
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Sebanti Sanyal

Ravi KumarJd wrote:Great answer - Thank you. I agree with you we cannot import instance methods or attributes directly with import statement. I wondered why the compiler not stopping me, when I append .* after a concrete class. If I understand correctly, for non static concrete Types/classes it's NOT better to append .* after the class name though the compiler okay.


If TestClass contained a static nested class or an enum, you could have imported them (as types) with import package2.TestClass.*;
Wanted to mention a situation here when the non-serializable superclass does not have a no-arg constructor. De-serialization in that case will throw a run-time exception.


As it stands, the code will not compile as the code does not acquire a lock on t before calling t.wait();



No, it will compile but result in a run-time exception.
You may also look into the JAMA package - a basic linear algebra package for Java. Here is the link.
12 years ago

bob fissle wrote:I'm trying to write a loop that will print to the buffer based on the following conditions:




The example above is a dumbed down version of what I'm trying to do, I'm mainly interested in the logic. Please assume that variables a and b will have an infinite amount of numbers -- for the sake of this example. The main point that i'm trying to illustrate is the pattern in the variables a and b; a starts with 0 and 1, and then every other two numbers are found in the variable a, and the opposite for b. I'm trying to figure out how to write logic that will print out values a or b based on the pattern described above inside a loop.



I believe there are four AP(Arithmetic Progression) series, stating from 0,1,2 and 3 respectively. The constant difference is 4 in each case.
Series 1: 0,4,8,12 .. prints a
Series 2: 1,5,9,13 ...prints a
Series 3: 2,6,10,14 ..prints b
Series 4: 3,7,11,15 .. prints b
Please check if the following code works.
12 years ago

Helen Ma wrote:Hi, Sebati,

Do you think the answer is only C. A few people on the forum feel that the only answer is C, not D. I don't think we need to worry about x.equal (null) for the exam. If the string code is null, the code.length will throw a null pointer exception, that is not the objective of this question.



D is incorrect. If the null input was taken care of, C would be the correct answer.
Thanks for your replies.

Dennis Deems wrote:Sebanti, can you please tell us what edition of K&B you are using?


Its 2008 edition for Java SE6



Which of the following will fulfill the equals() and hashCode() contracts for this class? (Choose all that apply.)
Answers are:
A. return ((SortOf)o).bal==this.bal;
B. return ((SortOf)o).code.length()==this.code.length();
C. return ((SortOf)o).code.length()*((SortOf)o).bal==this.code.length()*this.bal;
D. return ((SortOf)o).code.length()*((SortOf)o).bal*((SortOf)o).rate==this.code.length()*this.bal*this.rate;

Correct Answers: C & D
EXPLANATION:
C and D are correct. The equals() algorithm must be at least as precise in defining what "meaningfully equivalent" means as the hashCode() method is.
A and B are incorrect because these equals() implementations would allow instances to be equal that hashCode() would not see as equal.


Ref: question from K&B MasterExam CD

I feel only C is correct. I tested with D using the following main() function.

Output is: true false

Am I missing something here?
It is just the opposite. First example uses two different Runnable targets for the two threads. These two threads can concurrently execute the run() method. Hence, the output can be AABB or ABAB.
The second example uses the same Runnable object while creating two threads. The 'synchronized' keyword actually comes into play here.Once a thread is inside the run() method, the other thread has to wait for the former to complete. Therefore, the output has to be ABAB.
Thank you very much Stephan for the clear explanation.


Final Fields Initialization

Final fields don't get default values, they have to be explicitly initialized.
A final variable can only be initialized once, either via an initializer or an assignment statement. If a final instance variable is not assigned a value - there will be a compiler error !
If not initialized at the point of declaration: this is called a 'blank final' variable.
A blank final instance variable of a class must be definitely assigned at the end of every constructor of the class in which it is declared or an instance initializer block can be used.
Similarly, a blank final static variable must be definitely assigned in a static initializer of the class in which it is declared.
Instance Init Blocks or constructors cannot assign values to final static variables - this will be a compiler error. Why ? An object of the class might never be created and the static final variable will be unintialized.
A final field has to be initialized in every constructor OR the compiler will complain.
Alternatively the final field can be assigned in an intializer block, however if the field is also being assigned in the constructor then the compiler complains.
Why ? Because the initializer runs before rest of the constructor body, so it will amount to reassigning the final variable which is not allowed.


Ref:suhrid.net/wiki-Instantiation
Thanks for your reply. Is there any correlation between the end of run() method and release of locks or notification?
To enable assertion on Eclipse, select from Menu Bar,Run ->Run Configuration->Arguments tab.Now type -ea in VM arguments and click on Apply.Now you'll get the AssertionError when you run. I am using Mac OS X 10.5.8.