We\'ve set up a calculator GUI that converts the decimal answer to binary. We ar
ID: 3692566 • Letter: W
Question
We've set up a calculator GUI that converts the decimal answer to binary. We are supposed to be able to toggle the bits so that if we click on a "0", it changes to "1" and vice versa. However, we cannot write the setMouseClicked method to correctly change the label at the correct array index. Can we get some assisstance?
Label currentBit;
public void toggleBits() {
for (int i=0; i<32; i++) {
currentBit = bitLabel[i];
bitLabel[i].setOnMouseClicked(new EventHandler<MouseEvent>() {
public void handle(MouseEvent me) {
toggle();
}
});
}
} //toggleBits
public void toggle() {
if (currentBit.getText() == "0") {
currentBit.setText("1");
}
else if (currentBit.getText() == "1") {
currentBit.setText("0");
}
} //toggle
Explanation / Answer
bitLabel[i].setOnMouseClicked(new EventHandler DecimalToBinary ()
public class DecimalToBinary {
public String toBinary(int n) {
if (n == 0) {
return "0";
}
String binary = "";
while (n > 0) {
int rem = n % 2;
binary = rem + binary;
n = n / 2;
}
return binary;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.