Saturday, August 27, 2011

Arithmetic Operation using JOptionPane

Design Free Website with Free Internet Marketing Tools

Program Description:

The program below is a simple java code arithmetic operation using JButton as arithmetic operator selector and JOptionPane as user input. It is also capable of catching number format error like putting non-integer numbers.

Output:



Code:

/**
 * File: arithmeticOperationJOptionPane.java
 * Tiltle: Arithmetic Operation using JOptionPane
 * Author: http://java-program-sample.blogspot.com/
 */

//Java Core Package
import javax.swing.*;
//Java Extension Package
import java.awt.*;
import java.awt.event.*;

public class arithmeticOperationJOptionPane extends JFrame {
	
	//Initializing JButton and String operation as the label of each JButton
	private JButton buttons[];
	private String operation[] = {"Addition [+]","Subtraction [-]","Multiplication [x]","Division [/]"};

	//Setting up GUI
    public arithmeticOperationJOptionPane() {
    	
    	//Setting up the Title of the Window
    	super("Arithmetic Operation using JOptionPane");

    	//Set Size of the Window (WIDTH, HEIGHT)
    	setSize(320,120);

    	//Exit Property of the Window
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	
    	//Constructing JButton with a array size of 4
    	buttons = new JButton[4];
    	
    	//Setting up the container ready for the components to be added.
    	Container pane = getContentPane();
    	setContentPane(pane);

    	//Setting up the container layout
    	GridLayout grid = new GridLayout(2,2);
    	pane.setLayout(grid);
    	
    	//Constructing all 4 JButtons using "for loop" and add them in the container
    	for(int count=0; count<buttons.length; count++) {
    		buttons[count] = new JButton(operation[count]);
    		pane.add(buttons[count]);
    	}
		
		//Implemeting Even-Listener on JButton button[0] which is addition
		buttons[0].addActionListener(
		new ActionListener() {
			
			//Handle JButton event if it is clicked
			public void actionPerformed(ActionEvent event) {
				try { //fetch an error using "try-catch" function.
				
				//Initializing important variables for operation
				String input1, input2;
				int num1, num2, result;
				
				//Making two JOptionPane inputs
				input1 = JOptionPane.showInputDialog("Please Input First Number: ");
				input2 = JOptionPane.showInputDialog("Please Input Second Number: ");
				
				//Converting the inputs to integer in order to do the Arithmetic operation by parsing the inputs.
				num1 = Integer.parseInt(input1);
				num2 = Integer.parseInt(input2);
				
				//Processing Arithmetic Operation
				result = num1 + num2;
				
				//Display the result using JOptionPane
				JOptionPane.showMessageDialog(null,result,"Result:",JOptionPane.INFORMATION_MESSAGE);
				} catch (NumberFormatException e){ //Catch the error if the user inputs a non-integer value
				
						//Display the error
						JOptionPane.showMessageDialog(null, "Please Input a Integer","Error",JOptionPane.ERROR_MESSAGE);
				}
			}
		}
		);
		
		//Implemeting Even-Listener on JButton button[1] which is subtraction
		buttons[1].addActionListener(
		new ActionListener() {
			
			//Handle JButton event if it is clicked
			public void actionPerformed(ActionEvent event) {
				try {
				String input1, input2;
				int num1, num2, result;
				
				input1 = JOptionPane.showInputDialog("Please Input First Number: ");
				input2 = JOptionPane.showInputDialog("Please Input Second Number: ");
				
				num1 = Integer.parseInt(input1);
				num2 = Integer.parseInt(input2);
				
				result = num1 - num2;
				
				JOptionPane.showMessageDialog(null,result,"Result:",JOptionPane.INFORMATION_MESSAGE);
				} catch (NumberFormatException e){
						JOptionPane.showMessageDialog(null, "Please Input a Integer","Error",JOptionPane.ERROR_MESSAGE);

				}
			}
		}
		);
		
		//Implemeting Even-Listener on JButton button[2] which is multiplication
		buttons[2].addActionListener(
		new ActionListener() {
			
			//Handle JButton event if it is clicked
			public void actionPerformed(ActionEvent event) {
				try {
				String input1, input2;
				int num1, num2, result;
				
				input1 = JOptionPane.showInputDialog("Please Input First Number: ");
				input2 = JOptionPane.showInputDialog("Please Input Second Number: ");
				
				num1 = Integer.parseInt(input1);
				num2 = Integer.parseInt(input2);
				
				result = num1 * num2;
				
				JOptionPane.showMessageDialog(null,result,"Result:",JOptionPane.INFORMATION_MESSAGE);
				} catch (NumberFormatException e){
						JOptionPane.showMessageDialog(null, "Please Input a Integer","Error",JOptionPane.ERROR_MESSAGE);

				}
			}
		}
		);
		
		//Implemeting Even-Listener on JButton button[3] which is division
		buttons[3].addActionListener(
		new ActionListener() {
			
			//Handle JButton event if it is clicked
			public void actionPerformed(ActionEvent event) {
				try {
				String input1, input2;
				int num1, num2, result;
				
				input1 = JOptionPane.showInputDialog("Please Input First Number: ");
				input2 = JOptionPane.showInputDialog("Please Input Second Number: ");
				
				num1 = Integer.parseInt(input1);
				num2 = Integer.parseInt(input2);
				
				result = num1 / num2;
				
				JOptionPane.showMessageDialog(null,result,"Result:",JOptionPane.INFORMATION_MESSAGE);
				} catch (NumberFormatException e){
						JOptionPane.showMessageDialog(null, "Please Input a Integer","Error",JOptionPane.ERROR_MESSAGE);

				}
			}
		}
		);
		
		buttons[0].setMnemonic('A');
		buttons[1].setMnemonic('S');
		buttons[2].setMnemonic('M');
		buttons[3].setMnemonic('D');
		
    	/**Set all the Components Visible.
    	 * If it is set to "false", the components in the container will not be visible.
    	 */
    	setVisible(true);
    	setResizable(false); //Fix window height and width
    }
    
	//Main Method
    public static void main (String[] args) {
    	arithmeticOperationJOptionPane pjtf = new arithmeticOperationJOptionPane();
	}
}

Important Part of the Program:

//Constructing all 4 JButtons using "for loop" and add them in the container
    	for(int count=0; count<buttons.length; count++) {
    		buttons[count] = new JButton(operation[count]);
    		pane.add(buttons[count]);
    	}
		
		//Implemeting Even-Listener on JButton button[0] which is addition
		buttons[0].addActionListener(
		new ActionListener() {
			
			//Handle JButton event if it is clicked
			public void actionPerformed(ActionEvent event) {
				try { //fetch an error using "try-catch" function.
				
				//Initializing important variables for operation
				String input1, input2;
				int num1, num2, result;
				
				//Making two JOptionPane inputs
				input1 = JOptionPane.showInputDialog("Please Input First Number: ");
				input2 = JOptionPane.showInputDialog("Please Input Second Number: ");
				
				//Converting the inputs to integer in order to do the Arithmetic operation by parsing the inputs.
				num1 = Integer.parseInt(input1);
				num2 = Integer.parseInt(input2);
				
				//Processing Arithmetic Operation
				result = num1 + num2;
				
				//Display the result using JOptionPane
				JOptionPane.showMessageDialog(null,result,"Result:",JOptionPane.INFORMATION_MESSAGE);
				} catch (NumberFormatException e){ //Catch the error if the user inputs a non-integer value
				
						//Display the error
						JOptionPane.showMessageDialog(null, "Please Input a Integer","Error",JOptionPane.ERROR_MESSAGE);
				}
			}
		}
		);
		
		//Implemeting Even-Listener on JButton button[1] which is subtraction
		buttons[1].addActionListener(
		new ActionListener() {
			
			//Handle JButton event if it is clicked
			public void actionPerformed(ActionEvent event) {
				try {
				String input1, input2;
				int num1, num2, result;
				
				input1 = JOptionPane.showInputDialog("Please Input First Number: ");
				input2 = JOptionPane.showInputDialog("Please Input Second Number: ");
				
				num1 = Integer.parseInt(input1);
				num2 = Integer.parseInt(input2);
				
				result = num1 - num2;
				
				JOptionPane.showMessageDialog(null,result,"Result:",JOptionPane.INFORMATION_MESSAGE);
				} catch (NumberFormatException e){
						JOptionPane.showMessageDialog(null, "Please Input a Integer","Error",JOptionPane.ERROR_MESSAGE);

				}
			}
		}
		);
		
		//Implemeting Even-Listener on JButton button[2] which is multiplication
		buttons[2].addActionListener(
		new ActionListener() {
			
			//Handle JButton event if it is clicked
			public void actionPerformed(ActionEvent event) {
				try {
				String input1, input2;
				int num1, num2, result;
				
				input1 = JOptionPane.showInputDialog("Please Input First Number: ");
				input2 = JOptionPane.showInputDialog("Please Input Second Number: ");
				
				num1 = Integer.parseInt(input1);
				num2 = Integer.parseInt(input2);
				
				result = num1 * num2;
				
				JOptionPane.showMessageDialog(null,result,"Result:",JOptionPane.INFORMATION_MESSAGE);
				} catch (NumberFormatException e){
						JOptionPane.showMessageDialog(null, "Please Input a Integer","Error",JOptionPane.ERROR_MESSAGE);

				}
			}
		}
		);
		
		//Implemeting Even-Listener on JButton button[3] which is division
		buttons[3].addActionListener(
		new ActionListener() {
			
			//Handle JButton event if it is clicked
			public void actionPerformed(ActionEvent event) {
				try {
				String input1, input2;
				int num1, num2, result;
				
				input1 = JOptionPane.showInputDialog("Please Input First Number: ");
				input2 = JOptionPane.showInputDialog("Please Input Second Number: ");
				
				num1 = Integer.parseInt(input1);
				num2 = Integer.parseInt(input2);
				
				result = num1 / num2;
				
				JOptionPane.showMessageDialog(null,result,"Result:",JOptionPane.INFORMATION_MESSAGE);
				} catch (NumberFormatException e){
						JOptionPane.showMessageDialog(null, "Please Input a Integer","Error",JOptionPane.ERROR_MESSAGE);

				}
			}
		}
		);

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Hostgator Discount Code