Index Java 1.1.0
Index Java 1.1.0
Index Java 1.1.0
INDEX
18. Create a custom exception and throw in case of age<18 for voting.
import java.util.Scanner;
if (n == 0) {
return 1;
} else if (n < 0) {
} else {
}}
try {
} catch (IllegalArgumentException e) {
}
Q-3Write a java program to find product of two matrices in java?
public class MatrixProduct {
int[][] matrixA = {
{1, 2, 3},
{4, 5, 6}
};
int[][] matrixB = {
{7, 8},
{9, 10},
{11, 12}
};
if (matrixA[0].length != matrixB.length) {
+ matrixA[0].length + ") must equal number of rows in second matrix (" + matrixB.length
+ ").");
return;
System.out.println("Matrix A:");
printMatrix(matrixA);
System.out.println("Matrix B:");
printMatrix(matrixB);
System.out.println("Product of matrices:");
printMatrix(product);
System.out.println();
}
Q-4 Write a program to print fibnonic series in java ?
public class FibonacciRecursion {
System.out.println();
}
for (int i = midRow - 1; i >= 1; i--) {
for (int j = 1; j <= midRow - i; j++) {
System.out.print(" ");
}
System.out.println();
}
}
}
Q-6Write a program to implement static variable in java.?
Employee emp1 = new Employee("Alice", 101);
emp1.display();
emp2.display();
emp1.display();
emp2.display();
OUTPUT
Name: Alice
ID: 101
Company: ABC Inc.
Name: Bob
ID: 102
Company: ABC Inc.
Name: Alice
ID: 101
Company: XYZ Ltd.
Name: Bob
ID: 102
Company: XYZ Ltd.
Q-7 Write a program to implement constructors in java?
// Define a class
class Car {
String make;
String model;
int year;
System.out.println("Car 1:");
car1.displayInfo();
System.out.println("\nCar 2:");
car2.displayInfo();
}
}
Q-8Create a user defined package and use their classes in java.?
└── example
└── mypackage
└── Calculator.java
package com.example.mypackage;
return a + b;
return a - b;
}
// Main.java
import com.example.mypackage.Calculator;
public class Main {
public static void main(String[] args) {
Calculator calculator = new Calculator();
int result1 = calculator.add(5, 3);
System.out.println("5 + 3 = " + result1);
int result2 = calculator.subtract(10, 4);
System.out.println("10 - 4 = " + result2);
}
}
OUTPUT
5+3=8
10 - 4 = 6
Q-9 Write a program to implement simple inheritance in java.?
// Parent class
class Vehicle {
// Me String brand;
int year;
public Vehicle(String brand, int year) {
this.brand = brand;
this.year = year;
public void displayInfo() {
System.out.println("Brand: " + brand);
System.out.println("Year: " + year);
}
}
class Car extends Vehicle {
int mileage;
public Car(String brand, int year, int mileage) {
super(brand, year);
this.mileage = mileage;
}
public void displayCarInfo() {
displayInfo();
System.out.println("Mileage: " + mileage);
}
}
public class Main {
public static void main(String[] args) {
// Create a Car object
Car myCar = new Car("Toyota", 2020, 50000);
System.out.println("Car Information:");
myCar.displayCarInfo();
}
}
Q-10Write a program to implement Hierarchical inheritance in java?
// Parent class
class Vehicle {
String brand;
int year;
this.brand = brand;
this.year = year;
int mileage;
super(brand, year);
this.mileage = mileage;
displayInfo();
int capacity;
super(brand, year);
this.capacity = capacity;
}
public void displayTruckInfo() {
displayInfo();
System.out.println("Car Information:");
myCar.displayCarInfo();
System.out.println("\nTruck Information:");
myTruck.displayTruckInfo();
}
Q-11 Write a program to implement method overloading in java.?
public class OverloadingExample {
public int add(int a, int b) {
return a + b;
}
public int add(int a, int b, int c) {
return a + b + c;
}
}
Q-12 Write a program to implement method overriding in java.?
// Parent class
class Animal {
@Override
System.out.println("Dog barks"); }}
System.out.println("Cat meows"); }}
System.out.print("Dog: ");
myDog.makeSound();
System.out.print("Cat: ");
myCat.makeSound();
}
Q-13Write a program to implement dynamic method dispatch in java.?
// Parent class
class Animal {
@Override
System.out.println("Dog barks");}}
System.out.print("Dog: ");
myDog.makeSound();
System.out.print("Cat: ");
myCat.makeSound();}}
Q-14 Write a program to implement abstract class in java.?
// Abstract class
this.radius = radius;
}@Override
}}
this.length = length;
this.width = width;
}}
Q-15 Write a program to implement interfaces in java.?
interface Shape {
double radius;
this.radius = radius;
@Override
@Override
double length;
double width;
this.length = length;
this.width = width;
@Override
} @Override
public double calculatePerimeter() {
// Main class
}
Q-16Write a simple Multithread program in java.?
// Define a class that implements the Runnable interface
try {
} catch (InterruptedException e) {
e.printStackTrace();
}}}}
thread1.start();
thread2.start();
}
Q-17Write a program to handle try and multiple catch block.?
public class Main {
try {
} catch (ArithmeticException e) {
} catch (ArrayIndexOutOfBoundsException e) {
} catch (Exception e) {
return a / b;
}
Q-18Create a custom exception and throw in case of age<18 for voting.?
// Custom exception class for invalid age
super(message);}}
} else {
try {
} catch (InvalidAgeException e) {
}
Q-19Create a student table using <TABLE> tag in html.?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Student Table</title>
<style>
table {
width: 100%;
border-collapse: collapse;
th, td {
padding: 8px;
text-align: left;
th {
background-color: #f2f2f2;
</style>
</head>
<body>
<h2>Student Table</h2>
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
<th>Grade</th>
</tr>
<tr>
<td>1</td>
<td>John Doe</td>
<td>18</td>
<td>12</td>
</tr>
<tr>
<td>2</td>
<td>Jane Smith</td>
<td>17</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>Michael Johnson</td>
<td>16</td>
<td>10</td>
</tr>
</table>
</body>
</html>
Q-20 Write a program to draw different shapes using graphics methods.?
import javax.swing.*;
import java.awt.*;
public DrawShapes() {
setTitle("Drawing Shapes");
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
} @Override
super.paint(g);
g.setColor(Color.BLUE);
g.setColor(Color.RED);
g.setColor(Color.GREEN);
g.setColor(Color.MAGENTA);
g.setColor(Color.ORANGE);
new DrawShapes();
}}
Q-21 Create a login form in AWT.?
import java.awt.*;
import java.awt.event.*;
public LoginForm() {
setLayout(new FlowLayout());
add(new Label("Username:"));
usernameField = new TextField(20);
add(usernameField);
add(new Label("Password:"));
passwordField = new TextField(20);
passwordField.setEchoChar('*');
add(passwordField);
loginButton = new Button("Login");
add(loginButton);
messageLabel = new Label();
add(messageLabel);
loginButton.addActionListener(this);
setTitle("Login Form");
setSize(300, 150);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String username = usernameField.getText();
String password = passwordField.getText();
}
Q-22 Create a calculator using event listener in java.?
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public GUICalculator() {
JPanel panel1 = new JPanel();
button1 = new JButton("+");
button2 = new JButton("-");
button3 = new JButton("*");
button4 = new JButton("/");
textField1 = new JTextField(20);
textField2 = new JTextField(20);
textField3 = new JTextField(20);
panel1.add(textField1);
panel1.add(textField2);
panel1.add(textField3);
panel1.add(button1);
panel1.add(button2);
panel1.add(button3);
panel1.add(button4);
this.add(panel1);
}
try {
number1 = Double.parseDouble(textField1.getText());
number2 = Double.parseDouble(textField2.getText());
} catch (Exception error) {
error.printStackTrace();
}
if (e.getSource() == button1) {
textField3.setText(number1 + number2 + "");
} else if (e.getSource() == button2) {
textField3.setText(number1 - number2 + "");
} else if (e.getSource() == button3) {
textField3.setText(number1 * number2 + "");
} else if (e.getSource() == button4) {
textField3.setText(number1 / number2 + "");
}
}
}
}