• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

IBM Mock question

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can somebody tell me the correct answer for this question:
Assuming the code below is part of a Frame subclass, which are true?
1.public LayoutFrame() {
2. setLayout(new GridLayout(1, 3));
3. Button b = new Button("What is b location?");
4. Button a = new Button("What is a location?");
5. Panel p1 = new Panel();
6. p1.setLayout(new GridLayout(2, 1));
7. p1.add(a);
8. p1.add(b);
9. add(p1);
10. }
a) Two push buttons are added. Each is half the width of the Frame and the entire length vertically, with a above b.

b) There is a compile error since there is no receiving object specified for the methods in lines 2 and 9.

c) Two push buttons are added, each just larger than the size of the label and located in the center of the Frame.

d) There is a compile error since the superclass constructor is not called.

e) Two push buttons are added. Each is half the size of the entire Frame vertically and the entire width, with a above b.

any comments are appreciated!
 
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you considered compiling it to see what happens?
Marcus
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not only that, Marcus, but it would be nice if the person says what s/he thinks that answer is and why..., no?
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Actual answer is E.
1st grid layout is for setting the panels in the frame and so the panels will be placed according to specification as 1 row and 3 col.
2nd grid layout is for the panel. which divides the height of the panel to accomodate 2 buttons as specification for this grid layout is 2,1 so 2 rows and 1 col is created.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
haiy all,
i have also run the programm, who can tell me why the sentence No. 2 is not used, i think the Frame is with a GridLayout(1,3),
and the Panel is with a GridLayout(2,1)?!

regds
zhewen
 
Jason
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Marcus and Tony;
sorry about the posting.
the reason I did not put up my opinion there is that I disgree with any one of given answer. I think this code should compile ( sorry, my computer is out on strike again ) and the final appearance should be that the panel occupies the first column of the frame and two buttons are displayed and each is half of the height of frame and one third of frame width (Frame is laid out one row and three column).
any comments?
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anwser is E. I have run the code and verified it. The GridLayout Managers ignores the components preferred size. So when you have components fewer than the cells(parent Layout) it behaves strangely.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Praveen:
Anwser is E. I have run the code and verified it. The GridLayout Managers ignores the components preferred size. So when you have components fewer than the cells(parent Layout) it behaves strangely.


You're essentially adding one component (a panel) to a Frame that can hold three components in a GridLayout. In these situations, the contract behavior for GridLayout is to reduce the column count. If you were adding more components than dedicated slots, the contract behavior is to add to the column count. Nothing strange about it. Rows before columns.
 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i was pretty apprehensive before compiling this code but no it is the obvious... answer e.
i think the confusion is regarding the panel in the gridlayout(1,3)
well the fact that if both the rows and the columns are specified, only the rows are respected.wring (1, 3) is as good as writing (1, 5) or (1, 6) since the layout depends only on the no. of components and the rows specified.
so that is what happens... (1, 3) with only one component(panel) means that there is only one row and the component occupies the whole of it. what happens inside the panel is its story.
i suggest you replace (1, 3) with the following and then compile and see the results...
a) (2,0) - # of rows respected hard and fast
b) (0,2) - # of columns hard and fast
i am getting satisfactory o/p... od tell me about yours
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jason ...
JavaRanch has a Name Policy. Would you please read the policy and re-register with a name that complies.
Thanks for your cooperation.

------------------
Jane Griscti
Sun Certified Java 2 Programmer
"When ideas fail, words come in very handy" -- Goethe
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic