guys help me if these code is correct

import javax.swing.*;
import java.awt.event.*;
public class SampleCalc extends JFrame implements ActionListener{

	
	JTextField txtNum1 = new JTextField();
	JTextField txtNum2 = new JTextField();
	
	JButton btnAdd = new JButton("+");
	JButton btnSub = new JButton("-");
	JButton btnMul = new JButton("*");
	JButton btnDiv = new JButton("/");
	
	
	JPanel pane = new JPanel(); 

public static void main(String args[]){
	SampleCalc calc = new SampleCalc();
	calc.setSize(300,300);
	calc.setLocation(300,300);
	calc.setResizable(false);
	calc.setVisible(true);
	calc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
    public SampleCalc() {
    	super("Calculator");
    	
    	pane.setLayout(null);
    	
    	txtNum1.setBounds(50,20,80,20);
    	txtNum2.setBounds(160,20,80,20);
    	btnAdd.setBounds(50,60,80,20);
    	btnSub.setBounds(160,60,80,20);
    	btnMul.setBounds(50,100,80,20);
    	btnDiv.setBounds(160,100,80,20);
    	
    	
    	pane.add(txtNum1);
    	pane.add(txtNum2);
    	pane.add(btnAdd);
    	pane.add(btnSub);
    	pane.add(btnMul);
    	pane.add(btnDiv);
    	
    	btnAdd.addActionListener(this);
    	btnSub.addActionListener(this);
    	btnMul.addActionListener(this);
    	btnDiv.addActionListener(this);
    	setContentPane(pane);
    	
    }
    public void actionPerformed(ActionEvent e){
    	
    	String str1,str2;
    	str1 = txtNum1.getText();
    	str2 = txtNum2.getText();
    	
    	if((str1.length() == 0)||(str2.length() == 0)){
    		JOptionPane.showMessageDialog(null,"You must input two numbers...");
    	}
    	else{
    		double num1 = Double.parseDouble(txtNum1.getText());
    		double num2 = Double.parseDouble(txtNum2.getText());
    	
	    	if(e.getSource() == btnAdd){
	    		double sum;
	    		sum = num1 + num2;
	    	JOptionPane.showMessageDialog(null,"The Sum is: " + sum);
	    	}
	    	if(e.getSource() == btnSub){
	    		double diff;
	    		diff = num1 - num2;
	    	JOptionPane.showMessageDialog(null,"The Difference is: " + diff);
	    	}
	    	if(e.getSource() == btnMul){
	    		double pro;
	    		pro = num1 * num2;
	    	JOptionPane.showMessageDialog(null,"The Product is: " + pro);
	    	}
	    	if(e.getSource() == btnDiv){
	    			double quo;
	    			if(num2==0){
	    				JOptionPane.showMessageDialog(null,"Cannot be divided by zero");
	    			}
	    			else{
	    			quo = num1 / num2;
	    	        JOptionPane.showMessageDialog(null,"The Quotient is: " + quo);
	    			}
	            }
    	}	
    	
      }
    }

Recommended Answers

All 3 Replies

Have you tried running your code?? Be more specific with your questions. Try running it and if an error pops then you can get back to us regarding that error. If not then your code is good.

This are the errors when i run your program. By the way the i removed public by your class SampleCalc, because otherwise you have to but the code in a other document.class

java.lang.NoClassDefFoundError: calculator/Main
Caused by: java.lang.ClassNotFoundException: calculator.Main
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: calculator.Main. Program will exit.
Exception in thread "main" Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

I hope this will help you.

Next time, post your error message. Don't just plain asking for help and dump your code on us... Not nice...

OK, your code is fine. I could get it compiled and run without a problem (using javac command).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.