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.......

Saturday, July 17, 2010

Download JAVA and IDE for it

Download links to get JAVA

http://java.sun.com/javase/downloads/index.jsp


Given Below are two IDEs which I believe to be good,


Netbeans:

NetBeans refers to both a platform framework for Java desktop applications, and an integrated development environment (IDE) for developing with Java, JavaScript, PHP, Python, Ruby, Groovy, C, C++, Scala, Clojure, and others.

The NetBeans IDE is written in Java and runs everywhere where a JVM is installed, including Windows, Mac OS, Linux, and Solaris. A JDK is required for Java development functionality, but is not required for development in other programming languages




Download Links for Netbeans IDE

http://netbeans.org/


Eclipse

Eclipse is a multi-language software development environment comprising an integrated development environment (IDE) and an extensible plug-in system. It is written primarily in Java and can be used to develop applications in Java and, by means of various plug-ins, other languages including C, C++, COBOL, Python, Perl, PHP and Ruby (including Ruby on Rails framework). The IDE is often called Eclipse ADT for Ada, Eclipse CDT for C/C++, Eclipse JDT for Java and Eclipse PDT for PHP.

Download Links for Eclipse

http://www.eclipse.org/downloads/

Tuesday, July 13, 2010

Knowing Knoppix

Knoppix is an astoundingly clever product. It runs Linux completely from a CD or DVD. There is no need to install. It bypasses all the software already installed on your PC or laptop and automatically detects the hardware in your computer (subject to suitable hardware). When you've finished using Knoppix, simply restart. Your computer will return to your regular system, and it will behave as if nothing has happened. Knoppix is Free Software and open source under the terms of the GNU General Public Licence (GPL).

It is often possible to rescue files using Knoppix from crashed Windows computers, even when Windows cannot be started.



Download Knoppix Live CD/DVD

http://www.knoppix.net/

Wikibook on Knoppix

http://en.wikibooks.org/wiki/Knowing_Knoppix


Sunday, July 4, 2010

Ubuntu Linux 10.04 LTS

I have been using Ubuntu linux for the past few months and it is really cool.....The kind of facilities it offers is really amazing and all of this at no cost as it supports Open source environment...It tops among Best Linux Distros in the market, and this 10.04 offers you more features....

The various features that I believe to be interesting are,

1. Easy Installation of Software - Installing Software is no longer a difficult process as all the Installations are just a click away in Ubuntu...The "Ubuntu Software center" offers more than 35000 software packages and utilities.


But if you are a Computer geek and you like challenging ways then you can always install the software via lines of code in the TERMINAL and thereby gaining some Knowledge....

2. Cool and Eye Catchy GUI

The GUI that comes with Ubuntu is really cool and can easily attract you....


3. Social Networking, Chatting, Mailing made easier

Ubuntu's new Me Menu brings all your social networks to you. Connect to Facebook, Twitter and more through a single window..




You can use Empathy to chat and Evolution mail to handle your mails...




4. Easy to Install Ubuntu

The major advantage of this is that you can easily install Ubuntu than other Distros. You can install it from Windows itself...And within 20 minutes(give or take a few) you can install Ubuntu. Its like installing any other software.


5. Easily Switch over between KDE and GNOME environment.


6. It can fit in any PC or Laptop and Portability has been its major achievement.

7. Various IDEs like Netbeans, Eclipse, etc to assist you in programming.


I could guarantee you that it would be really useful for you...



Download Link given below

Ubuntu Download


Friday, May 28, 2010

CBSE's Grade System

CBSE Class X results are out, but this time the ambiance among the student fraternity after the results is different from previous years. Generally students would be anxious to know the marks they have scored, but this time since CBSE has implemented the grade system and so they are relieved of the mark pressure that prevailed in the otherwise MARK System. Students will now get to know the grades in each subject, plus the grade point and the cumulative grade point average (CGPA). So the discrimination among students(based on marks) that prevailed in the Mark system has been brought down significantly. As the expectations and the anxiety level among the parents has dropped down, I believe that the number of suicides that would normally occur after board exam results would be brought down dramatically.

Many counselors and psychologists have welcomed this move by the CBSE, now what I think is that various state boards around the country must rethink their system and follow what CBSE has done. Good thing done by Mr.Kapil Sibal in implementing this system. Also this grade system is not new and widely adopted in various developing countries.