Pages

Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Saturday, August 20, 2016

Program for calculator in JAVA

This is the source code for a calculater in java
import java.io.*;
import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;

public class Calculator extends JApplet implements ActionListener
{
int choice;
double a,b, result = 0;
String str;
JTextField jt;
JButton btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9;
JButton btnAdd, btnSub, btnMul, btnDiv, btnEqual, btnDot, btnCl;
JPanel pn1, pn2;
Container cp;
public void init() // Initializing Applet & its Components
{
cp = getContentPane();
cp.setLayout ( new BorderLayout() );
jt = new JTextField(10);
jt.setHorizontalAlignment(4);
pn1 = new JPanel();
pn2 = new JPanel();
pn1.setLayout ( new FlowLayout() );
pn2.setLayout ( new GridLayout(4, 4) );
btnCl = new JButton ("Clear");
btn0 = new JButton ("0");
btn1 = new JButton ("1");
btn2 = new JButton ("2");
btn3 = new JButton ("3");
btn4 = new JButton ("4");
btn5 = new JButton ("5");
btn6 = new JButton ("6");
btn7 = new JButton ("7");
btn8 = new JButton ("8");
btn9 = new JButton ("9");
btnAdd = new JButton ("+");
btnSub = new JButton ("-");
btnMul = new JButton ("*");
btnDiv = new JButton ("/");
btnEqual = new JButton ("=");
btnDot = new JButton (".");
addButtons();
addListeners();
pn1.add (jt);
pn1.add (btnCl);
cp.add (pn1, BorderLayout.NORTH);
cp.add (pn2, BorderLayout.CENTER);
jt.setText("");
}

public void addListeners() // Adding Listeners to all Components
{
btn0.addActionListener ( this );
btn1.addActionListener ( this );
btn2.addActionListener ( this );
btn3.addActionListener ( this );
btn4.addActionListener ( this );
btn5.addActionListener ( this );
btn6.addActionListener ( this );
btn7.addActionListener ( this );
btn8.addActionListener ( this );
btn9.addActionListener ( this );
btnDot.addActionListener ( this );
btnCl.addActionListener ( this );
btnAdd.addActionListener ( this );
btnSub.addActionListener ( this );
btnMul.addActionListener ( this );
btnDiv.addActionListener ( this );
btnEqual.addActionListener ( this );
}

public void addButtons() // Adding all Components to JPanel
{
pn2.add ( btn7 );
pn2.add ( btn8 );
pn2.add ( btn9 );
pn2.add ( btnDiv );
pn2.add ( btn4 );
pn2.add ( btn5 );
pn2.add ( btn6 );
pn2.add ( btnMul );
pn2.add ( btn1 );
pn2.add ( btn2 );
pn2.add ( btn3 );
pn2.add ( btnSub );
pn2.add ( btn0 );
pn2.add ( btnDot );
pn2.add ( btnAdd );
pn2.add ( btnEqual );
}
public void operation(double a, double b, int op)
{
switch ( choice )
{
case 1 :
{ result = a + b; break; }
case 2 :
{ result = a - b; break; }
case 3 :
{ result = a * b; break; }
case 4 :
{ result = a / b; break; }
default :
{ jt.setText("0"); break; }
}
}

public void actionPerformed(ActionEvent e) // Actions to be performed
{ // whenever event is triggered
try
{
str = (String)e.getActionCommand();
if ( str = = "+" )
{
a=Double.parseDouble (jt.getText());
jt.setText("");
choice = 1;
}
else if ( str = = "-" )
{
a=Double.parseDouble (jt.getText());
jt.setText("");
choice = 2;
}
else if ( str = = "*" )
{
a=Double.parseDouble (jt.getText());
jt.setText("");
choice = 3;
}
else if ( str = = "/" )
{
a=Double.parseDouble (jt.getText());
jt.setText("");
choice = 4;
}
else if ( str = = "=" )
{
b = Double.parseDouble (jt.getText());
operation(a, b, choice);
jt.setText("");
System.out.println(result);
}
else if ( str = = "Clear" )
{
jt.setText("");
choice = 0;
a = b = result = 0;
}
else
{
jt.setText( jt.getText() +str );
}
}
catch(Exception ex)
{
jt.setText("Invalid Operation");
System.out.println("Invalid Operation");
}
}
}

//<applet code="Calculator.class" width=200 height=200></applet>
Read More..

Saturday, July 5, 2014

10 Garbage Collection Interview Questions and Answers in Java Programming

GC Interview Questions Answer in Java
Garbage collection interview questions are very popular in both core Java and advanced Java Interviews. Apart from  Java Collection and Thread  many tricky Java questions stems Garbage collections which are tough to answer. In this Java Interview article I will share some questions from GC which is asked in various core Java interviews.These questions are based upon concept of How Garbage collection works, Different kinds of Garbage collector and JVM parameters used for garbage collection monitoring and tuning. As I said GC is an important part of any Java interview so make sure you have good command in GC. One more thing which is getting very important is ability to comprehend and understand Garbage collection Output, more and more interviewer are checking whether candidate can understand GC output or not. During Java interview they may provide a snippet of GC output and ask various questions based on that e.g. Which Garbage collector is used, whether output is from major collection or minor collection, How much memory is free from GC, What is size of new generation and old generation after GC etc. I have included few Garbage collection interview questions and answers from GC output to help with that. It’s recommended to prepare questions from Java collection,  multithreading and programming along with Garbage collection to do well in Java interviews at any stage.
Read more »
Read More..

Wednesday, May 21, 2014

Step By Step guide to Read XML file in Java Using SAX Parser Example

Reading XML file in java using SAX Parser is little different than reading xml file in Java with DOM parser which we had discussed in last article of this series. This tutorial is can be useful for those who are new to the java world and got the requirement for read an xml file in java in their project or assignment, key feature of java is it provides built in class and object to handle everything which makes our task very easy. Basically this process of handling XML file is known as parsing means break down the whole string into small pieces using the special tokens.
Parsing can be done using two ways:
Read more »
Read More..