-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathRadioButtonGroup.java
81 lines (67 loc) · 2.19 KB
/
RadioButtonGroup.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package radiobutton;
import java.util.HashMap;
/**
* Represents a group of "radio buttons": toggle buttons for which exactly one is selected
* at a given time. If a different button is selected, the previously-selected button is
* automatically unselected.
*
* Buttons are numbered from 0 to one less than the number of buttons. For example, if the
* button group has 4 buttons, they are numbered 0, 1, 2, and 3.
*/
public class RadioButtonGroup {
HashMap<Integer, Boolean> radioButtons;
/**
* Creates a group of radio buttons.
*
* @param numButtons
* The number of buttons in the group.
* @param initial
* The button number that is initially selected.
* @raise RuntimeException
* The initial button number is invalid.
*/
public RadioButtonGroup(int numButtons, int initial) {
// TODO: Implement this method.
if(initial > numButtons || initial < 1){
throw new RuntimeException("The initial button number is invalid");
}
radioButtons = new HashMap<Integer, Boolean>();
for(int i = 0; i < numButtons; i ++){
if(numButtons == initial){
radioButtons.put(i, true);
}else {
radioButtons.put(i, false);
}
}
}
/**
* Creates a group of radio buttons. Button 0 is initially selected.
*/
public RadioButtonGroup(int numButtons) {
this(numButtons, 0);
}
/**
* Selects a button, unselecting the button that was previously selected.
* @param button
* The button number to select.
* @raise RuntimeException
* The button number is invalid.
*/
public void select(int button) {
// TODO: Implement this method.
for (int btn : radioButtons.)
radioButtons.put(button, true);
}
/**
* Returns whether a given button is selected.
* @param button
* The button number to check.
* @raise RuntimeException
* The button number is invalid.
*/
public boolean isSelected(int button) {
// TODO: Implement this method.
return false;
}
// TODO: Add attributes and helper methods as needed.
}