First , your calculator should be fully functional. This means that it should be
ID: 3861309 • Letter: F
Question
First, your calculator should be fully functional. This means that it should be able to accomplish all arithmetic operations on any set of numbers. The number buttons should be fully active and implemented. The user cannot edit the display what so ever.
If the input is 3 then + then 5 then = and then + then 4. Your calculator should display 12. This means your calculator should be able to append other operations and operands on results.
Next, your calculator should be able to convert between binary and decimal. You are only required to do conversation and not binary arithmetic. In order to do this your will need to add two radio buttons to your form that give the user the option to pick if he/ she want to be in Decimal (default - startup) or binary mode.
This is an example of how your controls should / may look like
The binary mode is used solely to convert a number from Decimal to Binary and visa-versa. Example: if the user enters the number 10 in decimal mode and then selects the binary mode then 1010 should be displayed. If the user enters 101 while in binary mode and then selects the decimal mode then the number 5 should be displayed. Make sure you disable all non-necessary control elements when in binary mode. You need to write a method for converting binary into decimal and another method to convert from decimal to binary.
Finally. Your calculator should respond to keyboard key press (just like the calculator for windows). So if someone presses the number 1 on the keyboard; this will mean that the button with number 1 has been clicked and the display will show the number 1. If the * sign is pressed on the keyboard; it should act as if the * button is clicked and if the enter button is pressed on the keyboard then it acts just like if the “=” is clicked.
Hereis a link from MSDN that provides with the means on how to do this. Read it carefully.
Here is the grading schema for this HW
Fully functional calculator with GUI controls 50%
Binary / Decimal Mode 35%
Keyboard input 15%
Explanation / Answer
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Frame extends JFrame {
private double tempNumbers1 = 0;
private double tempNumbers2 = 0;
private byte function = -1;
private JTextField resultJText;
public Frame() {
JButton[] numberButtons = new JButton[10];
for ( int i = 9; i >= 0; i--) {
numberButtons[i] = new JButton(Integer.toString(i));
}
JButton enterButton = new JButton("Enter");
JButton cButton = new JButton("C");
JButton multiplyButton = new JButton("*");
JButton divideButton = new JButton("/");
JButton addButton = new JButton("+");
JButton substractButton = new JButton("-");
resultJText = new JTextField();
resultJText.setPreferredSize(new Dimension(160, 20));
resultJText.setBackground(Color.WHITE);
resultJText.setEnabled(false);
resultJText.setHorizontalAlignment(4);
resultJText.setDisabledTextColor(Color.BLACK);
JPanel motherPanel = new JPanel();
motherPanel.setLayout(new BoxLayout(motherPanel, BoxLayout.Y_AXIS));
JPanel textPanel = new JPanel();
textPanel.setPreferredSize(new Dimension(160, 20));
textPanel.add(resultJText);
JPanel numberButtonsPanel = new JPanel();
numberButtonsPanel.setPreferredSize(new Dimension(160, 100));
for(int i = 9; i>=0; i--) {
numberButtonsPanel.add(numberButtons[i]);
}
JPanel functionButtonPanel = new JPanel();
functionButtonPanel.setPreferredSize(new Dimension(160, 35));
functionButtonPanel.add(enterButton);
functionButtonPanel.add(cButton);
functionButtonPanel.add(multiplyButton);
functionButtonPanel.add(divideButton);
functionButtonPanel.add(addButton);
functionButtonPanel.add(substractButton);
numberButtonsAction[] numberButtonActions = new numberButtonsAction[10];
for ( int i = 0; i < 10; i++ ) {
numberButtonActions[i] = new numberButtonsAction(numberButtons[i]);
numberButtons[i].addActionListener(numberButtonActions[i]);
}
EnterButton enter = new EnterButton();
enterButton.addActionListener(enter);
CButton c = new CButton();
cButton.addActionListener(c);
MultiplyButton multiply = new MultiplyButton();
multiplyButton.addActionListener(multiply);
DivideButton divide = new DivideButton();
divideButton.addActionListener(divide);
AddButton add = new AddButton();
addButton.addActionListener(add);
SubtractButton subtract = new SubtractButton();
substractButton.addActionListener(subtract);
motherPanel.add(textPanel);
motherPanel.add(numberButtonsPanel);
motherPanel.add(functionButtonPanel);
add(motherPanel);
setTitle("ButtonTest");
setSize(180, 290);
setLocationByPlatform(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
private class numberButtonsAction implements ActionListener {
private String c;
public numberButtonsAction(JButton a) {
this.c = a.getText();
}
public void actionPerformed(ActionEvent e) {
if (!resultJText.getText().equals("0.0")) {
resultJText.setText(resultJText.getText() + c);
} else {
resultJText.setText("");
actionPerformed(e);
}
}
}
private class EnterButton implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
tempNumbers2 = Double.parseDouble(resultJText.getText());
if (function == 0) {
resultJText.setText(Double.toString((Math.round((tempNumbers1 / tempNumbers2) * 100)) / 100));
} else if (function == 1) {
resultJText.setText(Double.toString(tempNumbers1 * tempNumbers2));
} else if (function == 2) {
resultJText.setText(Double.toString(tempNumbers2 + tempNumbers1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.