IV SEM SYLLABUS
JAVA Material
DBMS I,II&III UNIT
SQL Material
PL/SQL Material
DBMS Practical Program
JAVA Practical Program
package NumPrograms;
public class GeometricMethodOverloading
{
void calcArea(float a)
{
float area = a * a;
System.out.println("The Area of a Square = " + area + " Sq Units");
}
void calcArea(float a, float b)
{
float area = a * b;
System.out.println("The Area of a Rectangle = " + area + " Sq Units");
}
void calcArea(double r)
{
double area = 3.14 * r * r;
System.out.println("The Area of a Circle = " + area + " Sq Units");
}
void calcArea(double a, double b)
{
double area = (a * b) / 2;
System.out.println("The Area of a Triangle = " + area);
}
public static void main(String[] args)
{
GeometricMethodOverloading obj = new GeometricMethodOverloading();
obj.calcArea(10.6f);
obj.calcArea(14.3f, 22.5f);
obj.calcArea(11.67);
obj.calcArea(18.0, 25.4);
}
}
class NumberPrinter extends Thread {
private int start;
private int increment;
public NumberPrinter(int start, int increment) {
this.start = start;
this.increment = increment;
}
public void run() {
for (int i = start; i <= 5; i += increment) {
System.out.println("Thread " + Thread.currentThread().getId() + ": " + i);
}
}
}
public class Main {
public static void main(String[] args) {
NumberPrinter thread1 = new NumberPrinter(1, 2);
NumberPrinter thread2 = new NumberPrinter(2, 2);
thread1.start();
thread2.start();
}
}
a. // Java Program to Handle Divide By Zero Exception
import java.io.*;
class GFG {
public static void main(String[] args)
{
int a = 6;
int b = 0;
System.out.print(a / b);
// this line Throw ArithmeticException: / by zero
}
}
Output:
C:\demo>javac GFG.java
C:\demo>java GFG
Exception in thread "main" java.lang.ArithmeticException: / by zero
at GFG.main(GFG.java:7)
b. // Java Program to Handle Divide By Zero Exception
import java.io.*;
class GFG {
public static void main(String[] args)
{
int a = 5;
int b = 0;
try {
System.out.println(a / b); // throw Exception
}
catch (ArithmeticException e) {
// Exception handler
System.out.println(
"Divided by zero operation cannot possible");
}
}
}
output:
C:\demo>javac GFG.java
C:\demo>java GFG
Divided by zero operation cannot possible
c. // Java Program to arthimetic exception
import java.io.*;
class GFG {
public static void main(String[] args)
{
try {
int number[] = new int[10];
number[10] = 30 / 9;
}
catch (ArithmeticException e) {
System.out.println(
"Zero cannot divide any number");
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println(
"Index out of size of the array");
}
}
}
output:
C:\demo>javac GFG.java
C:\demo>java GFG
Index out of size of the array
No comments:
Post a Comment