0% found this document useful (0 votes)
192 views11 pages

Java Lab-Exercise - 8 & 9

Uploaded by

maneeshgopisetty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
192 views11 pages

Java Lab-Exercise - 8 & 9

Uploaded by

maneeshgopisetty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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 [Link].*;

import [Link].*;

import [Link].*;

import [Link].*;

/*<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);

[Link](this);

public void actionPerformed(ActionEvent e) {

if([Link]()==B1) {

try {

int value1=[Link]([Link]());

int value2=[Link]([Link]());

int result=value1/value2;

[Link]([Link](result));

catch(NumberFormatException nfe) {

[Link](this,"Not a number");

catch(ArithmeticException ae) {

[Link](this,"Divided by Zero");

F:\javaprograms>javac [Link]

F:\javaprograms>appletviewer [Link]
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 [Link].*;

import [Link].*;

import [Link].*;

import [Link].*;

/*

<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);

[Link](new FlowLayout([Link]));

[Link](T1);

CPanel=new Panel();

[Link]([Link]);
[Link](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("=");

[Link](this);

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

[Link](NumButtons[i]);

[Link](Add);

[Link](Sub);

[Link](Mul);

[Link](Div);

[Link](EQ);

SPanel=new Panel();

[Link](new FlowLayout([Link]));

[Link]([Link]);

[Link](clear);

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

[Link](this);

[Link](this);

[Link](this);

[Link](this);

[Link](this);

[Link](this);

[Link](new BorderLayout());

add(nPanel,[Link]);

add(CPanel,[Link]);

add(SPanel,[Link]);

public void actionPerformed(ActionEvent ae) {

String str=[Link] ();

char ch=[Link](0);

if([Link](ch))

[Link]([Link]()+str);

else

if([Link]("+")){

num1=[Link] ([Link]());

Operation='+';

[Link] ("");

}
if([Link]("-")){

num1=[Link]([Link]());

Operation='-';

[Link]("");

if([Link]("*")){

num1=[Link]([Link]());

Operation='*';

[Link]("");

if([Link]("/")){

num1=[Link]([Link]());

Operation='/';

[Link]("");

if([Link]("%")){

num1=[Link]([Link]());

Operation='%';

[Link]("");

if([Link]("=")) {

num2=[Link]([Link]());

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;

[Link](this,"Divided by zero");

break;

[Link](""+result);

if([Link]("clear")) {

[Link]("");

You might also like