Click here to Skip to main content
15,902,198 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i create that code for a calculator but it didn't compile with command prompt. i code this in notepad after i copy this to net beans class and check errors. so i find there are duplicate class with same name. but there isn't such a thing. And the other thing is calculates are didn't work correctly. So can you fix me this problem please? i will copy my code in here.
Java
class calc implements ActionListener{
    public void actionPerformed(ActionEvent e){
    }
        calc(){

        JFrame F = new JFrame();
	JTextField display = new JTextField(30);
 	JButton b1 = new JButton("1");
 	JButton b2 = new JButton("2");
 	JButton b3 = new JButton("3");
 	JButton b4 = new JButton("4");
 	JButton b5 = new JButton("5");
	JButton b6 = new JButton("6");
	JButton b7 = new JButton("7");
	JButton b8 = new JButton("8");
	JButton b9 = new JButton("9");
	JButton b0 = new JButton("0");
	JButton bEqual = new JButton("=");
	JButton bAddition = new JButton("+");
	JButton bSubstraction = new JButton("-");
	JButton bDivision = new JButton("/");
	JButton bDot = new JButton(".");
	JButton bMultiplication = new JButton("*");
	JButton bOneX = new JButton("1/x");
	JButton bModulus = new JButton("%");
	JButton bCE = new JButton("CE");
	JButton bC = new JButton("C");
	JButton bMC = new JButton("MC");
	JButton bMR = new JButton("MR");
	JButton bMS = new JButton("MS");
	JButton bMadd = new JButton("M+");
	JButton bMsub = new JButton("M-");

 	JPanel p1 = new JPanel();
	JPanel p2 = new JPanel();
	
	p1.add(display);

	p2.add(b1);
	p2.add(b2);  
	p2.add(b3);
	p2.add(b4);
	p2.add(b5);
	p2.add(b6);
	p2.add(b7);
	p2.add(b8);
	p2.add(b9);
	p2.add(b0);
	p2.add(bCE);
	p2.add(bC);
	p2.add(bMC);
	p2.add(bMR);
	p2.add(bMS);
	p2.add(bMadd);
	p2.add(bMsub);
	p2.add(bAddition );
	p2.add(bSubstraction);
	p2.add(bDivision);
	p2.add(bDot);
	p2.add(bOneX);
	p2.add(bMultiplication);
	p2.add(bModulus);
	p2.add(bEqual);

	F.add(p1,BorderLayout.NORTH);
	F.add(p2,BorderLayout.CENTER);

	GridLayout g1 =new GridLayout(5,5);
	GridLayout g2 =new GridLayout(1,1);
	
        p1.setLayout(g2);
	p2.setLayout(g1);
	
	F.setTitle("Calculator_YESHANI");
	F.setLocation(370,200);
	F.setSize(340, 442);

	F.setResizable(false);

        String out = display.getText();
        
        String cl1 = null,cl2 = null,cl3 = null,cl4 = null,cl5 = null,cl6 = null,cl7 = null;
        
        if (e.getsource() == bCE) {
            display.setText("");
            if (e.source().equals(b1)) {
                display.setText(out + e.getActionCommand());

            } else if (e.getSource().equals(b2)) {
                display.setText(out + e.getActionCommand());
            } else if (e.getSource().equals(b3)) {
                display.setText(out + e.getActionCommand());
            } else if (e.getSource().equals(b4)) {
                display.setText(out + e.getActionCommand());
            } else if (e.getSource().equals(b5)) {
                display.setText(out + e.getActionCommand());
            } else if (e.getSource().equals(b6)) {
                display.setText(out + e.getActionCommand());
            } else if (e.getSource().equals(b7)) {
                display.setText(out + e.getActionCommand());
            } else if (e.getSource().equals(b8)) {
                display.setText(out + e.getActionCommand());
            } else if (e.getSource().equals(b9)) {
                display.setText(out + e.getActionCommand());
            } else if (e.getSource().equals(b0));
            display.setText(out + e.getActionCommand());

            if (e.getSource().equals(bCE)) {
                display.setText(out + e.getActionCommand());
            } else if (e.getSource() == (bAddition)) {
                cl1 = out;
                display.setText(null);
            } else if (e.getSource() == (bSubstraction)) {
                cl2 = out;
                display.setText(null);
            } else if (e.getSource() == (bMultiplication)) {
                cl3 = out;
                display.setText(null);
            } else if (e.getSource() == (bDivision)) {
                cl4 = out;
                display.setText(null);
            } else if (e.getSource() == (bModulus)) {
                cl5 = out;
                display.setText(null);
            } else if (e.getSource() == (bOneX)) {
                cl6 = out;
                display.setText(null);
            } else if (e.getSource() == (bC)) {
                cl7 = out;
                display.setText(null);
            } else if (bEqual.equals(e.getSource())) {

                if (cl1 != null) {
                    String value = String.valueOf(Double.parseDouble(cl1) + Double.parseDouble(display.getText()));
                    display.setText(value);
                    cl1 = null;
                } else if (cl2 != null) {
                    String value = String.valueOf(Double.parseDouble(cl2) - Double.parseDouble(display.getText()));
                    display.setText(value);
                    cl2 = null;
                } else if (cl3 != null) {
                    String value = String.valueOf(Double.parseDouble(cl3) * Double.parseDouble(display.getText()));
                    display.setText(value);
                    cl3 = null;
                } else if (cl4 != null) {
                    String value = String.valueOf(Double.parseDouble(cl4) / Double.parseDouble(display.getText()));
                    display.setText(value);
                    cl4 = null;
                } else if (cl5 != null) {
                    String value = String.valueOf(Double.parseDouble(cl5) * Double.parseDouble(display.getText()) / 100);
                    display.setText(value);
                    cl5 = null;
                } else if (cl6 != null) {
                    String value = String.valueOf(1 / Double.parseDouble(cl6));
                    display.setText(value);
                    cl6 = null;
                } else if (cl7 != null) {
                    String value = String.valueOf(Math.sqrt(Double.parseDouble(cl7)));
                    display.setText(value);
                  cl7 = null;

                }
            }
            b1.addActionListener(this);
            b2.addActionListener(this);
            b3.addActionListener(this);
            b4.addActionListener(this);
            b5.addActionListener(this);
            b6.addActionListener(this);
            b7.addActionListener(this);
            b8.addActionListener(this);
            b9.addActionListener(this);
            b0.addActionListener(this);
            bEqual.addActionListener(this);
            bCE.addActionListener(this);
            bModulus.addActionListener(this);
            bOneX.addActionListener(this);
            bAddition.addActionListener(this);
            bDot.addActionListener(this);
            bMultiplication.addActionListener(this);
            bDivision.addActionListener(this);
            bC.addActionListener(this);
            bSubstraction.addActionListener(this);
         
            F.setDefaultCloseOperation(EXIT_ON_CLOSE);
            p2.setBackground(Color.PINK);
            p1.setBackground(Color.BLUE);
            F.setVisible(true);
        }
    }
    public static void main(String args[]) {

        calc c = new calc();
}
    }

[Edit]Code block formatting added and tag updated (Javascript -> Java)[/Edit]
Posted
Updated 9-Jul-13 5:59am
v2
Comments
Sergey Alexandrovich Kryukov 9-Jul-13 12:07pm    
To start with, this has little to do with programming. As soon as you repeat JButton b1 = new JButton("..."); or p2.add(...), you totally defeat the purpose of programming. Did you hear of abstraction, reuse and Don't-Repeat-Yourself principle? And I would pick a better task than a calculator with buttons; who would need such a thing?
—SA
yeshani 9-Jul-13 12:16pm    
yaa give me
yeshani 9-Jul-13 12:20pm    
please give me correct code without errors
Sergey Alexandrovich Kryukov 9-Jul-13 13:33pm    
Are you serious?
—SA

1 solution

never - NEVER!!! - did I say never? - never ever!!

Java
JFrame F = new JFrame();


Variables are always starting lower case. The compiler probably got confused.

SA's demand for reuse:

Java
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
...

p2.add(b1);
p2.add(b2);  
p2.add(b3);

.....


convert to:

Java
ps.add(createButton("1"));

...


private JButton createButton(String strLabel){

// code to create button with actionlistener and return that
}
 
Share this answer
 

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