Index of java program
1. Write a progam to calculate the perimeter of rectangle.
2. write program using switch statement.
3. write a program to check that number is prime or not .
4. write a program to calculate factorial using recursion.
5. write a program using static keyword
6. write a progam to find the sum of 10 number using array
7. write a program to add 2D matrix.
8. write a program by using simple concept of class and object.
9. rite a program using string handling function.
10. write a program to find the area of rectangle and circle
using interface.
11. .write a program to implement multiple inheritance
12. write a program by using super keyword.
13. write a program to create and access package.
14. write a program using system package.
15. . Write a program to handle exception handling using
try and multiple catch statement and finally block.
16. write a program to create user-defined exeception.
17. Write a program of reading and writing from a file.
18. write a applet code to add 2 integer in the text box and
their sum should be appear in 3rd text box
19. write a program using AWT.
20. write code to show all the activites of mouse listener.
[Link] a progam to calculate the perimeter of rectangle.
public class rectangle {
import [Link];
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("enter the length of rectangle");
double l = [Link]();
[Link]("enter the breadth of rectangle");
double b = [Link]();
double p = 2 * (l + b);
[Link]("The perimeter is: " + p);
}}
2. write program using switch statement.
import [Link];
class secondpro {
public static void main(String[] args) {
[Link]("Enter case number:");
try (Scanner sc = new Scanner([Link])) {
int day = [Link]();
switch (day) {
case 1 -> [Link]("Monday");
case 2 -> [Link]("Tuesday");
case 3 -> [Link]("Wednesday");
case 4 -> [Link]("Thursday");
case 5 -> [Link]("Friday");
case 6 -> [Link]("Saturday");
case 7 -> [Link]("Sunday");
default -> [Link]("Invalid day number");
}
3. write a program to check that number is prime or not .
import [Link];
public class PrimeCheck {
public static void main(String[] args) {
int n, count = 0;
[Link]("Enter any number: ");
Scanner r = new Scanner([Link]);
n = [Link]();
for (int i = 1; i <= n; i++) {
if (n % i == 0) {
count++;
if (count == 2)
[Link]("Prime number");
else
[Link]("Not a prime number");
}
4. write a program to calculate factorial using recursion.
import [Link];
class A {
public static void main(String[] args) {
int n;
[Link]("Enter any number: ");
Scanner r = new Scanner([Link]);
n = [Link]();
A obj = new A();
int result = [Link](n);
[Link]("Factorial of the given number: " + result);
int fact(int n) {
if (n == 1 || n == 0) {
return 1;
} else {
return n * fact(n - 1); // Recursive call
}
5. write a program using static keyword.
public class cwh_31_methods {
static int logic(int x, int y) {
int z;
if (x > y) {
z = x + y;
} else {
z = (x + y) * 5;
return z;
public static void main(String[] args) {
int a = 5;
int b = 7;
int c;
c = logic(a, b);
int a1 = 5;
int b1 = 3;
int c1;
c1 = logic(a1, b1);
[Link](c);
[Link](c1);
}}
6. write a progam to find the sum of 10 number using array.
public class Mainsum {
public static void main(String[] args) {
float[] marks = { 45.7f, 67.8f, 63.4f, 99.2f, 100.0f, 12.3f, 45.6f, 78.9f, 98.7f, 65.4f };
float sum = 0;
for (float element : marks) {
sum += element;
[Link]("The value of sum is " + sum);
}
7. write a program to add 2D matrix.
public class Mainmatrix {
public static void main(String[] args) {
int i, j;
int[][] x = { { 55, 45 }, { 11, 23 }, { 79, 32 } };
int[][] y = { { 4, 9 }, { 3, 6 }, { 7, 0 } };
int[][] s = new int[3][2];
[Link]("Elements of 1st matrix are:");
for (i = 0; i < 3; i++) {
for (j = 0; j < 2; j++) {
[Link](x[i][j] + " ");
[Link]();
[Link]("Elements of 2nd matrix are:");
for (i = 0; i < 3; i++) {
for (j = 0; j < 2; j++) {
[Link](y[i][j] + " ");
[Link]();
// Matrix addition
for (i = 0; i < 3; i++) {
for (j = 0; j < 2; j++) {
s[i][j] = x[i][j] + y[i][j];
[Link]("Sum of the given matrices is:");
for (i = 0; i < 3; i++) {
for (j = 0; j < 2; j++) {
[Link](s[i][j] + " ");
[Link]();
}
8. write a program by using simple concept of class and object.
public class eight {
int id;
int salary;
String name;
public void printDetails() {
[Link]("My id is " + id);
[Link]("and my name is " + name);
public int getSalary() {
return salary;
// public class Custom {
public static void main(String[] args) {
[Link]("This is our custom class");
eight harry = new eight();
eight john = new eight();
[Link] = 12;
[Link] = 34000;
[Link] = "sumit";
[Link] = 17;
[Link] = 12000;
[Link] = "aashish";
[Link]();
int salary1 = [Link]();
[Link](salary1);
[Link]();
int salary = [Link]();
[Link](salary);
}
9. write a program using string handling function.
public class cwh_14_string_methods {
public static void main(String[] args) {
String name = "Harry";
int value = [Link](); // This line stores the length but doesn't use it
[Link]([Link]('r', 'p')); // Output: Happy
[Link]([Link]("r", "ier")); // Output: Hiarriery
}
[Link] a program to find the area of rectangle and circle using interface.
interface Area {
double Compute(double a, double b);
class Rectangle implements Area {
public double Compute(double l, double b) {
return l * b;
class Triangle implements Area {
public double Compute(double b, double h) {
return 0.5 * b * h;
public class MainArea {
public static void main(String[] args) {
Rectangle rect = new Rectangle();
double RArea = [Link](10, 20);
[Link]("The area of the Rectangle is " + RArea);
Triangle tri = new Triangle();
double TArea = [Link](10, 20);
[Link]("The area of the Triangle is " + TArea);
}}
[Link] a program to implement multiple inheritance.
public class eleven {
interface A {
void displayA();
interface B {
void displayB();
static class C implements A, B {
public void displayA() {
[Link]("This is displayA() from Interface A");
public void displayB() {
[Link]("This is displayB() from Interface B");
void show() {
[Link]("This is show() method from class C");
public static class MultipleInheritanceExample {
public static void main(String[] args) {
C obj = new C();
[Link]();
[Link]();
[Link]();
}
12. write a program by using super keyword.
class Animal {
String name;
Animal(String name) {
[Link] = name;
void displayInfo() {
[Link]("Animal name: " + name);
class Dog extends Animal {
String breed;
Dog(String name, String breed) {
super(name); // Call parent class constructor
[Link] = breed;
void displayDetails() {
[Link](); // Call parent class method
[Link]("Dog breed: " + breed);
}
public class Maindog {
public static void main(String[] args) {
Dog myDog = new Dog("Buddy", "Golden Retriever");
[Link]();
}
13. write a program to create and access package.
File name- Project/mypackage/[Link]
package [Link];
public class MyClass {
public void showMessage() {
[Link]("Hello brother i am sumit");
Filename- project/[Link]
package project;
import [Link];
public class mainp {
public static void main(String[] args) {
MyClass obj = new MyClass();
[Link]();
}
14. write a program using system package.
public class forteen {
public class SystemDemo {
public static void main(String[] args) {
[Link]("Welcome to the System package demo!");
long currentTime = [Link]();
[Link]("Current time in milliseconds: " + currentTime);
String userName = [Link]("[Link]");
[Link]("User name: " + userName);
long startTime = [Link]();
for (int i = 0; i < 1000000; i++) {
long endTime = [Link]();
[Link]("Execution time (nanoseconds): " + (endTime - startTime));
[Link]("Exiting program...");
[Link](0);
}
15. Write a program to handle exception handling using try and multiple catch
statement and finally block.
public class fifteen {
public class ExceptionDemo {
public static void main(String[] args) {
try {
int[] numbers = { 10, 20, 30 };
[Link]("Accessing element: " + numbers[5]);
int result = 10 / 0;
[Link]("Result: " + result);
} catch (ArrayIndexOutOfBoundsException e) {
[Link]("Error: Array index is out of bounds.");
} catch (ArithmeticException e) {
[Link]("Error: Cannot divide by zero.");
} catch (Exception e) {
[Link]("General exception caught.");
} finally {
[Link]("This block always executes.");
}
16. write a program to create user-defined exeception.
class AgeException extends Exception {
public AgeException(String message) {
super(message);
public class CustomExceptionDemo {
public static void checkAge(int age) throws AgeException {
if (age < 18) {
throw new AgeException("Age must be 18 or above.");
} else {
[Link]("Access granted. Age is valid.");
public static void main(String[] args) {
try {
checkAge(15);
} catch (AgeException e) {
[Link]("Caught Exception: " + [Link]());
}
18. write a applet code to add 2 integer in the text box and their sum should be appear in
3rd text box.
import [Link].*;
import [Link].*;
import [Link].*;
public class AddApp extends JFrame implements ActionListener {
JTextField t1, t2, t3;
JButton addButton;
public AddApp() {
setLayout(new FlowLayout());
t1 = new JTextField(5);
t2 = new JTextField(5);
t3 = new JTextField(10);
[Link](false);
addButton = new JButton("Add");
add(new JLabel("Enter first number:"));
add(t1);
add(new JLabel("Enter second number:"));
add(t2);
add(addButton);
add(new JLabel("Sum:"));
add(t3);
[Link](this);
setTitle("Addition App");
setSize(300, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
public void actionPerformed(ActionEvent e) {
try {
int num1 = [Link]([Link]());
int num2 = [Link]([Link]());
int sum = num1 + num2;
[Link]([Link](sum));
} catch (NumberFormatException ex) {
[Link]("Invalid input!");
public static void main(String[] args) {
new AddApp();
}
19. write a program using AWT.
import [Link].*;
import [Link].*;
public class AWTAddition extends Frame implements ActionListener {
TextField t1, t2, t3;
Button addButton;
public AWTAddition() {
setLayout(new FlowLayout());
Label l1 = new Label("First Number:");
t1 = new TextField(10);
Label l2 = new Label("Second Number:");
t2 = new TextField(10);
addButton = new Button("Add");
Label l3 = new Label("Result:");
t3 = new TextField(10);
[Link](false);
add(l1);
add(t1);
add(l2);
add(t2);
add(addButton);
add(l3);
add(t3);
[Link](this);
setTitle("AWT Addition");
setSize(300, 200);
setVisible(true);
public void actionPerformed(ActionEvent e) {
try {
int num1 = [Link]([Link]());
int num2 = [Link]([Link]());
int sum = num1 + num2;
[Link]([Link](sum));
} catch (NumberFormatException ex) {
[Link]("Invalid input");
public static void main(String[] args) {
new AWTAddition();
}
20. write code to show all the activites of mouse listener
import [Link].*;
import [Link].*;
public class MouseListenerDemo extends Frame implements MouseListener {
Label statusLabel;
public MouseListenerDemo() {
// Frame setup
setTitle("Mouse Listener Demo");
setSize(300, 200);
setLayout(new FlowLayout());
// Label to show mouse activity
statusLabel = new Label("Interact with the window...");
add(statusLabel);
// Add mouse listener to the frame
addMouseListener(this);
setVisible(true);
// MouseListener methods
public void mouseClicked(MouseEvent e) {
[Link]("Mouse Clicked at (" + [Link]() + ", " + [Link]() + ")");
public void mousePressed(MouseEvent e) {
[Link]("Mouse Pressed");
public void mouseReleased(MouseEvent e) {
[Link]("Mouse Released");
public void mouseEntered(MouseEvent e) {
[Link]("Mouse Entered the window");
public void mouseExited(MouseEvent e) {
[Link]("Mouse Exited the window");
public static void main(String[] args) {
new MouseListenerDemo();