Challenge

Project

package scientific_Calculator;

import java.awt.EventQueue;

import javax.swing.JFrame;
import java.awt.Color;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;

public class calculator {

	private JFrame frame;
	private JTextField textField;
	
	double first;
	double second;
	double result;
	String operation;
	String answer;
	private final ButtonGroup buttonGroup = new ButtonGroup();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					calculator window = new calculator();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public calculator() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.getContentPane().setFont(new Font("Verdana", Font.PLAIN, 15));
		frame.setFont(null);
		frame.getContentPane().setBackground(new Color(240, 240, 240));
		frame.getContentPane().setLayout(null);
		
		JLabel lblNewLabel = new JLabel("Scientific Calculator");
		lblNewLabel.setFont(new Font("Sitka Text", Font.BOLD, 15));
		lblNewLabel.setBounds(119, 10, 221, 36);
		frame.getContentPane().add(lblNewLabel);
		
		textField = new JTextField();
		textField.setBounds(77, 40, 373, 94);
		frame.getContentPane().add(textField);
		textField.setColumns(10);
		
		JButton btnNewButton = new JButton("\u221A");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				double a=Math.sqrt(Double.parseDouble(textField.getText()));
				textField.setText("");
				textField.setText(textField.getText()+a);
			}
		});
		btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 20));
		btnNewButton.setBounds(20, 182, 85, 64);
		frame.getContentPane().add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("1/x");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			double a=1/(Double.parseDouble(textField.getText()));
				textField.setText("");
				textField.setText(textField.getText()+a);
			}
		});
		btnNewButton_1.setFont(new Font("Tahoma", Font.PLAIN, 20));
		btnNewButton_1.setBounds(20, 244, 85, 64);
		frame.getContentPane().add(btnNewButton_1);
		
		JButton btnNewButton_1_1_1 = new JButton("X^3");
		btnNewButton_1_1_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				double a=(Double.parseDouble(textField.getText()));
				a=a*a*a;
				textField.setText("");
				textField.setText(textField.getText()+a);
			}
		});
		btnNewButton_1_1_1.setFont(new Font("Tahoma", Font.PLAIN, 20));
		btnNewButton_1_1_1.setBounds(20, 415, 85, 64);
		frame.getContentPane().add(btnNewButton_1_1_1);
		
		JButton btnNewButton_1_1_1_1 = new JButton("X^2");
		btnNewButton_1_1_1_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				double a=(Double.parseDouble(textField.getText()));
				a=a*a;
				textField.setText("");
				textField.setText(textField.getText()+a);
			}
		});
		btnNewButton_1_1_1_1.setFont(new Font("Tahoma", Font.PLAIN, 20));
		btnNewButton_1_1_1_1.setBounds(20, 468, 85, 64);
		frame.getContentPane().add(btnNewButton_1_1_1_1);
		
		JButton btnNewButton_1_1_1_1_1 = new JButton("n!");
		btnNewButton_1_1_1_1_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				double a=Double.parseDouble(textField.getText());
				double fact=1;
				while(a!=0) {
					a--;
			}
				textField.setText("");
				textField.setText(textField.getText()+fact);
			}
		});
		btnNewButton_1_1_1_1_1.setFont(new Font("Tahoma", Font.PLAIN, 20));
		btnNewButton_1_1_1_1_1.setBounds(20, 527, 85, 64);
		frame.getContentPane().add(btnNewButton_1_1_1_1_1);
		
		JButton btnPlusMinus = new JButton("+/-");
		btnPlusMinus.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				double a=Double.parseDouble(String.valueOf(textField.getText()));
				a=a*(-1);
				textField.setText(String.valueOf(a));
				
			}
		});
		btnPlusMinus.setFont(new Font("Tahoma", Font.PLAIN, 20));
		btnPlusMinus.setBounds(20, 589, 85, 64);
		frame.getContentPane().add(btnPlusMinus);
		
		JButton btnNewButton_2 = new JButton("e^X");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				double a=Math.exp(Double.parseDouble(textField.getText()));
				textField.setText("");
				textField.setText(textField.getText()+a);
			}
		});
		btnNewButton_2.setFont(new Font("Tahoma", Font.PLAIN, 20));
		btnNewButton_2.setBounds(101, 182, 85, 64);
		frame.getContentPane().add(btnNewButton_2);
		
		JButton btnNewButton_1_2 = new JButton("Log");
		btnNewButton_1_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				double a=Math.log(Double.parseDouble(textField.getText()));
				textField.setText("");
				textField.setText(textField.getText()+a);
			}
		});
		btnNewButton_1_2.setFont(new Font("Tahoma", Font.PLAIN, 20));
		btnNewButton_1_2.setBounds(101, 244, 85, 64);
		frame.getContentPane().add(btnNewButton_1_2);
		
		JButton btn7 = new JButton("7");
		btn7.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String number = textField.getText()+btn7.getText();
				textField.setText(number);
			}
		});
		btn7.setFont(new Font("Tahoma", Font.PLAIN, 20));
		btn7.setBounds(101, 361, 85, 64);
		frame.getContentPane().add(btn7);
		
		JButton btn4 = new JButton("4");
		btn4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String number = textField.getText()+btn4.getText();
				textField.setText(number);
			}
		});
		btn4.setFont(new Font("Tahoma", Font.PLAIN, 20));
		btn4.setBounds(101, 415, 85, 64);
		frame.getContentPane().add(btn4);
		
		JButton btn1 = new JButton("1");
		btn1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String number = textField.getText()+btn1.getText();
				textField.setText(number);
			}
		});
		btn1.setFont(new Font("Tahoma", Font.PLAIN, 20));
		btn1.setBounds(101, 468, 85, 64);
		frame.getContentPane().add(btn1);
		
		JButton btn0 = new JButton("0");
		btn0.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String number = textField.getText()+btn0.getText();
				textField.setText(number);
			}
		});
		btn0.setFont(new Font("Tahoma", Font.PLAIN, 20));
		btn0.setBounds(101, 529, 179, 124);
		frame.getContentPane().add(btn0);
		
		JButton btnNewButton_1_2_1_5 = new JButton("sin");
		btnNewButton_1_2_1_5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				double a=Math.sin(Double.parseDouble(textField.getText()));
				textField.setText("");
				textField.setText(textField.getText()+a);
			}
		});
		btnNewButton_1_2_1_5.setFont(new Font("Tahoma", Font.PLAIN, 20));
		btnNewButton_1_2_1_5.setBounds(188, 182, 92, 64);
		frame.getContentPane().add(btnNewButton_1_2_1_5);
		
		JButton btnBackSpace = new JButton("\uF0E7");
		btnBackSpace.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String backSpace=null;
				
				if(textField.getText().length()>0)
				{
					StringBuilder str=new StringBuilder(textField.getText());
					str.deleteCharAt(textField.getText().length()-1);
					backSpace=str.toString();
					textField.setText(backSpace);
					
				}
				
			}
		});
		btnBackSpace.setFont(new Font("Windings", Font.PLAIN, 20));
		btnBackSpace.setBounds(277, 306, 85, 59);
		frame.getContentPane().add(btnBackSpace);
		
		JButton btn8 = new JButton("8");
		btn8.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String number = textField.getText()+btn8.getText();
				textField.setText(number);
			}
		});
		btn8.setFont(new Font("Tahoma", Font.PLAIN, 20));
		btn8.setBounds(185, 362, 95, 62);
		frame.getContentPane().add(btn8);
		
		JButton btn9 = new JButton("9");
		btn9.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String number = textField.getText()+btn9.getText();
				textField.setText(number);
			}
		});
		btn9.setFont(new Font("Tahoma", Font.PLAIN, 20));
		btn9.setBounds(277, 361, 85, 64);
		frame.getContentPane().add(btn9);
		
		JButton btn5 = new JButton("5");
		btn5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String number = textField.getText()+btn5.getText();
				textField.setText(number);
			}
		});
		btn5.setFont(new Font("Tahoma", Font.PLAIN, 20));
		btn5.setBounds(185, 415, 95, 64);
		frame.getContentPane().add(btn5);
		
		JButton btn6 = new JButton("6");
		btn6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String number = textField.getText()+btn6.getText();
				textField.setText(number);
			}
		});
		btn6.setFont(new Font("Tahoma", Font.PLAIN, 20));
		btn6.setBounds(277, 415, 85, 64);
		frame.getContentPane().add(btn6);
		
		JButton btn2 = new JButton("2");
		btn2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String number = textField.getText()+btn2.getText();
				textField.setText(number);
			}
		});
		btn2.setFont(new Font("Tahoma", Font.PLAIN, 20));
		btn2.setBounds(185, 470, 92, 64);
		frame.getContentPane().add(btn2);
		
		JButton btn3 = new JButton("3");
		btn3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String number = textField.getText()+btn3.getText();
				textField.setText(number);
			}
		});
		btn3.setFont(new Font("Tahoma", Font.PLAIN, 20));
		btn3.setBounds(277, 468, 85, 64);
		frame.getContentPane().add(btn3);
		
		JButton btnDot = new JButton(".");
		btnDot.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String number = textField.getText()+btnDot.getText();
				textField.setText(number);
			}
		});
		btnDot.setFont(new Font("Tahoma", Font.PLAIN, 20));
		btnDot.setBounds(277, 527, 85, 126);
		frame.getContentPane().add(btnDot);
		
		JButton btnNewButton_1_2_1_6_1 = new JButton("tan");
		btnNewButton_1_2_1_6_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				double a=Math.tan(Double.parseDouble(textField.getText()));
				textField.setText("");
				textField.setText(textField.getText()+a);
			}
		});
		btnNewButton_1_2_1_6_1.setFont(new Font("Tahoma", Font.PLAIN, 20));
		btnNewButton_1_2_1_6_1.setBounds(365, 182, 85, 64);
		frame.getContentPane().add(btnNewButton_1_2_1_6_1);
		
		JButton btnEqual = new JButton("=");
		btnEqual.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				second=Double.parseDouble(textField.getText());
				if(operation=="+")
				{
					result=first+second;
					answer=String.format("%2f", result);
					textField.setText(answer);
				}
				else if(operation=="-")
				{
					result=first-second;
					answer=String.format("%2f", result);
					textField.setText(answer);
				}
				else if(operation=="*")
				{
					result=first*second;
					answer=String.format("%2f", result);
					textField.setText(answer);
				}
				else if(operation=="/")
				{
					result=first/second;
					answer=String.format("%2f", result);
					textField.setText(answer);
				}
				else if(operation=="%")
				{
					result=first%second;
					answer=String.format("%2f", result);
					textField.setText(answer);
				}
				else if(operation=="x^y")
				{
					double resultt=1;
					for(int i=0;i<second;i++)
					{
						resultt=first*resultt;
					}
					
					answer=String.format("%2f", result);
					textField.setText(answer);
				}
			}
		});
		btnEqual.setFont(new Font("Tahoma", Font.PLAIN, 20));
		btnEqual.setBounds(365, 533, 85, 120);
		frame.getContentPane().add(btnEqual);
		
		JButton btnEqual_1 = new JButton("/");
		btnEqual_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				first=Double.parseDouble(textField.getText());
			    textField.setText("");
			    operation="/";
			}
		});
		btnEqual_1.setFont(new Font("Tahoma", Font.PLAIN, 20));
		btnEqual_1.setBounds(365, 473, 85, 59);
		frame.getContentPane().add(btnEqual_1);
		
		JButton btnEqual_2 = new JButton("*");
		btnEqual_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				first=Double.parseDouble(textField.getText());
			    textField.setText("");
			    operation="*";
			}
		});
		btnEqual_2.setFont(new Font("Tahoma", Font.PLAIN, 20));
		btnEqual_2.setBounds(365, 415, 85, 64);
		frame.getContentPane().add(btnEqual_2);
		
		JButton btnEqual_3 = new JButton("-");
		btnEqual_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				first=Double.parseDouble(textField.getText());
			    textField.setText("");
			    operation="-";
				
			}
		});
		btnEqual_3.setFont(new Font("Tahoma", Font.PLAIN, 20));
		btnEqual_3.setBounds(365, 361, 85, 64);
		frame.getContentPane().add(btnEqual_3);
		
		JButton btnEqual_4 = new JButton("+");
		btnEqual_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				first=Double.parseDouble(textField.getText());
			    textField.setText("");
			    operation="+";
				
			}
		});
		btnEqual_4.setFont(new Font("Tahoma", Font.PLAIN, 20));
		btnEqual_4.setBounds(365, 307, 85, 64);
		frame.getContentPane().add(btnEqual_4);
		
		JButton btnC = new JButton("C");
		btnC.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				textField.setText(null);
			}
		});
		btnC.setFont(new Font("Sitka Text", Font.PLAIN, 20));
		btnC.setBounds(185, 308, 92, 57);
		frame.getContentPane().add(btnC);
		
		JButton btnBackSpace_2 = new JButton("%");
		btnBackSpace_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				first=Double.parseDouble(textField.getText());
			    textField.setText("");
			    operation="%";
				
			}
		});
		btnBackSpace_2.setFont(new Font("Sitka Text", Font.PLAIN, 20));
		btnBackSpace_2.setBounds(101, 308, 85, 64);
		frame.getContentPane().add(btnBackSpace_2);
		
		JButton btnBackSpace_3 = new JButton("B");
		btnBackSpace_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			}
		});
		btnBackSpace_3.setFont(new Font("Sitka Text", Font.PLAIN, 20));
		btnBackSpace_3.setBounds(20, 306, 85, 64);
		frame.getContentPane().add(btnBackSpace_3);
		
		JButton btnNewButton_1_2_1_5_1 = new JButton("sinh");
		btnNewButton_1_2_1_5_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				double a=Math.sinh(Double.parseDouble(textField.getText()));
				textField.setText("");
				textField.setText(textField.getText()+a);
			}
		});
		btnNewButton_1_2_1_5_1.setFont(new Font("Tahoma", Font.PLAIN, 20));
		btnNewButton_1_2_1_5_1.setBounds(188, 244, 92, 64);
		frame.getContentPane().add(btnNewButton_1_2_1_5_1);
		
		JButton btnNewButton_1_2_1_5_2 = new JButton("cos");
		btnNewButton_1_2_1_5_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				double a=Math.cos(Double.parseDouble(textField.getText()));
				textField.setText("");
				textField.setText(textField.getText()+a);
			}
		});
		btnNewButton_1_2_1_5_2.setFont(new Font("Tahoma", Font.PLAIN, 20));
		btnNewButton_1_2_1_5_2.setBounds(277, 182, 85, 64);
		frame.getContentPane().add(btnNewButton_1_2_1_5_2);
		
		JButton btnNewButton_1_2_1_5_3 = new JButton("cosh");
		btnNewButton_1_2_1_5_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				double a=Math.tanh(Double.parseDouble(textField.getText()));
				textField.setText("");
				textField.setText(textField.getText()+a);
			}
		});
		btnNewButton_1_2_1_5_3.setFont(new Font("Tahoma", Font.PLAIN, 20));
		btnNewButton_1_2_1_5_3.setBounds(277, 244, 85, 64);
		frame.getContentPane().add(btnNewButton_1_2_1_5_3);
		
		JButton btnNewButton_1_2_1_5_4 = new JButton("tanh");
		btnNewButton_1_2_1_5_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		btnNewButton_1_2_1_5_4.setFont(new Font("Tahoma", Font.PLAIN, 20));
		btnNewButton_1_2_1_5_4.setBounds(365, 244, 85, 64);
		frame.getContentPane().add(btnNewButton_1_2_1_5_4);
		
		JRadioButton rdbtnNewRadioButton = new JRadioButton("ON");
		buttonGroup.add(rdbtnNewRadioButton);
		rdbtnNewRadioButton.setFont(new Font("Tahoma", Font.BOLD, 18));
		rdbtnNewRadioButton.setBounds(49, 140, 103, 31);
		frame.getContentPane().add(rdbtnNewRadioButton);
		
		JRadioButton rdbtnOff = new JRadioButton("OFF");
		rdbtnOff.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btn1.setEnabled(false);
				btn2.setEnabled(false);
				btn3.setEnabled(false);
				btn4.setEnabled(false);
				btn5.setEnabled(false);
				btn6.setEnabled(false);
				btn7.setEnabled(false);
				btn8.setEnabled(false);
				btn9.setEnabled(false);
		
			}
		});
		buttonGroup.add(rdbtnOff);
		rdbtnOff.setFont(new Font("Tahoma", Font.BOLD, 18));
		rdbtnOff.setBounds(333, 145, 103, 31);
		frame.getContentPane().add(rdbtnOff);
		
		JButton btnNewButton_1_1_1_1_2 = new JButton("x^y");
		btnNewButton_1_1_1_1_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				first=Double.parseDouble(textField.getText());
			    textField.setText("");
			    operation="x^y";
			}
		});
		btnNewButton_1_1_1_1_2.setFont(new Font("Tahoma", Font.PLAIN, 20));
		btnNewButton_1_1_1_1_2.setBounds(20, 368, 85, 50);
		frame.getContentPane().add(btnNewButton_1_1_1_1_2);
		frame.setBounds(100, 100, 484, 707);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.