JavaPractical Merged Removed
JavaPractical Merged Removed
A practical file of
DBMS
Subject Code: BCA-506
18. Create a custom exception and throw in case of age<18 for voting.
scanner.close();
}
int count = 0;
while (number > 0) {
number = number / 10;
count++;
}
return count;
}
}
OUTPUT:-
Q2:-Write a program to find the factorial of a number using recursion.
Ans:- import java.util.Scanner;
if (number < 0) {
System.out.println("Factorial is not defined for negative numbers.");
} else {
long factorial = calculateFactorial(number);
System.out.println("Factorial of " + number + " is: " + factorial);
}
scanner.close();
}
OUTPUT:-
Q3:- Write a java program to find product of two matrices in java.
Ans:- import java.util.Scanner;
System.out.println("Fibonacci Series:");
for (int i = 0; i < n; i++) {
System.out.print(fibonacci(i) + " ");
}
scanner.close();
}
OUTPUT:-
Q5:- Write a program to implement static variable in java.
Ans:- public class StaticVariableExample {
// Static variable
static int count = 0;
public StaticVariableExample() {
// Increment the count each time an object is created
count++;
}
OUTPUT:-
Q6:- Write a program to print diamond pattern in java.
Ans:- import java.util.Scanner;
if (n % 2 == 0) {
System.out.println("Please enter an odd number.");
return;
}
int spaces = n / 2;
int stars = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= spaces; j++) {
System.out.print(" ");
}
for (int j = 1; j <= stars; j++) {
System.out.print("*");
}
System.out.println();
if (i <= n / 2) {
spaces--;
stars += 2;
} else {
spaces++;
stars -= 2;
}
}
scanner.close();
}
}
OUTPUT:-
Q7:-Write a program to implement constructors in java.
Ans:- public class Car {
private String make;
private String model;
private int year;
// Default constructor
public Car() {
make = "Toyota";
model = "Corolla";
year = 2023;
}
// Parameterized constructor
public Car(String make, String model, int year) {
this.make = make;
this.model = model;
this.year = year;
}
OUTPUT:-
Q8:-Create a user defined package and use their classes in java.
Ans:- package myPackage;
package myPackage;
OUTPUT:-
Q9:-Write a program to implement simple inheritance in java.
Ans:- // Parent class
class Shape {
// Method to calculate area
double calculateArea() {
return 0;
}
}
OUTPUT:-
Q10:-Write a program to implement Hierarchical inheritance in java.
Ans:- // Parent class
class Employee {
String name;
double salary;
void displayDetails() {
System.out.println("Name: " + name);
System.out.println("Salary: $" + salary);}}
class Manager extends Employee {
String department;
OUTPUT:-
Q11:- Write a program to implement method overloading in java.
Ans:- public class MethodOverloadingExample {
OUTPUT:-
Q12:-Write a program to implement method overriding in java.
Ans:- class Shape {
double area() {
return 0;}}
Circle(double radius) {
this.radius = radius;
}
double area() {
return Math.PI * radius * radius;
}
}
OUTPUT:-
Q13:-Write a program to implement dynamic method dispatch in java.
Ans:- // Superclass
class Shape {
void draw() {
System.out.println("Drawing a shape");
}
}
void draw() {
System.out.println("Drawing a triangle");
}
}
public class DynamicMethodDispatch {
public static void main(String[] args) {
Shape shape1 = new Circle(); // Circle object
Shape shape2 = new Rectangle(); // Rectangle object
Shape shape3 = new Triangle(); // Triangle object
OUTPUT:-
Q14:- Write a program to implement abstract class in java.
Ans:-abstract class Shape {
abstract double area();
abstract double perimeter();
}
class Circle extends Shape {
private double radius;
Circle(double radius) {
this.radius = radius;
}
double area() {
return Math.PI * radius * radius;
}
double perimeter() {
return 2 * Math.PI * radius;
}
}
class Rectangle extends Shape {
private double length;
private double width;
System.out.println("Circle:");
System.out.println("Area: " + circle.area());
System.out.println("Perimeter: " + circle.perimeter());
System.out.println("\nRectangle:");
System.out.println("Area: " + rectangle.area());
System.out.println("Perimeter: " + rectangle.perimeter());
}
}
OUTPUT:-
Q15:-Write a program to implement interfaces in java.
Ans:- interface ElectronicDevice {
void turnOn();
void turnOff();
}
interface Connectable {
void connectToInternet();{
class Smartphone implements ElectronicDevice, Connectable {
public void turnOn() {
System.out.println("Smartphone turned on");
}
public void turnOff() {
System.out.println("Smartphone turned off");
}
public void connectToInternet() {
System.out.println("Smartphone connected to the internet");
}
}
class Laptop implements ElectronicDevice, Connectable {
public void turnOn() {
System.out.println("Laptop turned on");
}
public void turnOff() {
System.out.println("Laptop turned off");
} public void connectToInternet() {
System.out.println("Laptop connected to the internet");
}
}class Smartwatch implements ElectronicDevice {
public void turnOn() {
System.out.println("Smartwatch turned on");
}
public void turnOff() {
System.out.println("Smartwatch turned off");
}
}
public class RealLifeExample {
public static void main(String[] args) {
Smartphone smartphone = new Smartphone();
Laptop laptop = new Laptop();
Smartwatch smartwatch = new Smartwatch();
smartphone.turnOn();
smartphone.connectToInternet();
laptop.turnOn();
laptop.connectToInternet();
smartwatch.turnOn();
}
}
OUTPUT:-
Q16:- Write a simple Multithread program in java.
Ans:-
class MyThread extends Thread {
// Override run method to define the task to be performed by the thread
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println("Thread: " + Thread.currentThread().getId() + " Count: " + i);
try {
// Adding a short delay for demonstration purposes
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
}
// Main class
public class MultiThreadExample {
public static void main(String[] args) {
// Creating and starting multiple threads
MyThread thread1 = new MyThread();
MyThread thread2 = new MyThread();
// Starting threads
thread1.start();
thread2.start();
}
}
OUTPUT:-
Q17:-Write a program to handle try and multiple catch block.
Ans:-
public class ExceptionHandlingExample {
public static void main(String[] args) {
try {
// Attempting to perform division by zero
int result = 10 / 0;
System.out.println("Result: " + result); // This line will not be reached
} catch (ArithmeticException e) {
// Catching ArithmeticException which occurs when dividing by zero
System.out.println("ArithmeticException caught: " + e.getMessage());
} catch (Exception e) {
// Catching any other exception
System.out.println("Exception caught: " + e.getMessage());
} finally {
// The code inside the finally block always executes, regardless of whether an exception occurred or not
System.out.println("Finally block executed");
}
OUTPUT:-
Q18:-Create a custom exception and throw in case of age<18 for voting.
Ans:-
class UnderageVotingException extends Exception {
public UnderageVotingException(String message) {
super(message);
}
}
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void vote() throws UnderageVotingException {
if (age < 18) {
throw new UnderageVotingException("Sorry, " + name + ". You are not eligible for voting as your age is
below 18.");
} else {
System.out.println(name + ", you are eligible for voting.");
}
}
}
public class CustomExceptionExample {
public static void main(String[] args) {
// Creating a Person object
Person person1 = new Person("Sachin", 20);
Person person2 = new Person("Nikhil", 16);
try {
// Calling the vote method for person1
person1.vote();
// Calling the vote method for person2
person2.vote();
} catch (UnderageVotingException e) {
// Catching the custom exception and printing the error message
System.out.println("Error: " + e.getMessage());
}
}
}
OUTPUT:-
Q19:-Create a student table using <TABLE> tag in html.
Ans:-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Student Table</title>
</head>
<body>
<h2>Student Table</h2>
<table border="1">
<tr>
<th>Student ID</th>
<th>Name</th>
<th>Age</th>
<th>Grade</th>
</tr>
<tr>
<td>060</td>
<td>Sachin</td>
<td>20</td>
<td>A</td>
</tr>
<tr>
<td>062</td>
<td>Sanjay</td>
<td>21</td>
<td>B+</td>
</tr>
<tr>
<td>059</td>
<td>Kunal</td>
<td>21</td>
<td>A-</td>
</tr>
<tr>
<td>061</td>
<td>Yash</td>
<td>21</td>
<td>C</td>
</tr>
</table>
</body>
</html>
OUTPUT:-
Q20:- Write a program to draw different shapes using graphics methods.
Ans:-//ELLIPSE
import java.awt.*;
import javax.swing.*;
public class ellipse extends JApplet {
public void init()
{
// set size
setSize(400, 400);
repaint();
}
public void paint(Graphics g)
{
// set Color for rectangle
g.setColor(Color.red);
// draw a ellipse
g.drawOval(100, 100, 150, 100);
}
}
OUTPUT:-
Q(B) DrawRect
import java.awt.*;
import javax.swing.*;
repaint();
}
// draw a rectangle
g.drawRect(100, 100, 200, 200);
}
}
OUTOUT:-
Q21:-Create a login form in AWT.
Ans:-
import java.awt.*;
import java.awt.event.*;
setTitle("Login Form");
setSize(300, 150);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose(); // Close the window
public void actionPerformed(ActionEvent e) {
String username = usernameField.getText();})}
String password = passwordField.getText();
if (username.equals("admin") && password.equals("password")) {
System.out.println("Login successful!");
} else {
System.out.println("Invalid username or password. Please try again.");}}
// Constructor
public LoginForm() {
// Set layout
setLayout(new FlowLayout());
// Initialize components
usernameLabel = new Label("Username:");
add(usernameLabel);
// ActionListener implementation
public void actionPerformed(ActionEvent e) {
String username = usernameField.getText();
String password = passwordField.getText();
// Main method
public static void main(String[] args) {
new LoginForm();
}
}
OUTPUT:-