java calculator code
Java
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
- how to see if a shape is touching another shape in java
- Category
- Java
- Title
- index of an array procesing
- Category
- Java
- Title
- jbutton open jframe java
- Category
- Java
- Title
- how to collect objective in java
- Category
- Java
- Title
- java get int from double without rounding
- Category
- Java
- Title
- method overloading
- Category
- Java
- Title
- number of matches regex java
- Category
- Java
- Title
- java fullscreen jframe
- Category
- Java
- Title
- set html text android java
- Category
- Java
- Title
- D/NetworkSecurityConfig: No Network Security Config specified, using platform default
- Category
- Java
- Title
- how to create dynamic string array in java
- Category
- Java
- Title
- localdate to string java
- Category
- Java
- Title
- String array into LinkedList java
- Category
- Java
- Title
- java catch multiple exceptions
- Category
- Java
- Title
- java script snippet for responsive
- Category
- Java
- Title
- how to convert a jsonobject to a dbobject
- Category
- Java
- Title
- what is a float java
- Category
- Java
- Title
- not equal java
- Category
- Java
- Title
- stream java example
- Category
- Java
- Title
- java loop through object
- Category
- Java
- Title
- make a commet in java
- Category
- Java
- Title
- code to include layout from java in android
- Category
- Java
- Title
- summary of operator java
- Category
- Java
- Title
- arraylist set method
- Category
- Java
- Title
- left shift in java
- Category
- Java
- Title
- java how to define a function
- Category
- Java
- Title
- loop collection android
- Category
- Java
- Title
- annotation spring notnull
- Category
- Java
- Title
- how to cut a certion part from a string in java
- Category
- Java
- Title
- Removing DOM nodes when traversing a NodeList
- Category
- Java
- Title
- java replace all xml decoding
- Category
- Java
- Title
- lopp array java
- Category
- Java
- Title
- the type javax.servlet.ServletException cannot be resolved. It is indirectly referenced from required .class files
- Category
- Java
- Title
- java random between two strings
- Category
- Java
- Title
- instantiate optinal java 8
- Category
- Java
- Title
- 2d array length in java
- Category
- Java
- Title
- android get distance between two locations kotlin
- Category
- Java
- Title
- priorityqueue poll
- Category
- Java
- Title
- java how to make a gui
- Category
- Java
- Title
- hibernate onetone with mapsid
- Category
- Java
- Title
- how to take max value from priority queue in java
- Category
- Java
- Title
- Method used for getting metadata of a database in jdbc
- Category
- Java
- Title
- java convert a string to char[]
- Category
- Java
- Title
- kotlin jsonobject get nested
- Category
- Java
- Title
- java get input
- Category
- Java
- Title
- android elevation not working
- Category
- Java
- Title
- absolute value java
- Category
- Java
- Title
- jtable get get row
- Category
- Java
- Title
- print hello world in java
- Category
- Java
- Title
- java replaceall single character
- Category
- Java
- Title
- java min function
- Category
- Java
- Title
- hashmaps java
- Category
- Java
- Title
- java string format .2f
- Category
- Java
- Title
- iterate through an arraylist java
- Category
- Java
- Title
- java not instanceof
- Category
- Java
- Title
- firebase connecten
- Category
- Java
- Title
- kotlin android intent pass data
- Category
- Java
- Title
- teimpo en segundos java
- Category
- Java
- Title
- math min max java
- Category
- Java
- Title
- setbackground java
- Category
- Java
- Title
- online money transfer andhra bank
- Category
- Java
- Title
- How to print in console java
- Category
- Java
- Title
- arraylist remove method java
- Category
- Java
- Title
- find frequency of each word in a string in java
- Category
- Java
- Title
- Java array multidimensional hashcode
- Category
- Java
- Title
- java int passed by reference
- Category
- Java
- Title
- java clone method
- Category
- Java
- Title
- reverse linked list in java to get both head and tail
- Category
- Java
- Title
- java display message
- Category
- Java
- Title
- java observable
- Category
- Java
- Title
- how to change actionbar color in android programmatically
- Category
- Java
- Title
- pretty print json in console
- Category
- Java
- Title
- java replace element in list
- Category
- Java
- Title
- java convert edittext to double
- Category
- Java
- Title
- java pause program until key pressed
- Category
- Java
- Title
- guess the number java
- Category
- Java
- Title
- displaying an arraylist in java
- Category
- Java
- Title
- java time code
- Category
- Java
- Title
- override java
- Category
- Java
- Title
- java is power of 2
- Category
- Java
- Title
- spigot repeating task
- Category
- Java
- Title
- how to make javafx full screen default
- Category
- Java
- Title
- java delay
- Category
- Java
- Title
- how to add objects in array java
- Category
- Java
- Title
- absolute value in java
- Category
- Java
- Title
- deserialize date java
- Category
- Java
- Title
- how to sort collection in java
- Category
- Java
- Title
- spring-boot actuator not working
- Category
- Java
- Title
- leap year checker java
- Category
- Java
- Title
- difference between java and javascript
- Category
- Java
- Title
- Spigot API inventory close
- Category
- Java
- Title
- java 8 list stream delete by name
- Category
- Java
- Title
- list in java
- Category
- Java
- Title
- import java.util.hashset
- Category
- Java
- Title
- java last element in array
- Category
- Java
- Title
- what is static setter and getter examples in java
- Category
- Java
- Title
- file to image javafx
- Category
- Java
- Title
- find a substring in a string java
- Category
- Java
- Title
- array methods in java
- Category
- Java
- Title
- java comments
- Category
- Java