0% found this document useful (0 votes)
46 views

Java Lab-Exercise - 8 & 9

Uploaded by

maneeshgopisetty
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Java Lab-Exercise - 8 & 9

Uploaded by

maneeshgopisetty
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Exercise – 8

a) Write a JAVA program that import and use the user defined packages
8. b) Write a program that creates a user interface to perform integer divisions. The user enters
two numbers in the textfields, Num1 and Num2. The division of Num1 and Num2 is displayed in
the Result field when the Divide button is clicked. If Num1 or Num2 were not an integer, the
program would throw a Number Format Exception. If Num2 were Zero, the program would
throw an Arithmetic Exception Display the exception in a message dialog box.

import java.awt.*;

import java.applet.*;

import java.awt.event.*;

import javax.swing.*;

/*<applet code="DivisionApplet" width=600 height=200>

</applet>*/

public class DivisionApplet extends Applet implements ActionListener {

Label L1,L2,L3;

TextField T1,T2,Result;

Button B1;

public void init() {

L1=new Label("Enter First Num :");

add(L1);

T1=new TextField(10);

add(T1);

L2=new Label("Enter Second Num :");

add(L2);

T2=new TextField(10);

add(T2);

L3=new Label("result");

add(L3);
Result=new TextField(10);

add(Result);

B1=new Button("Divide");

add(B1);

B1.addActionListener(this);

public void actionPerformed(ActionEvent e) {

if(e.getSource()==B1) {

try {

int value1=Integer.parseInt(T1.getText());

int value2=Integer.parseInt(T2.getText());

int result=value1/value2;

Result.setText(String.valueOf(result));

catch(NumberFormatException nfe) {

JOptionPane.showMessageDialog(this,"Not a number");

catch(ArithmeticException ae) {

JOptionPane.showMessageDialog(this,"Divided by Zero");

F:\javaprograms>javac DivisionApplet.java

F:\javaprograms>appletviewer DivisionApplet.java
Exercise – 9 Write a Java Program That works as a simple calculator using Grid layout to arrange
buttons for the digits and +, -, * % operations. Add a text filed to print the result.

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

import javax.swing.*;

/*

<applet code="MyCalculator" width=300 height=300>

</applet>

*/

public class MyCalculator extends Applet implements ActionListener {

int num1,num2,result;

TextField T1;

Button NumButtons[]=new Button[10];

Button Add,Sub,Mul,Div,clear,EQ;

char Operation;

Panel nPanel,CPanel,SPanel;

public void init() {

nPanel=new Panel();

T1=new TextField(30);

nPanel.setLayout(new FlowLayout(FlowLayout.CENTER));

nPanel.add(T1);

CPanel=new Panel();

CPanel.setBackground(Color.pink);
CPanel.setLayout(new GridLayout(5,5,3,3));

for(int i=0;i<10;i++) {

NumButtons[i]=new Button(""+i);

Add=new Button("+");

Sub=new Button("-");

Mul=new Button("*");

Div=new Button("/");

clear=new Button("clear");

EQ=new Button("=");

T1.addActionListener(this);

for(int i=0;i<10;i++) {

CPanel.add(NumButtons[i]);

CPanel.add(Add);

CPanel.add(Sub);

CPanel.add(Mul);

CPanel.add(Div);

CPanel.add(EQ);

SPanel=new Panel();

SPanel.setLayout(new FlowLayout(FlowLayout.CENTER));

SPanel.setBackground(Color.red);

SPanel.add(clear);

for(int i=0;i<10;i++) {
NumButtons[i].addActionListener(this);

Add.addActionListener(this);

Sub.addActionListener(this);

Mul.addActionListener(this);

Div.addActionListener(this);

clear.addActionListener(this);

EQ.addActionListener(this);

this.setLayout(new BorderLayout());

add(nPanel,BorderLayout.NORTH);

add(CPanel,BorderLayout.CENTER);

add(SPanel,BorderLayout.SOUTH);

public void actionPerformed(ActionEvent ae) {

String str=ae.getActionCommand ();

char ch=str.charAt(0);

if(Character.isDigit(ch))

T1.setText(T1.getText()+str);

else

if(str.equals("+")){

num1=Integer.parseInt (T1.getText());

Operation='+';

T1.setText ("");

}
if(str.equals("-")){

num1=Integer.parseInt(T1.getText());

Operation='-';

T1.setText("");

if(str.equals("*")){

num1=Integer.parseInt(T1.getText());

Operation='*';

T1.setText("");

if(str.equals("/")){

num1=Integer.parseInt(T1.getText());

Operation='/';

T1.setText("");

if(str.equals("%")){

num1=Integer.parseInt(T1.getText());

Operation='%';

T1.setText("");

if(str.equals("=")) {

num2=Integer.parseInt(T1.getText());

switch(Operation)

{
case '+':result=num1+num2;

break;

case '-':result=num1-num2;

break;

case '*':result=num1*num2;

break;

case '/':try {

result=num1/num2;

catch(ArithmeticException e) {

result=num2;

JOptionPane.showMessageDialog(this,"Divided by zero");

break;

T1.setText(""+result);

if(str.equals("clear")) {

T1.setText("");

You might also like