JAVA LAB Prog
JAVA LAB Prog
”
2. Write a program to demonstrate a division by zero exception
3. Write a program to create a user defined exception
4. Write a java program to add two integers and two float numbers. Using function overloading.
5. Write a program to perform mathematical operations. Using Inheritance.
6. Write a java program to create a student class to demonstrate constructor. The pass mark for each subject is 50.
If a candidate fails in any one of the subjects his total mark must be declared as zero
7. Write a program to handle Null Pointer Exception and use the “finally” method to display a message to the user.
8. Write a program to Create a user defined Package in Java.
9. Write a program which create and displays a message on the window.
10. Write a program to draw several shapes in the window.
11. Calculating Area of a Square and Triangle Using Java Interface.
12. Square and Square Root Calculation Using Abstract Class in Java.
13. Using Generics in Java to Handle Different Data Types.
14. Create a frame which displays your personal details with respect to a button click.
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Output
Hello, World!
try {
int result = numerator / denominator;
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
System.out.println("Exception caught: " + e.getMessage());
}
}
}.
Output
Exception caught: / by zero
Output
4. Write a java program to add two integers and two float numbers. Using function overloading.
Output
Sum of integers: 15
Sum of float numbers: 6.2
Output
Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2
import java.util.Scanner;
// Constructor
public Student(int enrollmentId, String name, int sub1Mark, int sub2Mark, int
sub3Mark) {
this.enrollmentId = enrollmentId;
this.name = name;
this.sub1Mark = sub1Mark;
this.sub2Mark = sub2Mark;
this.sub3Mark = sub3Mark;
Output
7. Write a program to handle Null Pointer Exception and use the “finally” method to display a message to the user.
Output
create a simple package called utilities which contains a class MathHelper that provides some basic mathematical functions.
user-defined-package/
│
├── src/
│ └── utilities/
│ └── MathHelper.java
│
└── Main.java
package utilities;
Main.java:
import utilities.MathHelper;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.*;
import java.awt.*;
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
// Draw a rectangle
g.drawRect(50, 50, 100, 50);
// Fill a rectangle
g.fillRect(200, 50, 100, 50);
// Draw an oval
g.setColor(Color.GREEN);
g.drawOval(50, 150, 100, 100);
// Fill an oval
g.setColor(Color.ORANGE);
g.fillOval(200, 150, 100, 100);
// Draw a line
g.setColor(Color.MAGENTA);
g.drawLine(50, 300, 300, 300);
}
frame.setVisible(true);
}
}
System.out.println("Enter base");
Double b=sc.nextDouble();
System.out.println("enter the height");
double h=sc.nextDouble();
double a=obj.areaTriangle(b,h);
System.out.println("area of triangle"+a);
System.out.println("/////////////////");
System.out.println("Ente the base to
calculate area of a square");
Double ba=sc.nextDouble();
Double s=obj.areaSquare(ba);
System.out.println("area of square"+s);
}
@Override
public double areaTriangle(double base,
double height) {
return 0.5*base*height;
}
@Override
public double areaSquare(double base){
return base * base;
}
}
//Area class
// SquareAndSquareRoot
package pack;
// Calculation class
package pack;
import java.util.Scanner;