Friday, August 27, 2010

A Simple Calculator in Java

Hi Friends....just had some free time, so thought of creating a "Simple Calculator" in Java...As claims it is "Simple" and don't expect anything Big out of it (Just some basic operations).....I have added the source code for it too....

(You can use it to Create your own Calculator and may be which is more efficient than mine..Just in case u find any flaws in this, pls do mail me abt it..)

Screenshot of "Calculator" that I created in Java



Download the "Calculator"

Download the Source Code

Code of Calculator.java

// Initial Declarations

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

// Creating a class named Calculator

class Calculator
{

// Components that are required to create the Calculator

JFrame frame = new JFrame();
// Creating the Menu Bar
JMenuBar menubar = new JMenuBar();
//---> Creating the "Calculator-->Exit" Menu
JMenu firstmenu = new JMenu("Calculator");
JMenuItem exitmenu = new JMenuItem("Exit");
// Creating The TextArea that gets the value
JTextField editor = new JTextField();
JRadioButton degree = new JRadioButton("Degree");
JRadioButton radians = new JRadioButton("Radians");

String[] buttons = {"BKSP","CLR","sin","cos","tan","7","8","9","/","+/-","4","5","6","X","x^2","1","2","3","-","1/x","0",".","=","+","sqrt"};

JButton[] jbuttons = new JButton[26];
double buf=0,result;
boolean opclicked=false,firsttime=true;
String last_op;

// Creating a Constructor to Initialize the Calculator Window

public Calculator()
{
frame.setSize(372,270);
frame.setTitle("Calculator - By G.Vivek Venkatesh.");
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);

ButtonHandler bhandler = new ButtonHandler();

menubar.add(firstmenu);
firstmenu.add(exitmenu);
exitmenu.setActionCommand("mExit");
exitmenu.addActionListener(bhandler);

editor.setPreferredSize(new Dimension(20,50));

Container buttoncontainer = new Container();

buttoncontainer.setLayout(new GridLayout(5,5));
for(int i=0;i {
jbuttons[i] = new JButton(buttons[i]);
jbuttons[i].setActionCommand(buttons[i]);
jbuttons[i].addActionListener(bhandler);
buttoncontainer.add(jbuttons[i]);
}
JPanel degrad = new JPanel();
degrad.setLayout(new FlowLayout());
ButtonGroup bg1 = new ButtonGroup();
bg1.add(degree);
bg1.add(radians);
degrad.add(degree);
radians.setSelected(true);
degrad.add(radians);

frame.setJMenuBar(menubar);
frame.add(editor,BorderLayout.NORTH);
frame.add(degrad,BorderLayout.CENTER);
frame.add(buttoncontainer,BorderLayout.SOUTH);
frame.setVisible(true);
}

// Class that handles the Events (that implements ActionListener)

public class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String action = e.getActionCommand();
if(action == "0" || action=="1" || action=="2" || action=="3" || action=="4" || action=="5" || action=="6" || action=="7" || action=="8" || action=="9" || action==".")
{
if(opclicked == false)
editor.setText(editor.getText() + action);
else
{
editor.setText(action);
opclicked = false;
}
}
if(action == "CLR")
{
editor.setText("");
buf=0;
result=0;
opclicked=false;
firsttime=true;
last_op=null;
}
//Addition
if(action=="+")
{
firsttime = false;
if(last_op!="=" && last_op!="sqrt" && last_op!="1/x" && last_op!="x^2" && last_op!="+/-")
{
buf = buf + Double.parseDouble(editor.getText());
editor.setText(Double.toString(buf));
last_op = "+";
opclicked=true;
}
else
{
opclicked=true;
last_op = "+";
}
}
// Subtraction
if(action=="-")
{
if(firsttime==true)
{
buf = Double.parseDouble(editor.getText());
firsttime = false;
opclicked=true;
last_op = "-";
}
else
{
if(last_op!="=" && last_op!="sqrt" && last_op!="1/x" && last_op!="x^2" && last_op!="+/-")
{
buf = buf - Double.parseDouble(editor.getText());
editor.setText(Double.toString(buf));
last_op = "-";
opclicked=true;
}
else
{
opclicked=true;
last_op = "-";
}
}
}
//Multiplication
if(action=="X")
{
if(firsttime==true)
{
buf = Double.parseDouble(editor.getText());
firsttime = false;
opclicked = true;
last_op = "X";
}
else
{
if(last_op!="=" && last_op!="sqrt" && last_op!="1/x" && last_op!="x^2" && last_op!="+/-")
{
buf = buf * Double.parseDouble(editor.getText());
editor.setText(Double.toString(buf));
last_op = "X";
opclicked=true;
}
else
{
opclicked=true;
last_op = "X";
}
}
}
//Division
if(action=="/")
{
if(firsttime==true)
{
buf = Double.parseDouble(editor.getText());
firsttime = false;
opclicked=true;
last_op = "/";
}
else
{
if(last_op!="=" && last_op!="sqrt" && last_op!="1/x" && last_op!="x^2" && last_op!="+/-")
{
buf = buf / Double.parseDouble(editor.getText());
editor.setText(Double.toString(buf));
last_op = "/";
opclicked=true;
}
else
{
opclicked=true;
last_op = "/";
}
}
}
// Equal to
if(action=="=")
{
result = buf;
if(last_op=="+")
{
result = buf + Double.parseDouble(editor.getText());
buf = result;
}
if(last_op=="-")
{
result = buf - Double.parseDouble(editor.getText());
buf = result;
}
if(last_op=="X")
{
result = buf * Double.parseDouble(editor.getText());
buf = result;
}
if(last_op=="/")
{
try
{
result = buf / Double.parseDouble(editor.getText());
}
catch(Exception ex)
{
editor.setText("Math Error " + ex.toString());

}
buf = result;
}

editor.setText(Double.toString(result));
last_op = "=";
}
// Sqrt
if(action=="sqrt")
{

if(firsttime==false)
{
buf = Math.sqrt(buf);
editor.setText(Double.toString(buf));
opclicked=true;
last_op = "sqrt";
}
else
{
if(editor.getText()=="")
JOptionPane.showMessageDialog(frame,"Enter input pls...","Input Missing",JOptionPane.ERROR_MESSAGE);
else
{
buf = Double.parseDouble(editor.getText());
buf = Math.sqrt(buf);
editor.setText(Double.toString(buf));
firsttime = false;
opclicked=true;
last_op = "sqrt";
}
}

}
// Reciprocal

if(action=="1/x")
{

if(firsttime==false)
{
buf = 1/ buf;
editor.setText(Double.toString(buf));
opclicked=true;
last_op = "1/x";
}
else
{
if(editor.getText()==null)
JOptionPane.showMessageDialog(frame,"Enter input pls...","Input Missing",JOptionPane.ERROR_MESSAGE);
else
{
buf = Double.parseDouble(editor.getText());
buf = 1 / buf;
editor.setText(Double.toString(buf));
firsttime = false;
opclicked=true;
last_op = "1/x";
}
}

}

// Square

if(action=="x^2")
{

if(firsttime==false)
{
buf = buf * buf;
editor.setText(Double.toString(buf));
opclicked=true;
last_op = "x^2";
}
else
{
if(editor.getText()==null)
JOptionPane.showMessageDialog(frame,"Enter input pls...","Input Missing",JOptionPane.ERROR_MESSAGE);
else
{
buf = Double.parseDouble(editor.getText());
buf = buf * buf;
editor.setText(Double.toString(buf));
firsttime = false;
opclicked=true;
last_op = "x^2";
}
}

}

// Negation +/-
if(action=="+/-")
{

if(firsttime==false)
{
buf = -(buf);
editor.setText(Double.toString(buf));
opclicked=true;
last_op = "+/-";
}
else
{
if(editor.getText()==null)
JOptionPane.showMessageDialog(frame,"Enter input pls...","Input Missing",JOptionPane.ERROR_MESSAGE);
else
{
buf = Double.parseDouble(editor.getText());
buf = -(buf);
editor.setText(Double.toString(buf));
firsttime = false;
opclicked=true;
last_op = "+/-";
}
}

}
// Exit
if(action=="mExit")
{
frame.dispose();
System.exit(0);
}
if(action=="mCut")
editor.cut();
if(action=="mCopy")
editor.copy();
if(action=="mPaste")
editor.paste();
if(action=="sin")
{
if(radians.isSelected())
{
if(firsttime==false)
{
buf = Math.sin(Double.parseDouble(editor.getText()));
editor.setText(Double.toString(buf));
opclicked=true;
last_op = "sin";
}
else
{
if(editor.getText()=="")
JOptionPane.showMessageDialog(frame,"Enter input pls...","Input Missing",JOptionPane.ERROR_MESSAGE);
else
{
buf = Double.parseDouble(editor.getText());
buf = Math.sin(Double.parseDouble(editor.getText()));
editor.setText(Double.toString(buf));
firsttime = false;
opclicked=true;
last_op = "sin";
}
}
}
else
{
if(firsttime==false)
{
double rad=Math.toRadians(Double.parseDouble(editor.getText()));
buf = Math.sin(rad);
editor.setText(Double.toString(buf));
opclicked=true;
last_op = "sin";
}
else
{
if(editor.getText()=="")
JOptionPane.showMessageDialog(frame,"Enter input pls...","Input Missing",JOptionPane.ERROR_MESSAGE);
else
{
double rad=Math.toRadians(Double.parseDouble(editor.getText()));
buf = Math.sin(rad);
editor.setText(Double.toString(buf));
firsttime = false;
opclicked=true;
last_op = "sin";
}
}
}
}// end of sin

if(action=="cos")
{
if(radians.isSelected())
{
if(firsttime==false)
{
buf = Math.cos(Double.parseDouble(editor.getText()));
editor.setText(Double.toString(buf));
opclicked=true;
last_op = "cos";
}
else
{
if(editor.getText()=="")
JOptionPane.showMessageDialog(frame,"Enter input pls...","Input Missing",JOptionPane.ERROR_MESSAGE);
else
{
buf = Double.parseDouble(editor.getText());
buf = Math.sin(Double.parseDouble(editor.getText()));
editor.setText(Double.toString(buf));
firsttime = false;
opclicked=true;
last_op = "cos";
}
}
}
else
{
if(firsttime==false)
{
double rad=Math.toRadians(Double.parseDouble(editor.getText()));
buf = Math.cos(rad);
editor.setText(Double.toString(buf));
opclicked=true;
last_op = "cos";
}
else
{
if(editor.getText()=="")
JOptionPane.showMessageDialog(frame,"Enter input pls...","Input Missing",JOptionPane.ERROR_MESSAGE);
else
{
double rad=Math.toRadians(Double.parseDouble(editor.getText()));
buf = Math.cos(rad);
editor.setText(Double.toString(buf));
firsttime = false;
opclicked=true;
last_op = "cos";
}
}
}
}// end of cos
if(action=="tan")
{
if(radians.isSelected())
{
if(firsttime==false)
{
buf = Math.tan(Double.parseDouble(editor.getText()));
editor.setText(Double.toString(buf));
opclicked=true;
last_op = "tan";
}
else
{
if(editor.getText()=="")
JOptionPane.showMessageDialog(frame,"Enter input pls...","Input Missing",JOptionPane.ERROR_MESSAGE);
else
{
buf = Double.parseDouble(editor.getText());
buf = Math.tan(Double.parseDouble(editor.getText()));
editor.setText(Double.toString(buf));
firsttime = false;
opclicked=true;
last_op = "tan";
}
}
}
else
{
if(firsttime==false)
{
double rad=Math.toRadians(Double.parseDouble(editor.getText()));
buf = Math.tan(rad);
editor.setText(Double.toString(buf));
opclicked=true;
last_op = "tan";
}
else
{
if(editor.getText()=="")
JOptionPane.showMessageDialog(frame,"Enter input pls...","Input Missing",JOptionPane.ERROR_MESSAGE);
else
{
double rad=Math.toRadians(Double.parseDouble(editor.getText()));
buf = Math.tan(rad);
editor.setText(Double.toString(buf));
firsttime = false;
opclicked=true;
last_op = "tan";
}
}
}
}// end of tan
}
}
}


Comments are welcomed....(Although care has been taken it may contain logical errors or bugs, because I did it in few hours....)



Thursday, August 26, 2010

Got Inspired from "The GOOGLE STORY"

It is not often that I read books, but at times I do, and especially "Books of Inspiration". Recently I had been to Delhi, where I had bought a book "The GOOGLE story" by David A Vise for `200 (download rupee font if you don't see it Rupee Symbol).

Just now completed some 50 Pages of the 320 pages book, but the Kind of Inspiration that it gave me was Amazing. I wonder the hard work put, and the commitment shown by Youth Legends Larry Page and Sergey Brin. The book has been perfectly written by David A Vise and it can assure you its a Gripping book written like a Detective Story.....The various points that have been highlighted in the books can give you vast amount of confidence and create Zeal and ardor within you to achieve something. Its a detailed account of spectacular rise of the Worlds No 1 Search Engine.

So just in case you haven't read the book, and get a chance to read, don't miss out!!!....Looking for some surprises, inspiration from the Remaining part of the book....


G.Vivek Venkatesh


(Google Books Preview)



Saturday, August 14, 2010

Productive Indian YOUTH

Ten things that I believe should be done for making INDIAN YOUTH MORE PRODUCTIVE, (some of these things may be harsh and even I would have been addicted, but if all these things done means there would be a Drastic change.)


1. Ban SMS BOOSTERS by all Mobile Networks and 'charge per SMS' must be higher than 'charge per call'. This is the first and foremost thing that I would do If i were a PM. Because it is such small things that matter a lot.

2. Adopt a comprehensive approach for effective usage of Social Networking sites. The Kind of addiction that has been caused by these must be a key issue to be considered.

3. As we all know it is impossible to stop people from smoking without banning 'Cigarettes', similarly a stage may come where we can't stop YOUTH from getting addicted to Social Networking Sites without imposing a Ban on it. (Although either cases it is difficult to impose a Ban and hard to Digest if it is done.)

4. Stop Colleges from putting restrictions on Student's Dress codes and Thinking. U know they got to allow students to be more creative in their approach and Dressing sense. I strongly oppose putting restrictions on Dress (with the exception of Vulgar dresses).

5. Students are Lethal Weapons for a country if it decides to be a Superpower. And our INDIA is highly lucky and fortunate that the population in the Coming years would comprise majority of YOUTH in it. So EDUCATION SYSTEM has to be designed in such a way that it has to nurture the talents within the students rather than making them MUG UP something.

6. The Conventional Pedagogies adopted by Colleges and Schools have to be replaced by Pedagogies of Scientific approach so that we soon have a Microsoft and Google of Indian origin....and of course great scientists too...

7. Education - Both Quantitative Approach, and Qualitative Approach have to be adopted. It is not only our responsibility to take Education to all the Children but also the Quality of Education that is being Imparted must be taken care. (At present we are more worried of Quantity that we almost neglect the Quality, If IITs are ranked around 50s in world colleges list means there is some defect..right??).

8. Both Parents and Children must have compulsory counsellings when the children are in their adolescent age as they are Susceptible to more problems during that period.

9. No Massive Structure or a Big Company can be created Single handedly. It requires Team Work. Students have to be trained to work in TEAMS. Like for Example - Lots of Project Works and Group Discussions can be done in Colleges and Schools rather than giving boring Assignments and having DROWSY classes.

10. Last but not the least...PATRIOTISM, DISCIPLINE...(Patriotism for country is necessary at any level of life). All these have to be imparted from Kinder Garden Level itself. If we leave Tiny Tots without proper attention, then TINY TOTS would become BIG SHOTS in committing mistakes.


Written by G.Vivek Venkatesh.


Wishing u ALL a 'HAPPY INDEPENDENCE DAY' for this AUGUST 15th.......