[Link].
INDEX REMARKS
1. Write a program to check palindrome number.
2. Write a program to check Armstrong number.
3. Write a program to check the prime number.
4. Write a program to calculate simple interest using the GUI
Form.
5. Write a program to demonstrate the thread life cycle.
6. Write a program to show the use of applet.
7. Write a program to demonstrate the concept of arrays.
8. Write a program to find the second largest and second
smallest number in array.
9. Write a program to perform multiplication of two matrices.
10. Write a program to demonstrate the concept of method
overloading.
11. Write a program to demonstrate the concept of constructor
overloading.
12. Write a program to demonstrate the concept of inner classes.
13. Write a program to demonstrate the concept of inheritance.
14. Write a program to demonstrate the concept of access
specifiers in java.
15. Write a program to implement the concept of interface.
16. Write a program to show the creation of package in java.
17. Write a program to design the user registration form with
basic registration details.
18. Write a program to show the exception handling process in
java.
19. Write a program to show the significance of multithreading.
1
Programming in Java
20. Write a program to read the data from the console device and
store it in any file in secondary storage.
21. Write a program to copy the content of any file into another
file.
22. Write a program to demonstrate the advantages of event
delegation model.
23. Write a program in java for command line value passing.
2
Programming in Java
Program 1: Write a program to check palindrome number in java.
Input:-
import [Link];
public class Palindrome {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
int original = num;
int reverse = 0;
while(num != 0) {
int digit = num % 10;
reverse = reverse * 10 + digit;
num = num / 10;
}
if(original == reverse) {
[Link](original + " is a Palindrome.");
} else {
[Link](original + " is NOT a Palindrome.");
}
}
}
Output:-
3
Programming in Java
Program 2: Write a program to check Armstrong number.
Input:
import [Link];
public class Armstrong {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
int original = num;
int result = 0;
while(num > 0) {
int digit = num % 10;
result += digit * digit * digit; // For 3-digit Armstrong number
num /= 10;
}
if(result == original) {
[Link](original + " is an Armstrong number.");
} else {
[Link](original + " is NOT an Armstrong number.");
}
}
}
Output:
4
Programming in Java
Program 3: Write a program to check the prime number.
Input:
import [Link];
public class PrimeCheck {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
boolean isPrime = true;
if(num <= 1) {
isPrime = false;
} else {
for(int i = 2; i <= num / 2; i++) {
if(num % i == 0) {
isPrime = false;
break;
}
}
}
if(isPrime) {
[Link](num + " is a Prime number.");
} else {
[Link](num + " is NOT a Prime number.");
}
}
}
Output:-
5
Programming in Java
Program 4: Write a program to calculate simple interest using the GUI Form.
Input:
import [Link].*;
import [Link].*;
import [Link].*;
public class SimpleInterestGUI extends JFrame implements ActionListener {
JLabel l1, l2, l3, l4;
JTextField t1, t2, t3, t4;
JButton b1, b2;
SimpleInterestGUI() {
setTitle("Simple Interest Calculator");
setSize(350, 300);
setLayout(new GridLayout(6, 2, 10, 10));
l1 = new JLabel("Principal Amount:");
l2 = new JLabel("Rate of Interest:");
l3 = new JLabel("Time (in years):");
l4 = new JLabel("Simple Interest:");
t1 = new JTextField();
t2 = new JTextField();
t3 = new JTextField();
t4 = new JTextField();
[Link](false);
b1 = new JButton("Calculate");
b2 = new JButton("Clear");
add(l1); add(t1);
add(l2); add(t2);
add(l3); add(t3);
add(l4); add(t4);
add(b1); add(b2);
[Link](this);
[Link](this);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
6
Programming in Java
public void actionPerformed(ActionEvent e) {
if ([Link]() == b1) {
double p = [Link]([Link]());
double r = [Link]([Link]());
double t = [Link]([Link]());
double si = (p * r * t) / 100;
[Link]([Link](si));
}
if ([Link]() == b2) {
[Link]("");
[Link]("");
[Link]("");
[Link]("");
}
}
public static void main(String[] args) {
new SimpleInterestGUI();
}
}
Output:
7
Programming in Java
Program 5: Write a program to demonstrate the thread life cycle.
Input:
class LifeCycleDemo extends Thread {
public void run() {
[Link]("Thread is in RUNNING state");
try {
[Link](1000); // Thread enters TIMED WAITING state
} catch (InterruptedException e) {
[Link](e);
}
[Link]("Thread is in TERMINATED state");
}
public static void main(String[] args) {
LifeCycleDemo t = new LifeCycleDemo();
[Link]("Thread is in NEW state");
[Link](); // Thread enters RUNNABLE state
}
}
Output:
8
Programming in Java
Program 6: Write a program to show the use of an applet.
Input:
import [Link];
import [Link];
/*
<applet code="[Link]" width="300" height="200">
</applet>
*/
public class MyApplet extends Applet {
public void init() {
// Initialization
}
public void paint(Graphics g) {
[Link]("Hello, This is a Java Applet", 50, 100);
}
}
Output:
This warning occurs because the Applet class is deprecated in newer Java versions.
Applets are no longer supported in modern environments, but the program compiles
correctly and is used for academic purposes.
Otherwise, in JDK 8 Version the output would be-
9
Programming in Java
Program 7: Write a program to demonstrate the concept of arrays.
Input:
class ArrayDemo {
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50};
[Link]("Elements of the array are:");
for (int i = 0; i < [Link]; i++) {
[Link](arr[i]);
}
}
}
Output:
10
Programming in Java
Program 8: Write a program to find the second largest and second smallest
number in the array.
Input:
import [Link];
import [Link];
class SecondLargeSmall {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of elements: ");
int n = [Link]();
int[] arr = new int[n];
[Link]("Enter array elements:");
for (int i = 0; i < n; i++) {
arr[i] = [Link]();
}
[Link](arr);
[Link]("Second Smallest Number: " + arr[1]);
[Link]("Second Largest Number: " + arr[n - 2]);
}
}
Output:
11
Programming in Java
Program 9: Write a program to perform multiplication of two matrices.
Input:
import [Link];
class MatrixMultiplication {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int i, j, k;
[Link]("Enter number of rows of first matrix: ");
int r1 = [Link]();
[Link]("Enter number of columns of first matrix: ");
int c1 = [Link]();
[Link]("Enter number of rows of second matrix: ");
int r2 = [Link]();
[Link]("Enter number of columns of second matrix: ");
int c2 = [Link]();
if (c1 != r2) {
[Link]("Matrix multiplication not possible");
return;
}
int[][] a = new int[r1][c1];
int[][] b = new int[r2][c2];
int[][] mul = new int[r1][c2];
[Link]("Enter elements of first matrix:");
for (i = 0; i < r1; i++) {
for (j = 0; j < c1; j++) {
a[i][j] = [Link]();
}
}
[Link]("Enter elements of second matrix:");
for (i = 0; i < r2; i++) {
for (j = 0; j < c2; j++) {
b[i][j] = [Link]();
}
}
12
Programming in Java
for (i = 0; i < r1; i++) {
for (j = 0; j < c2; j++) {
mul[i][j] = 0;
for (k = 0; k < c1; k++) {
mul[i][j] += a[i][k] * b[k][j];
}
}
}
[Link]("Resultant Matrix:");
for (i = 0; i < r1; i++) {
for (j = 0; j < c2; j++) {
[Link](mul[i][j] + " ");
}
[Link]();
}
}
}
Output:
13
Programming in Java
Program 10: Write a program to demonstrate the concept of method overloading.
Input:
import [Link];
class MethodOverloading {
void add(int a, int b) {
[Link]("Sum of two integers: " + (a + b));
}
void add(int a, int b, int c) {
[Link]("Sum of three integers: " + (a + b + c));
}
void add(double a, double b) {
[Link]("Sum of two doubles: " + (a + b));
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
MethodOverloading obj = new MethodOverloading();
[Link]("Enter two integers: ");
int x = [Link]();
int y = [Link]();
[Link](x, y);
[Link]("Enter three integers: ");
int a = [Link]();
int b = [Link]();
int c = [Link]();
[Link](a, b, c);
[Link]("Enter two decimal numbers: ");
double d1 = [Link]();
double d2 = [Link]();
[Link](d1, d2);
}
}
14
Programming in Java
Output:
15
Programming in Java
Program 11: Write a program to demonstrate the concept of constructor
overloading.
Input:
import [Link];
class ConstructorOverloading {
int a, b, c;
// Constructor with two parameters
ConstructorOverloading(int x, int y) {
a = x;
b = y;
[Link]("Sum of two numbers: " + (a + b));
}
// Constructor with three parameters
ConstructorOverloading(int x, int y, int z) {
a = x;
b = y;
c = z;
[Link]("Sum of three numbers: " + (a + b + c));
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter two integers: ");
int x = [Link]();
int y = [Link]();
new ConstructorOverloading(x, y);
[Link]("Enter three integers: ");
int p = [Link]();
int q = [Link]();
int r = [Link]();
new ConstructorOverloading(p, q, r);
}
}
16
Programming in Java
Output:
17
Programming in Java
Program 12: Write a program to demonstrate the concept of inner classes.
Input:
import [Link];
class OuterClass {
int num;
// Inner class
class InnerClass {
void display() {
[Link]("Value entered is: " + num);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
OuterClass outer = new OuterClass();
[Link]("Enter a number: ");
[Link] = [Link]();
[Link] inner = [Link] InnerClass();
[Link]();
}
}
Output:
18
Programming in Java
Program 13: Write a program to demonstrate the concept of inheritance.
Input:
import [Link];
// Parent class
class Parent {
int a;
void getData(int x) {
a = x;
}
}
// Child class
class Child extends Parent {
int b;
void getDataChild(int y) {
b = y;
}
void displaySum() {
[Link]("Sum of values: " + (a + b));
}
}
class InheritanceDemo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
Child obj = new Child();
[Link]("Enter value for a (Parent class): ");
int x = [Link]();
[Link](x);
[Link]("Enter value for b (Child class): ");
int y = [Link]();
[Link](y);
19
Programming in Java
[Link]();
}
}
Output:
20
Programming in Java
Program 14: Write a program to demonstrate the concept of access specifiers in
java.
Input:
import [Link];
// Parent class
class AccessParent {
public int publicVar;
protected int protectedVar;
int defaultVar; // default access
private int privateVar;
void setPrivate(int x) {
privateVar = x;
}
void displayParent() {
[Link]("Public value: " + publicVar);
[Link]("Protected value: " + protectedVar);
[Link]("Default value: " + defaultVar);
[Link]("Private value: " + privateVar);
}
}
// Child class
class AccessChild extends AccessParent {
void displayChild() {
[Link]("Accessing in Child class:");
[Link]("Public value: " + publicVar);
[Link]("Protected value: " + protectedVar);
[Link]("Default value: " + defaultVar);
// privateVar not accessible here
}
}
class AccessSpecifierDemo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
AccessChild obj = new AccessChild();
21
Programming in Java
[Link]("Enter public value: ");
[Link] = [Link]();
[Link]("Enter protected value: ");
[Link] = [Link]();
[Link]("Enter default value: ");
[Link] = [Link]();
[Link]("Enter private value: ");
int p = [Link]();
[Link](p);
[Link]("\nParent Class Output:");
[Link]();
[Link]("\nChild Class Output:");
[Link]();
}
}
Output:
22
Programming in Java
Program 15: Write a program to implement the concept of interface.
Input:
import [Link];
// Interface
interface Calculator {
void add(int a, int b);
void subtract(int a, int b);
}
// Class implementing interface
class MyCalculator implements Calculator {
public void add(int a, int b) {
[Link]("Addition: " + (a + b));
}
public void subtract(int a, int b) {
[Link]("Subtraction: " + (a - b));
}
}
class InterfaceDemo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
Calculator obj = new MyCalculator();
[Link]("Enter first number: ");
int x = [Link]();
[Link]("Enter second number: ");
int y = [Link]();
[Link](x, y);
[Link](x, y);
}
}
23
Programming in Java
Output:
24
Programming in Java
Program 16: Write a program to show the creation of package in java.
Input:
package mypackage;
import [Link];
public class PackageClass {
public void displaySum() {
Scanner sc = new Scanner([Link]);
[Link]("Enter first number: ");
int a = [Link]();
[Link]("Enter second number: ");
int b = [Link]();
[Link]("Sum = " + (a + b));
}
}
—-----------------------------------------------------------------------------------------
import [Link];
class PackageDemo {
public static void main(String[] args) {
PackageClass obj = new PackageClass();
[Link]();
}
}
Output:
25
Programming in Java
Program 17: Write a program to design the user registration form with basic
registration details.
Input:
import [Link].*;
import [Link].*;
import [Link].*;
class RegistrationForm extends JFrame implements ActionListener {
JTextField nameField, emailField;
JPasswordField passField;
JRadioButton male, female;
JComboBox<String> courseBox;
JButton registerBtn;
RegistrationForm() {
setTitle("User Registration Form");
setSize(400, 400);
setLayout(new GridLayout(7, 2, 10, 10));
add(new JLabel("Name:"));
nameField = new JTextField();
add(nameField);
add(new JLabel("Email:"));
emailField = new JTextField();
add(emailField);
add(new JLabel("Password:"));
passField = new JPasswordField();
add(passField);
add(new JLabel("Gender:"));
male = new JRadioButton("Male");
female = new JRadioButton("Female");
ButtonGroup bg = new ButtonGroup();
[Link](male);
[Link](female);
JPanel genderPanel = new JPanel();
[Link](male);
[Link](female);
add(genderPanel);
26
Programming in Java
add(new JLabel("Course:"));
String[] courses = {"BCA", "BSc", "BCom", "BA"};
courseBox = new JComboBox<>(courses);
add(courseBox);
registerBtn = new JButton("Register");
add(registerBtn);
[Link](this);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
String name = [Link]();
String email = [Link]();
String password = new String([Link]());
String gender = [Link]() ? "Male" : "Female";
String course = (String) [Link]();
[Link](this,
"Name: " + name +
"\nEmail: " + email +
"\nGender: " + gender +
"\nCourse: " + course,
"Registration Successful",
JOptionPane.INFORMATION_MESSAGE);
}
public static void main(String[] args) {
new RegistrationForm();
}
}
Output:
27
Programming in Java
28
Programming in Java
Program 18: Write a program to show the exception handling process in java.
Input:
import [Link];
class ExceptionHandlingDemo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
try {
[Link]("Enter first number: ");
int a = [Link]();
[Link]("Enter second number: ");
int b = [Link]();
int result = a / b;
[Link]("Result = " + result);
}
catch (ArithmeticException e) {
[Link]("Error: Division by zero is not allowed");
}
finally {
[Link]("Program execution completed");
}
}
}
Output:
29
Programming in Java
Program 19: Write a program to show the significance of multithreading
Input:
import [Link];
// First thread
class ThreadOne extends Thread {
int n;
ThreadOne(int n) {
this.n = n;
}
public void run() {
[Link]("Thread One started");
for (int i = 1; i <= n; i++) {
[Link]("Thread One: " + i);
}
[Link]("Thread One ended");
}
}
// Second thread
class ThreadTwo extends Thread {
int n;
ThreadTwo(int n) {
this.n = n;
}
public void run() {
[Link]("Thread Two started");
for (int i = 1; i <= n; i++) {
[Link]("Thread Two: " + i);
}
[Link]("Thread Two ended");
}
}
class MultithreadingDemo {
public static void main(String[] args) {
30
Programming in Java
Scanner sc = new Scanner([Link]);
[Link]("Enter limit for threads: ");
int n = [Link]();
ThreadOne t1 = new ThreadOne(n);
ThreadTwo t2 = new ThreadTwo(n);
[Link]();
[Link]();
}
}
Output:
31
Programming in Java
Program 20: Write a program to read the data from the console device and store it
in any file in secondary storage.
Input:
import [Link];
import [Link];
import [Link];
class ConsoleToFile {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
try {
FileWriter fw = new FileWriter("[Link]");
[Link]("Enter text to store in file: ");
String data = [Link]();
[Link](data);
[Link]();
[Link]("Data successfully written to file.");
}
catch (IOException e) {
[Link]("Error while writing to file.");
}
}
}
Output:
32
Programming in Java
Program 21: Write a program to copy the content of any file into another file.
Input:
import [Link];
import [Link];
import [Link];
import [Link];
class FileCopy {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
try {
[Link]("Enter source file name: ");
String source = [Link]();
[Link]("Enter destination file name: ");
String dest = [Link]();
FileReader fr = new FileReader(source);
FileWriter fw = new FileWriter(dest);
int ch;
while ((ch = [Link]()) != -1) {
[Link](ch);
}
[Link]();
[Link]();
[Link]("File copied successfully.");
}
catch (IOException e) {
[Link]("Error occurred while copying file.");
}
}
}
33
Programming in Java
Output:
34
Programming in Java
Program 22: Write a program to demonstrate the advantages of event delegation
mode.
Input:
import [Link].*;
import [Link].*;
class EventDelegationDemo extends JFrame implements ActionListener {
JButton btn;
EventDelegationDemo() {
btn = new JButton("Click Me");
[Link](100, 100, 120, 40);
[Link](this); // Event delegation
add(btn);
setSize(300, 300);
setLayout(null);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
[Link](this,
"Button Clicked!\nEvent handled using Event Delegation Model");
}
public static void main(String[] args) {
new EventDelegationDemo();
}
}
Output:
35
Programming in Java
36
Programming in Java
Program 23: Write a program in java for command line value passing.
Input:
class CommandLineDemo {
public static void main(String[] args) {
if ([Link] < 2) {
[Link]("Please pass two numbers from command line");
return;
}
int a = [Link](args[0]);
int b = [Link](args[1]);
[Link]("First number: " + a);
[Link]("Second number: " + b);
[Link]("Sum: " + (a + b));
}
}
Output:
37