2.
write a program to accept marks and find grades using if statement
import [Link];
public class GradeCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter your marks: ");
int marks = [Link]();
char grade;
if (marks >= 90) {
grade = 'A';
} else if (marks >= 80) {
grade = 'B';
} else if (marks >= 70) {
grade = 'C';
} else if (marks >= 60) {
grade = 'D';
} else {
grade = 'F';
[Link]("Your grade is: " + grade);
[Link]();
}
5. write a program to accept and check it is vowel or consonant using switch case statement.
import [Link];
public class VowelConsonantChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a letter: ");
char letter = [Link]().charAt(0);
switch ([Link](letter)) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
[Link](letter + " is a vowel.");
break;
default:
if ([Link](letter)) {
[Link](letter + " is a consonant.");
} else {
[Link]("Invalid input. Please enter a letter.");
break;
[Link]();
}
6. Write a program to copy all the elements of one array to another array.
# Copying Elements from One Array to Another
public class ArrayCopy {
public static void main(String[] args) {
int[] sourceArray = {1, 2, 3, 4, 5};
int[] destinationArray = new int[[Link]];
for (int i = 0; i < [Link]; i++) {
destinationArray[i] = sourceArray[i];
// Displaying the destination array
for (int element : destinationArray) {
[Link](element + " ");
}
[Link] a program to print all the Armstrong numbers from 0 to 999.
public class ArmstrongNumbers {
public static void main(String[] args) {
[Link]("Armstrong numbers from 0 to 999:");
for (int number = 0; number <= 999; number++) {
int sum = 0;
int temp = number;
int digits = [Link](number).length();
while (temp != 0) {
int digit = temp % 10;
sum += [Link](digit, digits);
temp /= 10;
if (sum == number) {
[Link](number);
}
12. Check entered string is palindrome or not.
import [Link];
public class PalindromeChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String input = [Link]();
String reversed = new StringBuilder(input).reverse().toString();
if ([Link](reversed)) {
[Link](input + " is a palindrome.");
} else {
[Link](input + " is not a palindrome.");
[Link]();
}
3. Write a program to print the sum, difference and product of two complex numbers by creating a class
named "Complex" with separate methods for each operation whose real and imaginary parts are
entered by user.
import [Link];
class Complex {
int real, imag;
Complex(int r, int i) {
real = r;
imag = i;
Complex add(Complex c) {
return new Complex([Link] + [Link], [Link] + [Link]);
Complex subtract(Complex c) {
return new Complex([Link] - [Link], [Link] - [Link]);
Complex multiply(Complex c) {
int realPart = [Link] * [Link] - [Link] * [Link];
int imagPart = [Link] * [Link] + [Link] * [Link];
return new Complex(realPart, imagPart);
void display() {
[Link](real + " + " + imag + "i");
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter real part of first complex number: ");
int r1 = [Link]();
[Link]("Enter imaginary part of first complex number: ");
int i1 = [Link]();
[Link]("Enter real part of second complex number: ");
int r2 = [Link]();
[Link]("Enter imaginary part of second complex number: ");
int i2 = [Link]();
Complex c1 = new Complex(r1, i1);
Complex c2 = new Complex(r2, i2);
[Link]("Sum: ");
[Link](c2).display();
[Link]("Difference: ");
[Link](c2).display();
[Link]("Product: ");
[Link](c2).display();
}
5)Write a single program to implement inheritance and polymorphism in java.
class Animal {
void sound() {
[Link]("Animal makes a sound");
class Dog extends Animal {
void sound() {
[Link]("Dog barks");
class Cat extends Animal {
void sound() {
[Link]("Cat meows");
public class Main {
public static void main(String[] args) {
Animal myDog = new Dog();
Animal myCat = new Cat();
[Link]();
[Link]();
}
4. Develop a program to find area of rectangle and circle using interfaces.
# Area Calculation Using Interfaces
interface Shape {
double area();
class Rectangle implements Shape {
private double length;
private double width;
public Rectangle(double length, double width) {
[Link] = length;
[Link] = width;
@Override
public double area() {
return length * width;
class Circle implements Shape {
private double radius;
public Circle(double radius) {
[Link] = radius;
}
@Override
public double area() {
return [Link] * radius * radius;
public class Main {
public static void main(String[] args) {
Shape rectangle = new Rectangle(5, 3);
Shape circle = new Circle(4);
[Link]("Area of Rectangle: " + [Link]());
[Link]("Area of Circle: " + [Link]());
}
8. Develop a program to implement the multilevel inheritance.
class Animal {
void eat() {
[Link]("Animal is eating");
class Mammal extends Animal {
void walk() {
[Link]("Mammal is walking");
class Dog extends Mammal {
void bark() {
[Link]("Dog is barking");
public class MultilevelInheritance {
public static void main(String[] args) {
Dog dog = new Dog();
[Link]();
[Link]();
[Link]();
}
1. Develop an Interest Interface which contains simple interest and compound interest methods
and static final field of rate25%. Write a class to implement those methods.
# Interest Interface and Implementation
public interface Interest {
double RATE = 0.25; // 25% interest rate
double simpleInterest(double principal, double time);
double compoundInterest(double principal, double time, int frequency);
public class InterestCalculator implements Interest {
@Override
public double simpleInterest(double principal, double time) {
return principal * RATE * time;
@Override
public double compoundInterest(double principal, double time, int frequency) {
return principal * [Link]((1 + RATE / frequency), frequency * time) - principal;
}
1. Write a program to implement following inheritance.
interface Exam {
int sports_marks = 20;
class Student implements Exam {
int roll_no;
String s_name;
int m1, m2, m3;
Student(int roll_no, String s_name, int m1, int m2, int m3) {
this.roll_no = roll_no;
this.s_name = s_name;
this.m1 = m1;
this.m2 = m2;
this.m3 = m3;
class Result extends Student {
Result(int roll_no, String s_name, int m1, int m2, int m3) {
super(roll_no, s_name, m1, m2, m3);
}
void display() {
[Link]("Roll No: " + roll_no);
[Link]("Name: " + s_name);
[Link]("Marks in Subject 1: " + m1);
[Link]("Marks in Subject 2: " + m2);
[Link]("Marks in Subject 3: " + m3);
[Link]("Sports Marks: " + sports_marks);
}
interface Salary {
double basic_sal();
class Employee implements Salary {
String name;
int age;
Employee(String name, int age) {
[Link] = name;
[Link] = age;
void display() {
[Link]("Name: " + name + ", Age: " + age);
public double basic_sal() {
return 30000; // Example basic salary
}
2. Write a program to create user defined exception in java.
// Custom Exception Class
class MyCustomException extends Exception {
public MyCustomException(String message) {
super(message);
}
}
// Main Class
public class UserDefinedExceptionExample {
public static void main(String[] args) {
try {
throw new MyCustomException("This is a user-defined exception.");
} catch (MyCustomException e) {
[Link]("Caught Exception: " + [Link]());
}
}
}
[Link] a java program in which Thread A will display the even number between 1 toCOM 50 and thread
B will display the odd numbers between 1 to 50. After 3rd jterati thread A should go to sleep for 500ms.
public class EvenOddThreads {
public static void main(String[] args) {
Thread threadA = new Thread(new EvenNumbers());
Thread threadB = new Thread(new OddNumbers());
[Link]();
[Link]();
class EvenNumbers implements Runnable {
public void run() {
for (int i = 1; i <= 50; i++) {
if (i % 2 == 0) {
[Link]("Even: " + i);
if (i / 2 == 3) {
try {
[Link](500);
} catch (InterruptedException e) {
[Link]();
}
class OddNumbers implements Runnable {
public void run() {
for (int i = 1; i <= 50; i++) {
if (i % 2 != 0) {
[Link]("Odd: " + i);
[Link] a program to accept a password from the user and throw "Authentication Failure"
import [Link];
public class PasswordAuthentication {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter your password: ");
String password = [Link]();
if () {
throw new RuntimeException("Authentication Failure");
} else {
[Link]("Authentication Successful");
[Link]();
}
[Link] a program to create two thread one to print odd number only and other to print even numbers.
class OddNumberPrinter extends Thread {
public void run() {
for (int i = 1; i <= 20; i += 2) {
[Link]("Odd: " + i);
try {
[Link](100);
} catch (InterruptedException e) {
[Link]();
class EvenNumberPrinter extends Thread {
public void run() {
for (int i = 0; i <= 20; i += 2) {
[Link]("Even: " + i);
try {
[Link](100);
} catch (InterruptedException e) {
[Link]();
public class OddEvenThreadExample {
public static void main(String[] args) {
OddNumberPrinter oddThread = new OddNumberPrinter();
EvenNumberPrinter evenThread = new EvenNumberPrinter();
[Link]();
[Link]();
}
[Link] an Exception called "NotMatchException" that is thrown when a password is not equal to
"MSBTE".Write the program.
public class NotMatchException extends Exception {
public NotMatchException(String message) {
super(message);
public class PasswordValidator {
private static final String VALID_PASSWORD = "MSBTE";
public void validatePassword(String password) throws NotMatchException {
if (!VALID_PASSWORD.equals(password)) {
throw new NotMatchException("Password does not match.");
public static void main(String[] args) {
PasswordValidator validator = new PasswordValidator();
String passwordToCheck = "wrongPassword";
try {
[Link](passwordToCheck);
} catch (NotMatchException e) {
[Link]([Link]());
}
[Link] an application to create from using Textfield, textarea,Button and Lable.
import [Link].*;
import [Link].*;
import [Link];
import [Link];
public class SimpleApplication {
public static void main(String[] args) {
JFrame frame = new JFrame("Simple Application");
[Link](JFrame.EXIT_ON_CLOSE);
[Link](400, 300);
[Link](new FlowLayout());
JLabel label = new JLabel("Enter Text:");
JTextField textField = new JTextField(20);
JTextArea textArea = new JTextArea(5, 20);
JButton button = new JButton("Submit");
[Link](new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String inputText = [Link]();
[Link](inputText + "\n");
[Link]("");
});
[Link](label);
[Link](textField);
[Link](button);
[Link](new JScrollPane(textArea));
[Link](true);
[Link] to create three Button with Caption, OK, RESET and Cancel.
import [Link];
import [Link];
public class ButtonExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Button Example");
[Link](JFrame.EXIT_ON_CLOSE);
[Link](300, 200);
[Link](null);
JButton okButton = new JButton("OK");
[Link](50, 50, 80, 30);
[Link](okButton);
JButton resetButton = new JButton("RESET");
[Link](150, 50, 80, 30);
[Link](resetButton);
JButton cancelButton = new JButton("Cancel");
[Link](100, 100, 80, 30);
[Link](cancelButton);
[Link](true);
}
[Link] a program to demonstrate the use of keyEvent when key is pressed and display "keypressed"
message.
import [Link].*;
import [Link];
import [Link];
public class KeyEventDemo extends JFrame implements KeyListener {
public KeyEventDemo() {
setTitle("KeyEvent Demo");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addKeyListener(this);
setVisible(true);
public void keyPressed(KeyEvent e) {
[Link]("Key Pressed: " + [Link]());
public void keyReleased(KeyEvent e) {
// Not used
public void keyTyped(KeyEvent e) {
// Not used
public static void main(String[] args) {
new KeyEventDemo();
}
5. Write a program to display The Number on Button from 0 to 9 using FlowLayout.
import [Link];
import [Link];
import [Link];
public class NumberButtons {
public static void main(String[] args) {
JFrame frame = new JFrame("Number Buttons");
[Link](new FlowLayout());
for (int i = 0; i < 10; i++) {
JButton button = new JButton([Link](i));
[Link](button);
[Link](300, 100);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
Button output:
import [Link].*;
import [Link].*;
class App {
public static void main(String[] args) {
JFrame frame = new JFrame("GridLayout Demo");
[Link](JFrame.EXIT_ON_CLOSE);
[Link](new GridLayout(3, 2, 5, 5));
JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
JButton button3 = new JButton("Button 3");
JButton button4 = new JButton("Button 4");
JButton button5 = new JButton("Button 5");
[Link](button1);
[Link](button2);
[Link](button3);
[Link](button4);
[Link](button5);
[Link](300, 200);
[Link](true);
}
8. WAP which create a menubar with different colour.
import [Link].*;
import [Link].*;
public class MenuBarExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Menu Bar Example");
[Link](JFrame.EXIT_ON_CLOSE);
[Link](400, 300);
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
[Link]([Link]);
[Link](fileMenu);
JMenu editMenu = new JMenu("Edit");
[Link]([Link]);
[Link](editMenu);
JMenu viewMenu = new JMenu("View");
[Link]([Link]);
[Link](viewMenu);
JMenu helpMenu = new JMenu("Help");
[Link]([Link]);
[Link](helpMenu);
[Link](menuBar);
[Link](true);
}
[Link] a program to demonstrate ActionListener Interface.
import [Link].*;
import [Link];
import [Link];
class App implements ActionListener {
private JButton button;
private JLabel label;
public App() {
JFrame frame = new JFrame("ActionListener Demo");
button = new JButton("Click Me");
label = new JLabel("Button not clicked yet.");
[Link](this);
[Link](new [Link]());
[Link](button);
[Link](label);
[Link](300, 200);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
public void actionPerformed(ActionEvent e) {
[Link]("Button clicked!");
public static void main(String[] args) {
new App();
}
12 Write a java program to create a table of name of Student,Percentage and Grade of 5 Student using
JTable.
import [Link].*;
import [Link];
public class App {
public static void main(String[] args) {
// Create a JFrame
JFrame frame = new JFrame("Student Grades");
[Link](JFrame.EXIT_ON_CLOSE);
[Link](400, 300);
// Create column names
String[] columnNames = {"Name", "Percentage", "Grade"};
// Create data for the table
Object[][] data = {
{"John Doe", 85.5, "A"},
{"Jane Smith", 78.0, "B"},
{"Emily Johnson", 92.0, "A"},
{"Michael Brown", 67.5, "C"},
{"Sarah Davis", 88.0, "B"}
};
// Create a table model
DefaultTableModel model = new DefaultTableModel(data, columnNames);
JTable table = new JTable(model);
// Add the table to a JScrollPane
JScrollPane scrollPane = new JScrollPane(table);
[Link](scrollPane);
// Set the frame visibility
[Link](true);
14. Design an application to demonstrate the use of Radio Button and Checkbox using swing
import [Link].*;
import [Link].*;
import [Link].*;
public class NoPanelNoBounds {
public static void main(String[] args) {
JFrame frame = new JFrame("Radio & Checkbox");
[Link](300, 250);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](new FlowLayout()); // No setBounds needed
// Radio buttons
[Link](new JLabel("Select Gender:"));
JRadioButton male = new JRadioButton("Male");
JRadioButton female = new JRadioButton("Female");
ButtonGroup genderGroup = new ButtonGroup();
[Link](male); [Link](female);
[Link](male); [Link](female);
// Checkboxes
[Link](new JLabel("Select Hobbies:"));
JCheckBox reading = new JCheckBox("Reading");
JCheckBox coding = new JCheckBox("Coding");
[Link](reading); [Link](coding);
// Submit button and result label
JButton submit = new JButton("Submit");
JLabel result = new JLabel("");
[Link](submit); [Link](result);
// Button action
[Link](e -> {
String gender = [Link]() ? "Male" : [Link]() ? "Female" : "None";
String hobbies = ([Link]() ? "Reading " : "") + ([Link]() ? "Coding" : "");
[Link]("Gender: " + gender + ", Hobbies: " + hobbies);
});
[Link](true);
}
import [Link].*;
import [Link].*;
public class BorderLayoutExample {
public static void main(String[] args) {
JFrame frame = new JFrame("BorderLayout Demo");
[Link](300, 200);
[Link](JFrame.EXIT_ON_CLOSE);
// Set BorderLayout
[Link](new BorderLayout());
// Add components to different regions
[Link](new JButton("North"), [Link]);
[Link](new JButton("South"), [Link]);
[Link](new JButton("East"), [Link]);
[Link](new JButton("West"), [Link]);
[Link](new JButton("Center"), [Link]);
[Link](true);
}
import [Link].*;
import [Link].*;
public class JTreeExample {
public static void main(String[] args) {
// Root node
DefaultMutableTreeNode style = new DefaultMutableTreeNode("Style");
// Child nodes
DefaultMutableTreeNode color = new DefaultMutableTreeNode("color");
[Link](new DefaultMutableTreeNode("red"));
[Link](new DefaultMutableTreeNode("blue"));
[Link](new DefaultMutableTreeNode("black"));
[Link](new DefaultMutableTreeNode("green"));
DefaultMutableTreeNode font = new DefaultMutableTreeNode("font");
// Add children to root
[Link](color);
[Link](font);
// Create JTree
JTree tree = new JTree(style);
// Add tree to scroll pane
JScrollPane scrollPane = new JScrollPane(tree);
// Frame setup
JFrame frame = new JFrame("JTree Example");
[Link](JFrame.EXIT_ON_CLOSE);
[Link](300, 200);
[Link](scrollPane);
[Link](true);
19. Write a program to create Combo Box
import [Link].*;
public class ComboBoxExample {
public static void main(String[] args) {
// Create a frame
JFrame frame = new JFrame("ComboBox Example");
// Create a combo box with some items
String[] items = { "Apple", "Banana", "Mango", "Orange" };
JComboBox<String> comboBox = new JComboBox<>(items);
// Set position and size
[Link](50, 50, 150, 20);
// Add combo box to the frame
[Link](comboBox);
// Frame settings
[Link](300, 200);
[Link](null);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
1. Write a program using URL class to retrieve the host, protocol, port and file of URL
[Link]
import [Link].*;
public class URLDetails {
public static void main(String[] args) {
try {
// Create a URL object with the given URL
URL url = new URL("[Link]
// Retrieve and display the URL components
[Link]("URL: " + url);
[Link]("Protocol: " + [Link]());
[Link]("Host: " + [Link]());
[Link]("Port: " + [Link]());
[Link]("File: " + [Link]());
} catch (MalformedURLException e) {
[Link]("Invalid URL");
}
2. Develop a program using InetAddress class to retrieve IP address of computer when hostname is
entered by the user.
import [Link].*;
import [Link];
public class IPAddressRetriever {
public static void main(String[] args) {
try {
// Create a Scanner object to take user input
Scanner scanner = new Scanner([Link]);
[Link]("Enter the hostname: ");
String hostname = [Link]();
// Get the InetAddress object for the given hostname
InetAddress inetAddress = [Link](hostname);
// Retrieve and display the IP address
[Link]("IP Address of " + hostname + ": " + [Link]());
} catch (UnknownHostException e) {
[Link]("Unable to find the IP address for the given hostname.");
}
1. Write program to Create a student table in database and insert a record in student table
-- SQL to create a student table
CREATE TABLE student (
student_id INT PRIMARY KEY,
name VARCHAR(50),
age INT,
course VARCHAR(50)
);
-- SQL to insert a record into the student table
INSERT INTO student (student_id, name, age, course)
VALUES (1, 'John Doe', 20, 'Computer Science');
3. Write program to implement delete statement
import [Link].*;
public class DeleteStudent {
public static void main(String[] args) {
// Database credentials
String url = "jdbc:mysql://localhost:3306/your_database";
String user = "your_username";
String password = "your_password";
// SQL query for deleting a student record
String query = "DELETE FROM student WHERE student_id = ?";
try (Connection conn = [Link](url, user, password);
PreparedStatement stmt = [Link](query)) {
// Set the student_id to be deleted (example: 1)
[Link](1, 1);
// Execute the delete statement
int rowsAffected = [Link]();
if (rowsAffected > 0) {
[Link]("Student record deleted successfully!");
} else {
[Link]("No record found with the given ID.");
} catch (SQLException e) {
[Link]();
4. Develop JDBC program to Retrieve Data from table using resultset interface.
import [Link].*;
public class RetrieveData {
public static void main(String[] args) {
// Database credentials
String url = "jdbc:mysql://localhost:3306/your_database";
String user = "your_username";
String password = "your_password";
// SQL query to select all students
String query = "SELECT * FROM student";
try (Connection conn = [Link](url, user, password);
Statement stmt = [Link]();
ResultSet rs = [Link](query)) {
// Iterate through the result set
while ([Link]()) {
int student_id = [Link]("student_id");
String name = [Link]("name");
int age = [Link]("age");
String course = [Link]("course");
// Print each record
[Link]("Student ID: " + student_id);
[Link]("Name: " + name);
[Link]("Age: " + age);
[Link]("Course: " + course);
[Link]("----------------------------");
} catch (SQLException e) {
[Link]();