File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Clarifications/Suggestions in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Clarifications/Suggestions in Java" Watch "Clarifications/Suggestions in Java" New topic
Author

Clarifications/Suggestions in Java

Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Hi Ranchers,
I have below clarifications.


1> I have a Employee class with six employee objects{e1,e2,e3,e4,e5,e6} added to it.How do i sort it using Tree Set ?Code example please
2> What is load factor in HashMap.?
3> Can i subclass a singleton class ?
4> what is lazy loading of Servlets ?
5> how do you intialize a session in Servlets ?
6>what is the pros and cons os using jsp:use bean tag
7> what is scope in use bean tag
8> give an example where you can use DynaAction form.Code example please



Thanks and Regards,
Deepak Lal


When The Going Gets Tougher,The Tougher gets Going
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

The first 7 questions can be answered each within a few minutes by just a bit of searching. You should DoYourOwnHomework.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Rob,i will be thanful if you help me this time.because few of the answers to questions are confusing and im unaware of the correct answer.Need help Ranchers
James Elsey
Ranch Hand

Joined: Dec 21, 2007
Posts: 228

Smells like homework ;)

Do as Rob suggested and have a quick search on google, attempt to write some code examples and if they fail post them up and we can help out

There is a good quote : If you give a man a fish, he'll eat for a day, if you teach a man to fish, he'll eat for a lifetime

In short, you would benefit soooo much more than being given answers!


Kind Regards, James. OCPJP 1.6 || My SCJP / OCJCP Study Notes
Interested in : SCJP, Google App Engine, Stripes, Android;|| My Bite-Size SCJP Study Blog
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

And it would likely be quicker.

Try posting your answers to the questions once you've done some research--if you have specific questions about anything you discovered we'll be happy to help!
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Hi Ranchers,
Hope you can help me now.




1> I have a Employee class with six employee objects{e1,e2,e3,e4,e5,e6} added to it.How do i sort it using Tree Set ?



2> Can i subclass a singleton class ? I have not tried but i *think* we cannot subclass a singleton class.Need example for this

3> How do you intialize a session in Servlets ?

4> give an example where you can use DynaActionForm ? Please help me with this..


Thanks and Regards
Deepak Lal
Gupta Tarun
Greenhorn

Joined: Sep 16, 2010
Posts: 22

Hints: not in the same order as your questions..

  • Private Constructors
  • Comparator/Comparable

  • One of your answers is correct.
    Google search..
    Bobby Smallman
    Ranch Hand

    Joined: Sep 09, 2010
    Posts: 107
    Something about posting eight questions in a single post on topics which are relatively well covered makes me less inclined to give large amounts of time responding. But I will however direct you to another post which 1/8th of your post was inquiring about.

    http://www.coderanch.com/t/511303/java/java/Sort-Name


    Everyday in every way, we get a little better.
    Deepak Lal
    Ranch Hand

    Joined: Jul 01, 2008
    Posts: 507

    Bobby Smallman wrote:Something about posting eight questions in a single post on topics which are relatively well covered makes me less inclined to give large amounts of time responding. But I will however direct you to another post which 1/8th of your post was inquiring about.

    http://www.coderanch.com/t/511303/java/java/Sort-Name


    I did go to website URL but i need to sort employee objects using TreeSet that is the requirement im having,help me out please.
    1> Question Answer




    Please help me,how should i proceeed further.

    2> Can we subclass singleton class ...Please help me ?
    David Newton
    Author
    Rancher

    Joined: Sep 29, 2008
    Posts: 12617

    I'm still not satisfied with the level of effort shown, and this still smacks *loudly* of homework and/or interview questions.

    Regarding TreeSet: have you read the documentation? If not, go do so now. If you have, what does it say about ordering and the iterator?
    Joanne Neal
    Rancher

    Joined: Aug 05, 2005
    Posts: 3169
        
      10
    Deepak Lal wrote:2> Can we subclass singleton class ...Please help me ?


    Why don't you write a simple singleton class - there are plenty of examples of how to do this here on javaranch and elsewhere - and then write a second class that extends it. Then see if it compiles. If it does then see if it runs. Then you have your answer.


    Joanne
    Deepak Lal
    Ranch Hand

    Joined: Jul 01, 2008
    Posts: 507

    David Newton wrote:I'm still not satisfied with the level of effort shown, and this still smacks *loudly* of homework and/or interview questions.

    Regarding TreeSet: have you read the documentation? If not, go do so now. If you have, what does it say about ordering and the iterator?


    1> David i have read the documentation of TreeSet and it says you need to implement Comparator interface and thats what i have done in the code..Could you please please render help.

    2> Regarding subclassing a singleton class,it gets subclassed.but i want to know why it is possible because a singleton by definition itself gives a single instance of class which has a private constructor,so whats the use of
    subclassing a singleton class.Please clarify me on this point ?
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 36501
        
      16
    Please only use code tags for code. And avoid very long lines.
    Kiaamaa Liammes
    Ranch Hand

    Joined: Oct 03, 2009
    Posts: 32

    //I'm stuck here.can you help me out now
    SortedSet s = Collections.synchronizedSortedSet(new TreeSet());
    // i dont know how to proceed at this step.Please help me now.


    you were looking for a example of sorting using TreeSet, but the line you are stuck ,indicates you are trying to synchronize access to your treeSet


    SCJP ,SCWCD
    David Newton
    Author
    Rancher

    Joined: Sep 29, 2008
    Posts: 12617

    Deepak Lal wrote: 1> David i have read the documentation of TreeSet and it says you need to implement Comparator interface and thats what i have done in the code..Could you please please render help.

    Help with what? If you have a comparator, and you have a TreeSet, it's sorted. Re-read the documentation.
    Joanne Neal
    Rancher

    Joined: Aug 05, 2005
    Posts: 3169
        
      10
    Deepak Lal wrote:Regarding subclassing a singleton class,it gets subclassed.but i want to know why it is possible because a singleton by definition itself gives a single instance of class which has a private constructor,so whats the use of
    subclassing a singleton class.Please clarify me on this point ?


    Show us the code for your singleton class and subclass.
    Deepak Lal
    Ranch Hand

    Joined: Jul 01, 2008
    Posts: 507

    Singleton class and subclass -->Can you check whats wrong.

    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19543
        
      16

    A constructor will always call a constructor from its super class. This is called constructor chaining. But to be able to call that constructor it must be visible for the subclass. Private constructors aren't.
    Joanne Neal
    Rancher

    Joined: Aug 05, 2005
    Posts: 3169
        
      10
    Deepak Lal wrote:Singleton class and subclass -->Can you check whats wrong.



    You've correctly implemented a singleton class and when you tried to write another class that extended it you got an error.
    Your original question was
    Deepak Lal wrote:Can i subclass a singleton class ?
    So, based on what you found, what do you think the answer is ?
    Deepak Lal
    Ranch Hand

    Joined: Jul 01, 2008
    Posts: 507

    Since we have private Constructors in a subclass which extends a Singleton class,we cannot subclass a Singleton class.
    Joanne Neal
    Rancher

    Joined: Aug 05, 2005
    Posts: 3169
        
      10
    Deepak Lal wrote:Since we have private Constructors in a subclass which extends a Singleton class,we cannot subclass a Singleton class.


    Try making the constructor in the subclass public and see if anything changes.
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19543
        
      16

    Although that allows you to subclass a class it will no longer be a singleton - the class can be instantiated from anywhere. Singletons that are designed to be subclassed usually have protected constructors.
    Joanne Neal
    Rancher

    Joined: Aug 05, 2005
    Posts: 3169
        
      10
    Rob Prime wrote:Although that allows you to subclass a class it will no longer be a singleton - the class can be instantiated from anywhere. Singletons that are designed to be subclassed usually have protected constructors.


    I wasn't talking about the constructor in the singleton class.
    Deepak said that the reason you couldn't subclass a singleton was because the constructor in the subclass was private. I was just suggesting he tried making it public and he would have noticed that it made no difference i.e. the access modifier of the subclass constructor has nothing to do with not being able to extend a singleton class. It is, as you said earlier, the fact that the singleton class constructor is private that means you can't extend it.
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19543
        
      16

    I missed the "subclass" there
    Deepak Lal
    Ranch Hand

    Joined: Jul 01, 2008
    Posts: 507

    I hope Im rightly concluded that "The singleton class constructor is private that means you can't extend/subclass it."
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19543
        
      16

    That conclusion is 100% correct. Because of constructor chaining the super class constructor must be visible, and with private it simply isn't.
    Deepak Lal
    Ranch Hand

    Joined: Jul 01, 2008
    Posts: 507

    Hi Rancers and Bartenders,
    I have few queries in Java Collections framework as well.

    1>Suppose i have 5 elements added to a ArrayList and Vector.? suppose i now add 6th element to ArrayList and Vector.How will it be internally added in both the cases.Please explain

    2>In which scenario would i use a List and a Map except for the normal differences being that list is being used for sequential addition of elements and Map is for (Key,Value) Pair ?
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19543
        
      16

    Deepak Lal wrote:1>Suppose i have 5 elements added to a ArrayList and Vector.? suppose i now add 6th element to ArrayList and Vector.How will it be internally added in both the cases.Please explain

    In your JDK folder there is a file called src.zip. Unpack it, then look for files java/util/ArrayList.java and java/util/LinkedList.java. That's the source of these classes.

    2>In which scenario would i use a List and a Map except for the normal differences being that list is being used for sequential addition of elements and Map is for (Key,Value) Pair ?

    You've already given the answer yourself, haven't you?
    Deepak Lal
    Ranch Hand

    Joined: Jul 01, 2008
    Posts: 507

    1> i asked for ArrayList which implies ArrayList.java and i asked for Vector ,does the Vector implementation refer to LinkedList.java as you have pointed out.
    2> More differences,The interviewer was not happy with this answer,He wanted more differences from a realistic point of view.he said thats a common answer.
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19543
        
      16

    Deepak Lal wrote:1> i asked for ArrayList which implies ArrayList.java and i asked for Vector ,does the Vector implementation refer to LinkedList.java as you have pointed out.

    Sorry, that should have been java/util/Vector.java of course.
    Deepak Lal
    Ranch Hand

    Joined: Jul 01, 2008
    Posts: 507

    Rob,
    what about the answer to second question.COuld you help me with that please. More differences required with respect to List and a Map.
    Regards
    Deepak Lal

    [removed excessive use of font and color]
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19543
        
      16

    I believe that searching around in this forum, Beginning Java, the Java tutorials and Google, you should be able to find more than enough answers on that question. We do require everyone to ShowSomeEffort.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 36501
        
      16
    Rob Prime wrote: . . . We do require everyone to ShowSomeEffort.
    Deepak Lal: this site is not some sort of substitute for your reading your textbook, turning up to lectures, looking at web tutorials, or doing any work at all.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Clarifications/Suggestions in Java
     
    Similar Threads
    Is XDoclet useful if i am using IBM Websphere
    hi friends this is regarding Servlets, JSP, HTML and XML?
    html:select with two List
    use of type in <jsp:usebean>
    Interceptors in Struts 2