Assignment 9
Concept of Execution Handling
A. Write a code segment in Try block where divide by zero occurs, also
   write corresponding catch block to catch the exception that occurs in the
   try block. Print the origin of the exception caught.
Program Code:
public class Assgn9a {
    public static void main(String[] args)
    {
        try
        {
            int r = 10 / 0;
        }
catch (ArithmeticException e)
        {
                       System.out.println(e);
                }
    }
}
Compilation:
>javac Assgn9a.java
Output:
>java Assgn9a
java.lang.ArithmeticException: / by zero
B. Create an array of 10 integers and assign an integer in location 15 of the array. Print
   the appropriate message in catch block. Considering the same assignment write two
   catch block one to catch the “Exception” another for exception
   “ArrayIndexOutofBoundsException”. In first catch block re-throw the exception
   caught. In second catch block print the origin of the exception.
Program Code:
import java.util.Scanner;
class Assgn9b {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int arr[] = new int[10];
try{
System.out.println("Enter the array elements: ");
for(int i=0; i<10; i++){
arr[i] = sc.nextInt();
}
System.out.println("The array is: ");
for(int i=0; i<10; i++){
System.out.println(arr[i]);
}
System.out.println("Enter element at position: ");
int a = sc.nextInt();
System.out.println("The element at position: "+arr[a]);
sc.close();
}
catch(Exception E){
System.out.println();
try{
throw E;
}
catch(ArrayIndexOutOfBoundsException e){
e.printStackTrace();
System.out.println("Entry of element beyond the upperbound is not possible !!!");
}
}
}
}
Compilation: >javac Assgn9b.java
Output: >java Assgn9b
Enter the array elements:
1 2 3 4 5 6 7 8 9 10
The array is:
1
2
3
4
5
6
7
8
9
10
Enter element at position:
15
java.lang.ArrayIndexOutOfBoundsException: Index 15 out of bounds for length 10
     at Assgn9b.main(Assgn9b.java:17)
Entry of element beyond the upperbound is not possible !!!
C. Write a java code segments that results a “NullPointerException”. Write
   a necessary catch block to handle the exception. Also write a finally
   block with appropriate statements in it.
Program Code:
public class Assgn9c {
    public static void main(String[] args) {
        try {
            String str = null; // Null reference
      System.out.println(str.length()); // This will throw
NullPointerException
        } catch (NullPointerException e) {
      System.out.println("Exception caught: Null pointer exception
occurred.");
        } finally {
      System.out.println("This is the finally block, which always
executes.");
        }
    }
}
Compilation:
>javac Assgn9c.java
Output:
>java Assgn9c
Exception caught: Null pointer exception occurred.
This is the finally block, which always executes.
D. Create a superclass Mathexception and two subclasses
   Overflowexception and UnderflowException. Write a code segment that
   throws an Overflowexception. Write three catch block one for
   Mathexception and others are for Overflowexception and
   UnderflowException. In first catch block re-throw the exception caught
   in other two catch blocks, write appropriate message to handle it and
   show the results. Instantiate an integer variable and initialize with some
   value. If the value is greater than 100 an OverFlowException is thrown
   otherwise an UnderFlowException is thrown, handle the exception with
   appropriate message.
Program Code:
import java.util.Scanner;
class MathException extends Exception{
    MathException(String str){
          System.out.println("Mathexception: "+str);
    }
}
class OverflowException extends MathException{
    OverflowException(String str){
          super(str);
          System.out.println("OverflowException: "+str);
    }
}
class UnderflowException extends MathException{
    UnderflowException(String str){
          super(str);
          System.out.println("UnderflowException: "+str);
    }
}
class Assgn9d {
   public static void main(String args[]){
          Scanner sc = new Scanner(System.in);
          System.out.println("Enter a value: ");
          int n = sc.nextInt();
          try{
                  if(n > 100){
                          try{
         throw new OverflowException("The given number is greater
than100.");
                          }
                          catch(OverflowException e1){
                          throw e1;
                          }
                  }
                  else{
                          try{
          throw new UnderflowException("The given number is less than
100.");
                          }
                          catch(UnderflowException e2){
                                  throw e2;
                          }
                  }
          }
          catch(Exception e){
                  System.out.println("Exception: "+e);
         }
     }
}
Compilation: >javac Assgn9d.java
Output: >java Assgn9d
➢ Enter a value:
110
Mathexception: The given number is greater than 100.
OverflowException: The given number is greater than 100.
Exception: OverflowException
➢ Enter a value:
97
Mathexception: The given number is less than 100.
UnderflowException: The given number is less than 100.
Exception: UnderflowException
                                            Assignment 12
                                         Concept of Applet
    A. Create an applet to draw a smiling face.
    Program Code:
    import java.awt.*;
    import java.applet.*;
    public class Face extends Applet {
       public void paint(Graphics g) {
           g.drawOval(40, 40, 120, 150);// Head
           g.drawOval(57, 75, 30, 20);// Left Eye
           g.drawOval(110, 75, 30, 20);// Left Eye
           g.fillOval(68, 81, 10, 10);// Pupil left
           g.fillOval(121, 81, 10, 10);// Pupil right
           g.drawOval(85, 100, 30, 30);// Nose
           g.fillArc(60, 125, 80, 40, 180, 180);// Mouth
           g.drawOval(25, 92, 15, 30);// Left Ear
            g.drawOval(160, 92, 15, 30);// Right Ear
           /*<applet code=Face.class width=300 height=300>
           </applet>*/
       }
}
Execution: javac Face.java
Appletviewer Face.java
Output:
B. Create another applet to draw a house whose door will open and close
   at 1 second interval.
Program Code:
import java.applet.Applet;
import java.awt.*;
public class House extends Applet implements Runnable {
  boolean flag;
  Thread t;
  public void start() {
    t = new Thread(this);
    t.start();
    flag = true;
  } // End of start()
  public void run() {
    while (true) {
       try {
           if (flag)
               flag = false;
           else
               flag = true;
           t.sleep(1000);
           repaint();
       } // End of try block
       catch (Exception e) {
       }
    } // End of While
  } // End of run()
public void paint(Graphics g) {
  /* for house */
  int x[] = {300, 400, 500};
  int y[] = {300, 200, 300};
  g.drawPolygon(x, y, 3);
  g.setColor(new Color(150, 150, 150));
  g.fillPolygon(x, y, 3);
  g.drawRect(300, 300, 200, 100);
  g.setColor(Color.yellow);
  g.fillRect(300, 300, 200, 100);
  if (flag) {
    // for Closed door
    g.drawRect(375, 350, 50, 50); // draw rectangle
    g.setColor(new Color(120, 0, 0)); // boundary color of rectangle
    g.fillRect(375, 350, 50, 50); // fill rectangle
    g.setColor(Color.black); // fill with black color
    g.drawLine(400, 350, 400, 400); // draw line within rectangle
  } // End of if
  else {
    // for open door
    g.drawRect(375, 350, 50, 50);
    g.setColor(Color.black);
    g.fillRect(375, 350, 50, 50); // for empty space between open doors
    int x1[] = {375, 390, 390, 375, 375};
    int y1[] = {350, 360, 390, 400, 350};
    g.setColor(new Color(120, 0, 0));
      g.fillPolygon(x1, y1, 5); // for left door
      int x2[] = {425, 410, 410, 425, 425};
      int y2[] = {350, 360, 390, 400, 350};
      g.setColor(new Color(120, 0, 0)); // for right door
      g.fillPolygon(x2, y2, 5);
    } // End of else
/* <applet code=”House” width=”700” height=”700”>
</applet>*/
  } // End of paint
} // End of House
Execution:
javac House.java
appletviewer House.java
Output: