henry joe

Ranch Hand
+ Follow
since Jul 24, 2012
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
2
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by henry joe

Hi guys,

Please, this question is from the OCP Java SE 6 Programmer Practice Exams textbook, Assessment Test 2 question 11. Below is the code for the question:



class A{}
class B extends A {}
class C extends B {}




I don't quite understand the syntax of the generic declaration at the class level(line 1) and at the method level(line 2). Anyone care to explain them to me and also point me to a basic beginner's tutorial on Java Generics?

Thanks

Paul Anilprem wrote:

henry joe wrote:

Henry Wong wrote:

henry joe wrote:
you can check the code in the text book and you will see the Friends class is an inner class. I did instantiated the outer class to get it to work. I am now interested in understanding why output was null when I printed it to the console.



Perhaps you can tell us what you think it should be, if it is not null.

Henry



Please Henry Wong, I am just seeking to understand why they gave the answer as null. IF I knew, would I be here asking? I am a beginner java developer and interested in understanding some things that are not so clear with the language. The programme did output null but I want to know why?


Well, it is very simple actually. You put something in the Map and you are trying to retrieve it back. The way a map works is that first you associate a key with a value and to retrieve the value later on, you need to give that same key. Here, "same" means that the object that you gave as a key earlier should be "equal" to the object that you are giving now to retrieve the value. i.e. key1.equals(key2) should return true.

Do you think that is happening in your code for it to not print null?



Thanks alot for the clarity. According to the text book, that code was wrong and I had thought the answer would be C which is : it won't compile. The reason is because you have to access the Friends methods through the Birthdays Object since Friends is an inner class but in the code, that was NOT the case. So, why did they give a wrong code and instead of choosing C as the answer, they chose D as the answer?

Henry Wong wrote:

henry joe wrote:
you can check the code in the text book and you will see the Friends class is an inner class. I did instantiated the outer class to get it to work. I am now interested in understanding why output was null when I printed it to the console.



Perhaps you can tell us what you think it should be, if it is not null.

Henry



Please Henry Wong, I am just seeking to understand why they gave the answer as null. IF I knew, would I be here asking? I am a beginner java developer and interested in understanding some things that are not so clear with the language. The programme did output null but I want to know why?

Henry Wong wrote:

henry joe wrote:In the Java OCP Java SE 6 Programmer Practice Exams book, on page 17 is the code fragment below :



Unfortunately, this code doesn't compile, giving the following error :

No enclosing instance of type Birthdays is accessible. Must qualify the allocation with an enclosing instance of type Birthdays (e.g. x.new A() where x is an instance of Birthdays).

.



The reason it doesn't compile is because the Friends class is an inner class, and you can't instantiate an instance of an inner class without an instance of it's outer class.

Now, we could try to work with you to figure out how to use the inner class, but something tells me that you entered it wrong. In the book, have you learned inner classes yet? Meaning is the Friends class supposed to be inner class? Or should it have been just another class ?

[EDIT. Never mind. It looks like your example partially uses the inner class as an inner class, and partially doesn't. The part that doesn't is the incorrect part]

Henry




you can check the code in the text book and you will see the Friends class is an inner class. I did instantiated the outer class to get it to work. I am now interested in understanding why output was null when I printed it to the console.
In the Java OCP Java SE 6 Programmer Practice Exams book, on page 17 is the code fragment below :



Unfortunately, this code doesn't compile, giving the following error :

No enclosing instance of type Birthdays is accessible. Must qualify the allocation with an enclosing instance of type Birthdays (e.g. x.new A() where x is an instance of Birthdays).

. I tried this as shown in the second code below and it compiled BUT in the book, the answer is option A which is null instead of C which is the best answer given the code in the text book is NOT correct and they gave this explanation :

A is correct. The Friends class doesn't override equals() and hasCode(), so the key to the HashMap is a specific instance of Friends, not the value of a given Friends instance's name

. Given the code in the book, I couldn't even get it to compile. I tried going by the instruction given by the error message and modified it as shown below :



It compiles and runs but I got two questions:
1. Why is the code in the text book wrong ?
Since the code is wrong, the answer is supposed to D which is "Compilation fails" but the authors chose A which is null. There is no way one can get null given the code in the book.
2.Could anyone explain why the answer is null. I am not so sure I understood the explanation given in the text book.

Thanks

Jeanne Boyarsky wrote:You could try it and see what the output is. Asking why on the internet is more helpful than asking strangers to run your test examples for you.

I'll pretend you did that and explain why that is not the output.

The constructor for both enums runs. Which prints cp twice and increments count. Then the println with the count in it runs.



Hi, I am sorry, I keep bugging you with the enum issue. Ok, I did test it and found out it goes this way : [c p c p 2 2]

It seems that even though the count++ statement came before the System.out.println("p ") statement, the out seems to work by printing out the strings first, only then is the count increment printed. I am trying to understand if this is how the enum constructor is built to work or something else is happening here. I mean, it is not going sequentially any more but jumping the count++ to the statement below that. This is what I am trying to understand.

Thanks

Henry Wong wrote:

henry joe wrote:
So, what does the block mean in this code, why is the syntax written this way



Well, it's an instance initializer -- which is executed (in textual order) along with initializations of instance variables.


Please, I am just so curious about this instance initializer. I would, finally, like to know why it was used in the code fragment? I never come across such syntax until now. So, what is the use?

Thank

Jeanne Boyarsky wrote:

1) No, it is not a static block. You can tell because the keyword static does not appear immediately before it.
.



So, what does the block mean in this code, why is the syntax written this way instead of this ?

Jeanne Boyarsky wrote:To be more explicit about the source: K&B, practice exams, self assessment test #2.

1) No. When you learn about inner classes, you will see an enum can be an inner class instead of outside a class. For example, this code is fine:


2) An enum creates all the values at the same time, not when you first reference them. This means the two constructors are called one after another. And both are called before the println runs to output the counts.



So, basically, the System.out.println("c ") statement is called one for RAINY and then for SUNNY after which the count is incremented and again, called on the RAINY and then the SUNNY. So, it is calling both on each statement. Whould I be correct IF the statement had been this :


AND THEN..calling this should give : c c 2 2 p p. Am I correct with this assumption?

Jeanne Boyarsky wrote:To be more explicit about the source: K&B, practice exams, self assessment test #2.

The code calls binary search with va[0] as a comparator. Also known as a descending sort.

So your question is, given the array:
a[0] = 200, va[1] = 60 and va[2] = 40
where does 80 go in the list when sorting descendingly.

80 goes between 200 and 60. Which means if we inserted it at index 1 (and moved everything over one), we'd have the descendingly sorted list: 200, 80, 60, 40.

Java then takes that index 1, makes it negative (-1) and subtracts 1 (-2).



Why does Java do this insertion thing when the number is NOT found? Why doesn't it just throw an exception or something like the number is NOT FOUND? Just curious to know the benefit of Java doing the (-(insertion)-1) thing

Thanks
In the code fragment below :



I have the following questions:

1. Is this line {s+= "i ";} a static block in the code?

2. Why must the s2 String variable in the Go class be static ? I noticed when I remove the static keyword, it complains in the super() method.

3.I was expecting it to print out "-s4 sb i" but instead it printed out "-sb s4 i" why is this so? Could anyone explain to me?

Thanks
In the code fragment below :



I have the following questions:

1.Are Enums always declared outside a class in Java?
2. why did the print out in the main print the following : C C 1 1? I thought the counter was increasing.. so that it becomes C 1 C 2. Someone please explain this to me.

Thanks
In the code below :




The second print out returned -2. When I checked the explanation, it says something like : "when binarySearch() can't find any element, it returns (-(insertionpoint)-1) to indicate where the element would have been placed if it had been found". Given this satement, how did the index become -2? Note, this is sorting in a descending manner. so that we now have va[0] = 200, va[1] = 60 and va[2] = 40 after the sort method was called.

Thanks
public class MathBoy {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
long x = 123456789;
short y = 22766;

System.out.printf("%1$+10d %2$010d",x,MAX_VALUE -y);
System.out.println(new Date());
}

Please, my question is on the format, what does "%1$" and "%2$010d" mean? Could someone please explain?

Thanks

Paweł Baczyński wrote:The result is:
[3, 7, 5, 9, 1]

Note that in doStuff() you are adding "9" do list named x and then the reference to that list is stored in variable y.
This results in x and y pointing to the same instance of the list.
So, whatever you do to y is reflected in x.

You are wrong about new ArrayList being created. The only thing that happens is passing reference to an ArrayList object created at line 4.




Thank you alot. I get it now.