Вы находитесь на странице: 1из 4

Java is a general-purpose computer-programming language that is concurrent, class-

based, object-oriented, and specifically designed to have as few implementation


dependencies as possible
Question -Read the following and create appropriate java applets.
a. Create a try block that is likely to generate two types of exception and then
incorporate necessary catch block to handle them appropriately
class Excep
{
public static void main(String[] args)
{
try
{
int arr[]={1,2};
arr[2]=3/0;
}
catch(ArithmeticException ae)
{
System.out.println("divide by zero");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("array index out of bound exception");
}
}
}

c) Create the following form in Java:-


Subject should display name of subjects :-c++, VB, Java
Foreign language should display choice of foreign languages French, German and
Japanese.
Submit button should submit the result.
Import java.awt.*
import java.applet.*
import java.awt.event.*
public class student extends Frame implements ActionListener
(
String fin;
Button s = submit(“submit”);
Label l1= new Label(“Subjects”, Label.LEFT);
Label l2 = new Label(“Languages”, Label.LEFT);
Label l3 = new Label(“Student form”, Label.CENTER);
CheckboxGroup sub = new CheckboxGroup( ) ;
Checkbox c1 = new Checkbox(“C++”, false, sub);
Checkbox c2 = new Checkbox(“Java”, false, sub);
Checkbox c3 = new Checkbox(“VB”, false, sub);
TextField t1 = new TextField( );
Choice language = new Choice( );
public void student (){
addWindowListener(new myWindowAdapter());
setLayout(null);
add(c1);
add(c2);
add(c3);
add(l1);
add(l2);
add(t1);
add(language);
s.addActionListener(this);
add(s);
language.add(“French”);
language.add(“German”);
language.add(“Japanese”);
l1.setBounds(25,120,90,20);
c1.setBounds(120,120,50,20);
c2.setBounds(170,120,60,20);
c3.setBounds(220,120,70,20);
l2.setBounds(25, 90, 90, 20);
Languages.setBounds(30, 90, 95,20);
s.setBounds(25, 140, 90, 50, 30);
}
public void paint(Graphics g){
{g.drawString(msg,100,250);}
public void actionPerformed(ActionEvent ae)
if(ae.getActionCommand().equals("submit"))
{fin="submitted!";}
}
public static void main(String g[]) {
student f=new student();
f.setSize(new Dimension(300,300));
f.setTitle("Registration Form");
f.setVisible(true);
}
}
class myWindowAdapter extends WindowAdapter {
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}

b) Create a java applet with a button “Display”. On click of button it should display
Rectangle, Oval and Circle within the Applet.

mport java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class AnAppletWithButtons extends Applet implements ActionListener {

Button drawCircle = new Button("Draw A Circle");


Button drawRectangle = new Button("Draw A Rectangle");
String isCircle="N", isRectangle="N";
String displayString , inputString ;
public void init()
{
add(drawCircle);
add(drawRectangle);
drawCircle.addActionListener(this);
drawRectangle.addActionListener(this);
enterLabel = new Label ( "Enter" ) ;
enterBox = new TextField (12 ) ;
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==drawCircle)
{
isCircle="Y";
isRectangle="N";
}
if (e.getSource()==drawRectangle)
{
isCircle="N";
isRectangle="Y";
}
}
public void paint(Graphics gscreen)
{
if(isCircle=="Y")
{
gscreen.drawString("Display Box for Circle Radius", 30,45);
gscreen.drawOval(85,45,75,75);
add ( enterLabel );
add (enterBox );
}
else if (isRectangle=="Y")
{
gscreen.drawString("Display Box for Rectangle Radius", 30,45);
}
}
}

Вам также может понравиться