/* Program to create a Simple Calculator */
import java.awt.*;
import java.awt.event.*;
public class MyCalculator extends Frame implements ActionListener {
double num1,num2,result;
Label lbl1,lbl2,lbl3;
TextField tf1,tf2,tf3;
Button btn1,btn2,btn3,btn4;
char op;
MyCalculator() {
lbl1=new Label("Number 1: ");
lbl1.setBounds(50,100,100,30);
tf1=new TextField();
tf1.setBounds(160,100,100,30);
lbl2=new Label("Number 2: ");
lbl2.setBounds(50,170,100,30);
tf2=new TextField();
tf2.setBounds(160,170,100,30);
btn1=new Button("+");
btn1.setBounds(50,250,40,40);
btn2=new Button("-");
btn2.setBounds(120,250,40,40);
btn3=new Button("*");
btn3.setBounds(190,250,40,40);
btn4=new Button("/");
btn4.setBounds(260,250,40,40);
lbl3=new Label("Result : ");
lbl3.setBounds(50,320,100,30);
tf3=new TextField();
tf3.setBounds(160,320,100,30);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
add(lbl1); add(lbl2); add(lbl3);
add(tf1); add(tf2); add(tf3);
add(btn1); add(btn2); add(btn3); add(btn4);
setSize(400,500);
setLayout(null);
setTitle("Calculator");
setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
num1 = Double.parseDouble(tf1.getText());
num2 = Double.parseDouble(tf2.getText());
if(ae.getSource() == btn1)
{
result = num1 + num2;
tf3.setText(String.valueOf(result));
}
if(ae.getSource() == btn2)
{
result = num1 - num2;
tf3.setText(String.valueOf(result));
}
if(ae.getSource() == btn3)
{
result = num1 * num2;
tf3.setText(String.valueOf(result));
}
if(ae.getSource() == btn4)
{
result = num1 / num2;
tf3.setText(String.valueOf(result));
}
}
public static void main(String args[]) {
MyCalculator calc=new MyCalculator();
}
}
1) Use eclipse or Netbean platform and acquaint with the various menus, create a test project, add a test class and run it see how you can use auto suggestions, auto fill. Try code formatter and code refactoring like renaming variables, methods and classes. Try debug step by step with a small program of about 10 to 15 lines which contains at least one if else condition and a for loop. View Solution
2) Write a Java program to demonstrate the OOP principles. [i.e., Encapsulation, Inheritance, Polymorphism and Abstraction] View Solution
3) Write a Java program to handle checked and unchecked exceptions. Also, demonstrate the usage of custom exceptions in real time scenario. View Solution
4) Write a Java program on Random Access File class to perform different read and write operations. View Solution
5) Write a Java program to demonstrate the working of different collection classes. [Use package structure to store multiple classes]. View Solution
6) Write a program to synchronize the threads acting on the same object. [Consider the example of any reservations like railway, bus, movie ticket booking, etc.] View Solution
7) Write a program to perform CRUD operations on the student table in a database using JDBC. View Solution
8) Write a Java program that works as a simple calculator. Use a grid layout to arrange buttons for the digits and for the +, -,*, % operations. Add a text field to display the result. Handle any possible exceptions like divided by zero. View Solution
9) Write a Java program that handles all mouse events and shows the event name at the center of the window when a mouse event is fired. [Use Adapter classes] View Solution