simple calculator program in java
Java
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Enter two numbers: ");
// nextDouble() reads the next double from the keyboard
double first = reader.nextDouble();
double second = reader.nextDouble();
System.out.print("Enter an operator (+, -, *, /): ");
char operator = reader.next().charAt(0);
double result;
switch(operator)
{
case '+':
result = first + second;
break;
case '-':
result = first - second;
break;
case '*':
result = first * second;
break;
case '/':
result = first / second;
break;
// operator doesn't match any case constant (+, -, *, /)
default:
System.out.printf("Error! operator is not correct");
return;
}
System.out.printf("%.1f %c %.1f = %.1f", first, operator, second, result);
}
}Save this file as MyCalculator.java
to compile it use
javac MyCalculator.java
to use the calcuator do this
java MyCalculator
**********************************************/
import java.awt.*;
import java.awt.event.*;
/*********************************************/
public class MyCalculator extends Frame
{
public boolean setClear=true;
double number, memValue;
char op;
String digitButtonText[] = {"7", "8", "9", "4", "5", "6", "1", "2", "3", "0", "+/-", "." };
String operatorButtonText[] = {"/", "sqrt", "*", "%", "-", "1/X", "+", "=" };
String memoryButtonText[] = {"MC", "MR", "MS", "M+" };
String specialButtonText[] = {"Backspc", "C", "CE" };
MyDigitButton digitButton[]=new MyDigitButton[digitButtonText.length];
MyOperatorButton operatorButton[]=new MyOperatorButton[operatorButtonText.length];
MyMemoryButton memoryButton[]=new MyMemoryButton[memoryButtonText.length];
MySpecialButton specialButton[]=new MySpecialButton[specialButtonText.length];
Label displayLabel=new Label("0",Label.RIGHT);
Label memLabel=new Label(" ",Label.RIGHT);
final int FRAME_WIDTH=325,FRAME_HEIGHT=325;
final int HEIGHT=30, WIDTH=30, H_SPACE=10,V_SPACE=10;
final int TOPX=30, TOPY=50;
///////////////////////////
MyCalculator(String frameText)//constructor
{
super(frameText);
int tempX=TOPX, y=TOPY;
displayLabel.setBounds(tempX,y,240,HEIGHT);
displayLabel.setBackground(Color.BLUE);
displayLabel.setForeground(Color.WHITE);
add(displayLabel);
memLabel.setBounds(TOPX, TOPY+HEIGHT+ V_SPACE,WIDTH, HEIGHT);
add(memLabel);
// set Co-ordinates for Memory Buttons
tempX=TOPX;
y=TOPY+2*(HEIGHT+V_SPACE);
for(int i=0; i<memoryButton.length; i++)
{
memoryButton[i]=new MyMemoryButton(tempX,y,WIDTH,HEIGHT,memoryButtonText[i], this);
memoryButton[i].setForeground(Color.RED);
y+=HEIGHT+V_SPACE;
}
//set Co-ordinates for Special Buttons
tempX=TOPX+1*(WIDTH+H_SPACE); y=TOPY+1*(HEIGHT+V_SPACE);
for(int i=0;i<specialButton.length;i++)
{
specialButton[i]=new MySpecialButton(tempX,y,WIDTH*2,HEIGHT,specialButtonText[i], this);
specialButton[i].setForeground(Color.RED);
tempX=tempX+2*WIDTH+H_SPACE;
}
//set Co-ordinates for Digit Buttons
int digitX=TOPX+WIDTH+H_SPACE;
int digitY=TOPY+2*(HEIGHT+V_SPACE);
tempX=digitX; y=digitY;
for(int i=0;i<digitButton.length;i++)
{
digitButton[i]=new MyDigitButton(tempX,y,WIDTH,HEIGHT,digitButtonText[i], this);
digitButton[i].setForeground(Color.BLUE);
tempX+=WIDTH+H_SPACE;
if((i+1)%3==0){tempX=digitX; y+=HEIGHT+V_SPACE;}
}
//set Co-ordinates for Operator Buttons
int opsX=digitX+2*(WIDTH+H_SPACE)+H_SPACE;
int opsY=digitY;
tempX=opsX; y=opsY;
for(int i=0;i<operatorButton.length;i++)
{
tempX+=WIDTH+H_SPACE;
operatorButton[i]=new MyOperatorButton(tempX,y,WIDTH,HEIGHT,operatorButtonText[i], this);
operatorButton[i].setForeground(Color.RED);
if((i+1)%2==0){tempX=opsX; y+=HEIGHT+V_SPACE;}
}
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent ev)
{System.exit(0);}
});
setLayout(null);
setSize(FRAME_WIDTH,FRAME_HEIGHT);
setVisible(true);
}
//////////////////////////////////
static String getFormattedText(double temp)
{
String resText=""+temp;
if(resText.lastIndexOf(".0")>0)
resText=resText.substring(0,resText.length()-2);
return resText;
Also in Java:
- Title
- java stream to list
- Category
- Java
- Title
- print out value java
- Category
- Java
- Title
- read integer input java
- Category
- Java
- Title
- hide element selenium
- Category
- Java
- Title
- how to minimize jframe in java
- Category
- Java
- Title
- simple javascript to detect browser using java utils
- Category
- Java
- Title
- applicationcontext.xml
- Category
- Java
- Title
- spigot title
- Category
- Java
- Title
- read csv java
- Category
- Java
- Title
- java list all non directory files in the directory
- Category
- Java
- Title
- android clear specific sharedpreference value
- Category
- Java
- Title
- install java debian 8
- Category
- Java
- Title
- java double 2 decimal
- Category
- Java
- Title
- java loop object
- Category
- Java
- Title
- sign flutter app android studio
- Category
- Java
- Title
- get spring application context
- Category
- Java
- Title
- how-to-use-volley-string-request-in-android
- Category
- Java
- Title
- how to create an array of arraylist in java
- Category
- Java
- Title
- sc.nextline skips
- Category
- Java
- Title
- bootstrap alert
- Category
- Java
- Title
- same method name with different arguments
- Category
- Java
- Title
- java creare costante
- Category
- Java
- Title
- java "->"
- Category
- Java
- Title
- java lambda list of objects cast
- Category
- Java
- Title
- how to loop trough an object java script
- Category
- Java
- Title
- java loop hashmap
- Category
- Java
- Title
- deleting elements of an array in java
- Category
- Java
- Title
- link to method javadoc
- Category
- Java
- Title
- java stack
- Category
- Java
- Title
- convert string to int java
- Category
- Java
- Title
- Write a JAVA method that expands a given binomial (ax + by)n, where integers a, b, n are user inputs. For example, if a = 2, b = -12, n = 4 are entered the method should print or return
- Category
- Java
- Title
- java string to int
- Category
- Java
- Title
- how to add an object to a list of objects in java
- Category
- Java
- Title
- como saber de que tipo es una variable en java
- Category
- Java
- Title
- how to remove spaces from an array in java
- Category
- Java
- Title
- android studio centering textview in relativelayout
- Category
- Java
- Title
- jtable font size
- Category
- Java
- Title
- kotlin add value to MutableLiveData
- Category
- Java
- Title
- java max
- Category
- Java
- Title
- print map java
- Category
- Java
- Title
- java hashmap syntax
- Category
- Java
- Title
- how to get the screen dimensions in java
- Category
- Java
- Title
- jenkins decrypt password script console
- Category
- Java
- Title
- java length of matrix
- Category
- Java
- Title
- java sql question mark
- Category
- Java
- Title
- date to string java
- Category
- Java
- Title
- how to reverse a string in java
- Category
- Java
- Title
- online money transfer andhra bank
- Category
- Java
- Title
- sdkmanager JAVA_HOME invalid directory
- Category
- Java
- Title
- value receive null with post method in the java spring controller
- Category
- Java
- Title
- how to make array of objects in java and use it
- Category
- Java
- Title
- java list get first element
- Category
- Java
- Title
- searching in database using java
- Category
- Java
- Title
- tower of hanoi program in java using recursion
- Category
- Java
- Title
- java parse xml string
- Category
- Java
- Title
- how to end a program in an if statement java
- Category
- Java
- Title
- mkyong restful web services spring boot examples post
- Category
- Java
- Title
- hibernate Unknown integral data type for ids : java.lang.String
- Category
- Java
- Title
- java for in loop
- Category
- Java
- Title
- int to string java
- Category
- Java
- Title
- how to get length of integer in java
- Category
- Java
- Title
- import java.util.hashset
- Category
- Java
- Title
- enumerated data types in java
- Category
- Java
- Title
- is self divisor java
- Category
- Java
- Title
- bracket balancing program in java
- Category
- Java
- Title
- android studio remove notification bar
- Category
- Java
- Title
- fibonacci sequence in java recursion
- Category
- Java
- Title
- count occurrences of character in string java 8
- Category
- Java
- Title
- initialize scanner java
- Category
- Java
- Title
- binary to int java
- Category
- Java
- Title
- creating the functional interface in java
- Category
- Java
- Title
- java cannot find file path
- Category
- Java
- Title
- check if a char is a space java
- Category
- Java
- Title
- how to compare two maps in java
- Category
- Java
- Title
- java store hexadecimal value
- Category
- Java
- Title
- java read file text
- Category
- Java
- Title
- java switch
- Category
- Java
- Title
- set top corner of shape radius programmatically android
- Category
- Java
- Title
- addition of two numbers in java
- Category
- Java
- Title
- java unit test an api
- Category
- Java
- Title
- java random number
- Category
- Java
- Title
- how to declare a linked list in java
- Category
- Java
- Title
- java iterate through hashmap
- Category
- Java
- Title
- how to calculate exponential in java
- Category
- Java
- Title
- retrofit
- Category
- Java
- Title
- java next permutation
- Category
- Java
- Title
- java - a program to print open ports
- Category
- Java
- Title
- java get keys from hashmap
- Category
- Java
- Title
- how lock horizontal orientation android
- Category
- Java
- Title
- demo java file
- Category
- Java
- Title
- how to check if rs next is null
- Category
- Java
- Title
- read and write files pattern java
- Category
- Java
- Title
- remove space string java
- Category
- Java
- Title
- primitive data types in java
- Category
- Java
- Title
- java http interceptor
- Category
- Java
- Title
- spring data jpa inheritance repository
- Category
- Java
- Title
- kotlin add element to array
- Category
- Java
- Title
- java if
- Category
- Java
- Title
- java random char a-z
- Category
- Java
- Title
- declare array java inline
- Category
- Java