Java and Android Programming Tutorials
Java
Arithmetic Operation using AWT Frame with Event Delegation
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
// author : www.raviroza.com
// date : 27-Jan-2023, 8.25 am
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ArithmaticOperation
extends Frame
implements ActionListener
{
Label l1,l2,lblResult;
TextField txtNo1, txtNo2;
Button btnAdd, btnSub, btnMul, btnDiv;
public ArithmaticOperation() {
// TODO Auto-generated constructor stub
l1 = new Label("enter number 1 : ");
l2 = new Label("enter number 2 : ");
lblResult = new Label("0");
txtNo1 = new TextField(3);
txtNo2 = new TextField(3);
btnAdd = new Button("+");
btnSub = new Button("-");
btnMul = new Button("*");
btnDiv = new Button("/");
setLayout(new FlowLayout());
add(l1); add(txtNo1);
add(l2); add(txtNo2);
add(lblResult);
add(btnAdd); add(btnSub);
add(btnMul); add(btnDiv);
btnAdd.addActionListener(this);
btnSub.addActionListener(this);
btnMul.addActionListener(this);
btnDiv.addActionListener(this);
setSize(500, 500);
//validate();
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
int a = Integer.parseInt(txtNo1.getText());
int b = Integer.parseInt(txtNo2.getText());
int c = 0;
if(e.getSource() == btnAdd)
{
c = a + b;
}
else if(e.getSource() == btnSub)
{
c = a - b;
}
else if(e.getSource() == btnMul)
{
c = a * b;
}
else if(e.getSource() == btnDiv)
{
c = a / b;
}
lblResult.setText ( Integer.toString(c));
}
public static void main(String[] args)
{
new ArithmaticOperation();
}
}
Video Tutorial to Learn Java Basics
Java is an object-oriented programming language written from scratch by James Gosling at Sun Micro-systems and released in 1995. Some of the syntaxes of java are similar to c and C++.
Following is Java Video tutorial to learn basics of Java language.
Buffered Input & Output, Buffered Reader & Writer in Java
- Buffered Input Streram
- It adds functionality to another input stream-namely, the ability to buffer the input and to support the mark and reset methods.
- When the BufferedInputStream is created, an internal buffer array is created.
- Buffered Output Stream
- This class implements a buffered output stream.
- By setting up such an output stream, an application can write bytes to the underlying output stream without necessarily causing a call to the underlying system for each byte written.