This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Checkboxes? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Checkboxes?" Watch "Checkboxes?" New topic
Author

Checkboxes?

sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435
Which checkboxes will initially be selected when the following program is run?

Options :
1)All Checkboxes
2)None of the Checkboxes
3)It will throw Runtime Exception
4)The checkbox labeled Checkbox0 and Checkbox2
5)The checkbox labeled Checkbox3 and Checkbox4
Answer : 5)
I just want to check that the answer is 5) ONLY because of the statement cbgs[i%2] where 2 refers to the last two checkboxes.
Am I right?
Sonir
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
i ran your code and yes the checkbox 3 and 4 were selected. so your answer is right.
BUT i just could not understand this line
CheckboxGroup[] cbgs = new CheckboxGroup[] { new CheckboxGroup(), new CheckboxGroup()};
can someone explain this line ?
the array cbgs[] has how many memebers ? each is an object of class CheckboxGroup.

Originally posted by sonir shah:
Which checkboxes will initially be selected when the following program is run?

Options :
1)All Checkboxes
2)None of the Checkboxes
3)It will throw Runtime Exception
4)The checkbox labeled Checkbox0 and Checkbox2
5)The checkbox labeled Checkbox3 and Checkbox4
Answer : 5)
I just want to check that the answer is 5) ONLY because of the statement cbgs[i%2] where 2 refers to the last two checkboxes.
Am I right?
Sonir
bob dapaah
Ranch Hand

Joined: Jan 15, 2002
Posts: 46
The i%2 does not refer to the last two elements. % is the modulo operator (gives the remainder). So for iteration 1:
i = 0 so 0%2 = 0
This Checkbox is therefore assigned to the first CheckboxGroup in the array. for iteration 2:

i = 1 so 1%2 = 1 (remember this gives the remainder).
This Chechbox is therefore assigned to the second CheckboxGroup in the array. For itearation 3:
i = 2 so 2%2 = 0
This Checkbox is therefore assigned to the first CheckboxGroup in the array.
Two things are important to remember in this situation:
*Arrays begin indexing (counting) from 0 so the first element is always at the 0 position (the Checkboxgroups array for example).
*Only one Checkbox can be selected in any CheckboxGroup (no multiple selections). So the last one to be added (in this example) to any Checkboxgroup will be the only one selected (all the Checkboxes in this example are constructed with the 'selected' boolean set to true)
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Sonir i%2 does not mean "the last two checkboxes" but it is just a way or organizing the checkboxes into the CheckBoxGroup.
mark,
CheckboxGroup[] cbgs = new CheckboxGroup[] { new CheckboxGroup(), new CheckboxGroup()};
is a way of declaring and initializing an array more rapidly. That is, we have an array of CheckboxGroup elements (or components in the JLS jargon, but nothing to do with AWT components) named cbgs containing two CheckboxGroups.
You could do the same work as follows:
CheckboxGroup[] cbgs = new CheckboxGroup[2];
cbgs[0] = new CheckboxGroup();
cbgs[1] = new CheckboxGroup();
Now, sonir, you see that we have two CheckboxGroups in which we would like to put our Checkboxes... When we build a Checkbox instance we tell him to which CheckboxGroup it will belong by giving as the third argument to the Checkbox constructor the reference to the CheckboxGroup, like this :
new Checkbox("Checkbox"+i, true, cbgs[i%2] );
This means that the first checkbox (i=0) will be affected to CheckboxGroup 0%2=0 (the first group),the second Checkbox to 1%2 = 1 (the second group), the third Checkbox to 2%2 = 0 (the first group), the fourth Checkbox to 3%2 = 1 (the second group) and finally the fifth Checkbox to 4%2 = 0 (the first group).
To sum up, we have the 1st, 3rd and 5th Checkboxes in the first CheckboxGroup and the 2nd and 4th in the second CheckboxGroup.
HIH
[ January 20, 2002: Message edited by: Valentin Crettaz ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435
Hello Valentin.
Infact its making me more confused!!
first of all how are two groups of checkboxes formed?
And how does it select only the last 2 checkboxes in the output?
U said that 2 groups are formed : one containing 1,3,5 checkboxes and second group contains 2,4 checkboxes, but how are they related with the output i.e 3 ,4 checkBoxes.

Sonir
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Sonir,
the CheckboxGroups are constructed by the line:
CheckboxGroup[] cbgs = new CheckboxGroup[] { new CheckboxGroup(), new CheckboxGroup()};
I've already explained how that line works above.
Now, when several Checkboxes are in a CheckboxGroup, only one of them can be checked.
At the beginning your groups are empty. Then you create new Checkboxes and add them to each group. In the Checkbox constructor, the second argument (true in this case) tells you that the Checkbox is checked by default. If a group already contains a checked Checkbox and you add a new checked Checkbox, the new Checkbox is the one to be checked now and the previous one is unchecked. That's why the last two Checkboxes to be added to each group are the one to be checked, hence the selected Checkboxes, 3 and 4.
To sum up,
CheckboxGroup 0 contains:
Checkbox 0 (unchecked)
Checkbox 2 (unchecked)
Checkbox 4 (checked)
CheckboxGroup 1 contains:
Checkbox 1 (unchecked)
Checkbox 3 (checked)
HIH
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Checkboxes?