Basic Math Functions in Java

Download
Download is available until [expire_date]
  • Version
  • Download 67
  • File Size 3.84 KB
  • File Count 1
  • Create Date May 12, 2016
  • Last Updated May 12, 2016

Basic Math Functions in Java

Basic Math Functions in Java

This java program is written in console and gui format. The program will demonstrate the basic mathematical operators (add, minus, divide and multiply). It has also a do while loop which allows the user to execute the program again or just close and end it.

Source code:

import javax.swing.*;
class MathOperator {
public static void main(String[] args) {
	
String input1, input2;	
double firstnum, secondnum;
String cont="n";
String operator;
double result=0;

do
{
operator=JOptionPane.showInputDialog(null, "Please select an operation:" + "
" + "1 for addition" + "
" + "2 for subtraction" + "
" + "3 for multiplication" + "
" + "4 for division");

input1 = JOptionPane.showInputDialog(null,"enter first number: ");
firstnum=Double.parseDouble(input1);

input2 = JOptionPane.showInputDialog(null,"enter second number: ");
secondnum=Double.parseDouble(input2);

if (operator.matches("1")){
result = firstnum + firstnum ;
} else if (operator.matches("2")){
result=firstnum - secondnum;
}else if (operator.matches("3")){
result=firstnum * secondnum;	
} else if (operator.matches("4")){
result=firstnum / secondnum;	
}

JOptionPane.showMessageDialog(null,"Result: " + result  );

	cont =	JOptionPane.showInputDialog(null,"Try Again? (press y or n) ");
	}while (cont.matches("y"));

JOptionPane.showMessageDialog(null,"Program closing"  );

}
}
, , , , , ,

Post navigation

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.