Click here to Skip to main content
15,894,539 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to make this code to in form design pattern of "Interpreter pattern". How I can do?
---------------------------------------------------------------------------------------------------------------------------


package test;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Calculator extends JPanel implements ActionListener {
private JTextField display = new JTextField("0");

private String buttonText = "789/456*123-0.=+";

private double result = 0;

private String operator = "=";

private boolean calculating = true;

public Calculator() {
setLayout(new BorderLayout());

display.setEditable(false);
add(display, "North");

JPanel p = new JPanel();
p.setLayout(new GridLayout(4, 4));

for (int i = 0; i < buttonText.length(); i++) {
JButton b = new JButton(buttonText.substring(i, i + 1));
p.add(b);
b.addActionListener(this);

}
add(p, "Center");
}

public void actionPerformed(ActionEvent evt) {
String cmd = evt.getActionCommand();
if ('0' <= cmd.charAt(0) && cmd.charAt(0) <= '9' || cmd.equals(".")) {
if (calculating)
display.setText(cmd);
else
display.setText(display.getText() + cmd);
calculating = false;
} else {
if (calculating) {
if (cmd.equals("-")) {
display.setText(cmd);
calculating = false;
} else
operator = cmd;
} else {
double x = Double.parseDouble(display.getText());
calculate(x);
operator = cmd;
calculating = true;
}
}
}

private void calculate(double n) {
if (operator.equals("+"))
result += n;
else if (operator.equals("-"))
result -= n;
else if (operator.equals("*"))
result *= n;
else if (operator.equals("/"))
result /= n;
else if (operator.equals("="))
result = n;
display.setText("" + result);
}

public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("Calculator");
frame.setSize(200, 200);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

Container contentPane = frame.getContentPane();
contentPane.add(new Calculator());
frame.show();
}
}
Posted
Comments
OriginalGriff 5-Dec-14 3:34am    
BTW: I was going to improve your question by formatting the code with pre tags - but there is no indentation there in the first place!
Indent your code: and post it here indented and formatted. It makes it a lot easier for us to read, and that means a better answer. Except when it's your homework and you haven't done anything yourself, of course
Sergey Alexandrovich Kryukov 5-Dec-14 3:35am    
The question makes no sense, as all question where one wants to use some pattern for the sake of using a pattern, not explaining what is the goal.
—SA
Member 11287299 5-Dec-14 4:15am    
I send this code to my professor. But, he return it to me. he say it not correct it is not design pattern. ????

1 solution

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
 
Share this answer
 
Comments
Member 11287299 5-Dec-14 4:07am    
I don't know. How I can separate class for make to Interpreter pattern?
OriginalGriff 5-Dec-14 6:25am    
You wrote the code, so you know what it does and how it does it.
You know the design pattern.
So what is the problem?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900