Pages

E-mail us at prgms4starters@gmail.com for any queries or to share your programs. Please mention the program definition in the subject line.

In case you need help in finishing a program, do mention "incomplete pgm" in the subject line. Our team of experts will do their best to help you.

Click the link to follow this page for updates and reminders: FOLLOW THIS SITE

You can also visit our Facebook page by clicking HERE

**don't forget to participate in the polls at the bottom of the page

Saturday, September 3, 2011

calculator using applet

//Calculator Applet

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/*
<applet code=calc width=400 height=350>
</applet>
*/
public class calc extends Applet implements ActionListener // step1
{
double currentValue = 0;// currentValue stores the number currently being displayed
double previousValue = 0;// previousValue stores the number that was displayed before
boolean waitingForDigit = true;
char lastOperation = '=';
boolean computationError = false;

JTextField MainDisplay;
Font f1 = new Font("LCD", 1, 21);
Font f2 = new Font("DigifaceWide", 1, 30);
LayoutManager Layout;
JPanel pNum,pMast,pSym;
JButton b[];//button dec
public void init()
{
pNum=new JPanel();
pSym=new JPanel();
pNum.setBackground(Color.BLUE);
pSym.setBackground(Color.BLACK);
pMast=new JPanel();
pMast.setBackground(Color. BLACK);
MainDisplay=new JTextField();
MainDisplay.setFont(f2);
b=new JButton[23];
for(int i=0;i<=9;i++)
b[i]=new JButton(String.valueOf(i));
b[10] = new JButton("+/-");
b[11] = new JButton(".");
b[12] = new JButton("=");
b[13] = new JButton("/");
b[14] = new JButton("*");
b[15] = new JButton("-");
b[16] = new JButton("+");
b[17] = new JButton("sqrt");
b[18] = new JButton(" 1/x");
b[19] = new JButton("Sin");
b[20] = new JButton("cos");
b[21] = new JButton("tan");
b[22] = new JButton("C");
//seting color for buttons
for (int i = 0; i < b.length; i++)
{
b[i].setFont(f1);
if (i < 10)
b[i].setForeground(Color.blue);
else
b[i].setForeground(Color.red);
}
pNum.setLayout (new GridLayout(4, 3, 2, 2));
pSym.setLayout (new GridLayout(4, 3, 2, 2));
//Set the NUmbers
for (int i = 7; i <= 9; i++)
pNum.add(b[i]);
for (int i = 4; i <= 6; i++)
pNum.add(b[i]);
for (int i = 1; i <= 3; i++)
pNum.add(b[i]);
pNum.add(b[0]);//0
pSym.add(b[19]);//SIN
pSym.add(b[20]);//COS
pSym.add(b[21]);//TAN
pSym.add(b[17]);//sqrt
pSym.add(b[10]);//+-
pSym.add(b[18]);//1/x
pSym.add(b[16]);//+
pSym.add(b[15]);//sub
pSym.add(b[14]);// mul
pSym.add(b[13]);//division
pSym.add(b[22]);//C
pSym.add(b[12]);//=
for (int i = 0; i<=22; i++)
b[i].addActionListener(this);
pMast.setLayout(new BorderLayout(10,10));
pMast.add(pNum, BorderLayout.WEST);
pMast.add(pSym, BorderLayout.EAST);
pMast.add(MainDisplay,BorderLayout.NORTH);
this.setLayout(new BorderLayout(10,10));
add(pMast, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent ae)//ae contains source event
{
String label = ae.getActionCommand(); // The button label
char first = label.charAt(0); // the first char of the label
String x;
if (first == 'C')
{
currentValue = 0;
waitingForDigit = true;
computationError = false;
} else if (label.equals("+/-"))
{
currentValue = -currentValue;
waitingForDigit = true;
} else if ('0'<=first && first <='9')
{
//int digit = first - '0'; // Convert to an int
x=String.valueOf(first);
int digit=Integer.parseInt(x);
if (waitingForDigit)
{
currentValue = digit;
waitingForDigit = false;
} else
currentValue=(currentValue*10)+digit;
} else if(first==' '||first=='S'||first=='s'||first=='c'||first=='t')
{
lastOperation = first;
computeLastOperation();
lastOperation = '=';
waitingForDigit = false;
} else
{ // +,-,*,/, or =
computeLastOperation();
lastOperation = first;
previousValue = currentValue;
waitingForDigit = true;
}
displayCurrentValue();
}

private void displayCurrentValue()
{
if (computationError)
MainDisplay.setText(" Error ");
else
MainDisplay.setText(Double.toString(currentValue));
}

private void computeLastOperation()
{
switch (lastOperation)
{
case '=':
break;
case 's':currentValue=Math.sqrt(currentValue);
break;
case '+':currentValue=previousValue + currentValue;
break;
case '-':currentValue=previousValue - currentValue;
break;
case '*':currentValue=previousValue * currentValue;
break;
case ' ':
if (currentValue==0)
computationError = true;
else
currentValue = 1 / currentValue;
displayCurrentValue() ;
break;
case 'S':currentValue=Math.sin(currentValue);
break;
case 'c':currentValue=Math.cos(currentValue);
break;
case 't':currentValue=Math.tan(currentValue);
break;
case '/':
if (currentValue==0)
computationError = true;
else
currentValue = previousValue / currentValue;
break;
}
}
}

We have noticed that 20% of our visitors are still using internet explorer. For a better and faster surfing experience we recommend that you switch to either Google Chrome or Mozilla Firefox. We cannot provide you the downloads but here are the links to sites where you can download them from

Google Chrome - click here to go to site

Mozilla Firefox - click here to go to site

Where are you from

Do you think this site was helpful

Should we put up more C or C++ programs