Java program to implement a simple calculator

Here is a small calculator that make use of operators [+,-,/,*,%]. Two numbers have to be inputed to calculate the all the results. Program is applicable for both integer and floating point numbers.

/**
 * SimpleCalc.java
 * Program for a simple calculator for integer and floating numbers
 *+using operators [+,-,/,*,%]
 * uses swing class to provide basic gui
 *  
 * @author jo
 * @author [jintu.jacob@gmail.com]
 */

import java.util.Random;
import javax.swing.*;

public class SimpleCalc {
     
     static int i,j,k;
     static float u,v,w;
     
     static void printInt(String x, int y) {
          System.out.println(x+" = "+y );
     }
     
     static void printFloat(String x, float y) {
          System.out.println(x+" = "+y);
     }
     
     static void readInt() {
          // This function reads two integer numbers and places in the class variables[i,j]
          String x = JOptionPane.showInputDialog("Number 1 : ");
          String y = JOptionPane.showInputDialog("Number 2 : ");
          i = Integer.parseInt(x);
          j = Integer.parseInt(y);
     }
     
     static void readFloat() {
          // This function reads two floating point numbers and places in the class variables[u,v]
          String x = JOptionPane.showInputDialog("Number 1 : ");
          String y = JOptionPane.showInputDialog("Number 2 : ");
          u = Float.parseFloat(x);
          v = Float.parseFloat(y);
     }
     
     public static void main(String[] args) {
          String c = JOptionPane.showInputDialog("1. Integer operation\n 2.Floating point operation\nEnter 1/2 :");
          int ch = Integer.parseInt(c);
         
          if (ch==1){
               readInt();
               printInt("i : ", i);
               printInt("j : ", j);

               k = i+j; printInt("i+j",k);
               k = i-j; printInt("i-j",k);
               k = i/j; printInt("i/j",k);
               k = i*j; printInt("i*j",k);
               k = i%j; printInt("i%j",k);

          }
          else {
               readFloat();
               printFloat("i : ", u);
               printFloat("i : ", v);
               
               w = u+v; printFloat("i+j",w);
               w = u-v; printFloat("i-j",w);
               w = u/v; printFloat("i/j",w);
               w = u*v; printFloat("i*j",w);
               w = u%v; printFloat("i%j",w);
          }
          System.exit(0);              
         
     }
}

/** Some additional facts!!
 *  ————————-
 *  use the Random class to generate the random numbers    
 *  Random xx = new Random();
 *  i = xx.nextInt(100);  //to generate a random integer number within 100
 *  i = xx.nextFloat(100);  //to generate a random floating number within 100
 */

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s