1.
Fibonacci Series up to a Limit
Import [Link].*;
public class FibonacciSeries {
public static void main (String[] args) {
int n = 10;
int first = 0, second = 1;
[Link]("Fibonacci Series upto “+n+” terms:”);
for (int i = 1; i <+ n; i++) {
[Link](first+””);
int next = first + second;
first = second;
second = next;
}
}
}
[Link] of a triangle
class Triangle
double base, height;
void readData (double b, double h)
base=b;
height=h;
double findArea( )
double a = 0.5*(base *height);
return a;
}
}
class Area
public static void main (String args[ ])
double area1, area2;
Triangle t1=new Triangle();
Triangle t2=new Triangle();
[Link]=20.5;
[Link]=15.5;
area1= [Link]();
[Link](30.0,15.0);
area2=[Link]();
[Link]("Area of the first triangle is ="+area1);
[Link]("\nArea of the second triangle is ="+area2);
[Link] of a string
Import [Link].*;
Class Reverse {
Public static void main (string args[]) {
Scanner sc=new Scanner([Link]);
[Link]("enter a string:");
string str = [Link]();
Char temp;
String str 2= " ";
for(int i = 0; i<[Link]();i++) {
temp = str. CharAt(i);
Str2= temp+str2;
}
System out Println("Reverse:" + str2);
}
}
[Link] of a number
Import [Link].*;
Public class Reverse Number{
public static void main(String [] args) {
Scanner Scanner = new Scanner ([Link]);
[Link]("Enter a number:");
int number = [Link]();
int original Number = number;
int reverse = 0;
While (number !=o){
int digit =number % 10;
reverse =reverse* 10+digit,
number =number/10;
}
[Link] Println (“Reverse of " + original Numbert+" is: "+reverse);
[Link]();
}
}
5. mathematical functions and following use switch
case statements
1. Maximum of 2 numbers
2. Square root of a number
3. Absolute value of a number
4. Power of anumber
5. Minimum of two numbers
import [Link];
class MathFunctionsSwitch {
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
int choice;
[Link]("\n=== Mathematical Functions Menu ===");
[Link]("1. Maximum of two numbers");
[Link]("2. Square root of a number");
[Link]("3. Absolute value of a number");
[Link]("4. Power of a number");
[Link]("5. Minimum of two numbers");
[Link]("Enter your choice (1-5): ");
choice = [Link]();
switch (choice) {
case 1:
[Link]("Enter first number: ");
double num1 = [Link]();
[Link]("Enter second number: ");
double num2 = [Link]();
[Link]("Maximum = " + [Link](num1, num2));
break;
case 2:
[Link]("Enter a number: ");
double num = [Link]();
[Link]("Square root = " + [Link](num));
break;
case 3:
[Link]("Enter a number: ");
double numAbs = [Link]();
[Link]("Absolute value = " + [Link](numAbs));
break;
case 4:
[Link]("Enter base: ");
double base = [Link]();
[Link]("Enter exponent: ");
double exponent = [Link]();
[Link]("Power = " + [Link](base, exponent));
break;
case 5:
[Link]("Enter first number: ");
double numMin1 = [Link]();
[Link]("Enter second number: ");
double numMin2 = [Link]();
[Link]("Minimum = " + [Link](numMin1, numMin2));
break;
default:
[Link]("Invalid choice!");
}
[Link] of square,rectangle,circle using method overloading
class ShapeArea {
double area(double side) {
return side * side;
}
double area(double length, double width) {
return length * width;
}
double area(float radius) {
return 3.14159 * radius * radius;
}
}
public class TestOverloading {
public static void main(String[] args) {
ShapeArea shape = new ShapeArea();
double squareArea = [Link](5);
double rectangleArea = [Link](4, 6);
double circleArea = [Link](3.5f);
[Link]("Area of Square: " + squareArea);
[Link]("Area of Rectangle: " + rectangleArea);
[Link]("Area of Circle: " + circleArea);
}
}
[Link]();
}
}
[Link] constructor overloading
class rectangle
{
int l,b;
float p,q;
Rectangle(int x, int y)
{
l = x;
b = y;
}
int first() {
return (l * b);
}
Rectangle(int x) {
l = x;
b = x;
}
int Second() {
return (l * b);
}
Rectangle(float x) {
p = x;
q = x;
}
float Third(){
return (p * q);
}
Rectangle(float x, float y){
p = x;
q = y;
}
float Fourth(){
return (p * q);
}
}
class ConstructorOverloading {
public static void main(String args[]) {
Rectangle r1 = new Rectangle(2,4);
int area1 = [Link]();
[Link]("Area in first constructor : " + area1);
Rectangle r2 = new Rectangle(5);
int area2 = [Link]();
[Link]("Area in Second constructor : " + area2);
Rectangle r3 = new Rectangle(2.0f);
float area3 = [Link]();
[Link]("Area in Third constructor : " + area3);
Rectangle r4 = new Rectangle(4.0f,5.0f);
float area4 = [Link]();
[Link]("Area in Fourth constructor : " + area4);
}
}
[Link] addition
import [Link];
public class MatrixAddition {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of rows: ");
int rows = [Link]();
[Link]("Enter number of columns: ");
int cols = [Link]();
int[][] matrix1 = new int[rows][cols];
int[][] matrix2 = new int[rows][cols];
int[][] sum = new int[rows][cols];
[Link]("Enter elements of first matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
[Link]("Element [" + i + "][" + j + "] : ");
matrix1[i][j] = [Link]();
}
}
[Link]("Enter elements of second matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
[Link]("Element [" + i + "][" + j + "] : ");
matrix2[i][j] = [Link]();
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
[Link]("Resultant matrix after addition:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
[Link](sum[i][j] + "\t");
}
[Link](); }
[Link]();
}
}
[Link] multiplication
import [Link];
public class MatrixMultiplication {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter rows of first matrix: ");
int r1 = [Link]()
[Link]("Enter columns of first matrix : ");
int c1 = [Link]();
[Link]("Enter rows of first matrix: ");
int r2 = [Link]();
[Link]("Enter columns of second matrix: ");
int c2 = [Link]();
if (c1 != r2) {
[Link]("Matrix multiplication not possible. Columns of first
matrix must equal rows of second matrix:");
return;
}
int[][] a = new int[r1][c1];
int[][] b = new int[r2][c2];
int[][] result = new int[r1][c2];
[Link]("Enter elements of first matrix:");
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c1; j++) {
a[i][j] = [Link]();
}
}
[Link]("Enter elements of second matrix:");
for (int i = 0; i < r2; i++) {
for (int j = 0; j < c2; j++) {
b[i][j] = [Link]();
}
}
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
for (int k = 0; k < c1; k++) {
result[i][j] += a[i][k] * b[k][j];
}
}
}
[Link]("Resultant Matrix after multiplication:");
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
[Link](result[i][j] + " ");
}
[Link]();
}
[Link]();
}
}
[Link] inheritance
Import [Link];
class Person {
String name;
int age;
}
void setPersonDetails(String n, int a) {
name = n;
age = a;
}
void displayPersonDetails() {
[Link]("Name: " + name);
[Link]("Age: " + age);
}
}
class Student extends Person {
String grade;
void setStudentDetails(String g) {
grade = g;
}
void displayStudentDetails() {
displayPersonDetails();
[Link]("Grade: " + grade);
}
}
public class SingleInheritanceDemo {
public static void main(String[] args) {
Student student = new Student();
[Link]("Alice", 16);
[Link]("10th Grade");
[Link]("Student Information:");
[Link]();
}
}
[Link] inheritance
import [Link];
class Person {
String name;
int age;
void getPersonDetails(Scanner sc) {
[Link]("Enter name: ");
name = [Link]();
[Link]("Enter age: ");
age = [Link]();
[Link]();
void showPersonDetails() {
[Link]("Name: " + name);
[Link]("Age: " + age);
class Student extends Person {
String schoolName;
int grade;
void getStudentDetails(Scanner sc) {
[Link]("Enter school name: ");
schoolName = [Link]();
[Link]("Enter grade (class): ");
grade = [Link]();
[Link](); }
void showStudentDetails() {
[Link]("School Name: " + schoolName);
[Link]("Grade: " + grade);
Class ExamResult extends Student {
int mathMarks;
int scienceMarks;
void getMarks(Scanner sc) {
[Link]("Enter Math marks: ");
mathMarks = [Link]();
[Link]("Enter Science marks: ");
scienceMarks = [Link]();
void showResult() {
int total = mathMarks + scienceMarks;
double average = total / 2.0;
[Link]("Math Marks: " + mathMarks);
[Link]("Science Marks: " + scienceMarks);
[Link]("Total Marks: " + total);
[Link]("Average Marks: " + average);
public class MultilevelInheritanceWithInput {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
ExamResult student = new ExamResult();
[Link](sc);
[Link](sc);
[Link](sc);
[Link]("\n----- Student Report -----");
[Link]();
[Link]();
[Link]();
[Link]();
[Link] class
abstract Class Shape
abstract double Area();
abstract double circumferences();
class Circle extends Shape
double radius;
Circle (double radius)
this radius = radius;
double Circumference ()
return 2*3.14* radius;
double Area()
{
return 3.14 radius*radius;
class Rectangle extends shape
double length;
double breadth;
Rectangle (double length, double breadth)
thit length = length;
this breadth breadth;
double Area ()
return length * breadth;
double Circumference()
return 2* length *breadth;
Class DemoAbstract
Public static void main(String[] args)
Rectangle r = new Rectangle (10.0,6.0);
Circle c = new circle (4.0);
[Link] ("The area of the rectangle is = "+[Link]());
[Link](" The Circumference of the rectangle is = [Link]());
Systern of. Println (" The area of the circle is ="+ [Link]());
[Link]. Println ("The circumference of the circle is ="+C. circumference);
[Link] inheritance
interface Student {
void getDetails(String name, int rollNo);
interface Marks {
void getMarks(int m1, int m2, int m3);
class Person {
String name;
int rollNo;
void displayDetails() {
[Link]("Name: " + name);
[Link]("Roll No: " + rollNo);
class Result extends Person implements Student, Marks {
int m1, m2, m3, total;
double percentage;
public void getDetails(String name, int rollNo) {
[Link] = name;
[Link] = rollNo;
public void getMarks(int m1, int m2, int m3) {
this.m1 = m1;
this.m2 = m2;
this.m3 = m3;
calculateResult();
void calculateResult() {
total = m1 + m2 + m3;
percentage = total / 3.0;
void displayResult() {
displayDetails();
[Link]("Marks: " + m1 + ", " + m2 + ", " + m3);
[Link]("Total: " + total);
[Link]("Percentage: " + percentage + "%");
public class HybridInheritanceExample {
public static void main(String[] args) {
Result student1 = new Result();
[Link]("Asha", 101);
[Link](85, 90, 88);
[Link]();
[Link] of a Matrix
import [Link];
public class TransposeMatrix {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of rows: ");
int rows = [Link]();
[Link]("Enter number of columns: ");
int cols = [Link]();
int[][] matrix = new int[rows][cols];
int[][] transpose = new int[cols][rows];
[Link]("Enter matrix elements:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j] = [Link]();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
transpose[j][i] = matrix[i][j];
[Link]("\nOriginal Matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
[Link](matrix[i][j] + " ");
[Link]();
[Link]("\nTranspose of Matrix:");
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
[Link](transpose[i][j] + " ");
}
[Link]();
[Link]();
[Link] hierarchy using inheritance
import [Link];
class Office {
int empno;
String empname;
double salary;
void getvalue() {
Scanner sc = new Scanner([Link]);
[Link]("Enter Employee Number: ");
empno = [Link]();
[Link]();
[Link]("Enter Employee Name: ");
empname = [Link]();
[Link]("Enter Salary: ");
salary = [Link]();
void displayInfo() {
[Link]("Employee Number: " + empno);
[Link]("Employee Name: " + empname);
[Link]("Salary: " + salary);
}
class Teaching extends Office {
String designation;
void setvalue() {
getvalue();
Scanner sc = new Scanner([Link]);
[Link]("Enter Teaching Designation (e.g., Lecturer, Professor): ");
designation = [Link]();
void displayTeaching() {
[Link]("\n--- Teaching Staff Details ---");
displayInfo();
[Link]("Designation: " + designation);
class NonTeaching extends Office {
String designation;
void setvalue() {
getvalue();
Scanner sc = new Scanner([Link]);
[Link]("Enter Non-Teaching Designation (e.g., Clerk, Lab Assistant): ");
designation = [Link]();
void displayNonTeaching() {
[Link]("\n--- Non-Teaching Staff Details ---");
displayInfo();
[Link]("Designation: " + designation);
}
public class EducationalHierarchy {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter details for Teaching staff:");
Teaching t = new Teaching();
[Link]();
[Link]("\nEnter details for Non-Teaching staff:");
NonTeaching nt = new NonTeaching();
[Link]();
[Link]();
[Link]();
16. package to perform the mathematical operation
Addition,Subtraction,Multiplication, Division and Modulus. Write a menu driven
program for all these operations and import the package for the above said
operations.
package mathpack;
public class Calculator1 {
public int add(int a, int b) {
return a + b;
public int subtract(int a, int b) {
return a - b;
public int multiply(int a, int b) {
return a * b;
}
public int divide(int a, int b) {
if (b == 0) {
throw new ArithmeticException("Division by zero is not allowed.");
return a / b;
public int modulus(int a, int b) {
if (b == 0) {
throw new ArithmeticException("Modulus by zero is not allowed.");
return a % b;
import [Link];
import mathpack.Calculator1;
public class MathTest1 {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
Calculator1 calc = new Calculator1();
int choice;
int a, b;
do {
[Link]("\n--- Math Operations Menu ---");
[Link]("1. Addition");
[Link]("2. Subtraction");
[Link]("3. Multiplication");
[Link]("4. Division");
[Link]("5. Modulus");
[Link]("6. Exit");
[Link]("Enter your choice: ");
choice = [Link]();
if (choice >= 1 && choice <= 5) {
[Link]("Enter first number: ");
a = [Link]();
[Link]("Enter second number: ");
b = [Link]();
try {
switch (choice) {
case 1:
[Link]("Result: " + [Link](a, b));
break;
case 2:
[Link]("Result: " + [Link](a, b));
break;
case 3:
[Link]("Result: " + [Link](a, b));
break;
case 4:
[Link]("Result: " + [Link](a, b));
break;
case 5:
[Link]("Result: " + [Link](a, b));
break;
catch (ArithmeticException e) {
[Link](“Error:”+[Link]());
} else if (choice != 6) {
[Link]("Invalid choice! Please try again.");
} while (choice != 6);
[Link]("Exiting program. Goodbye!");
[Link]();
[Link] shapes that contains classes Circle, Square, and Rectangle. Each class
should have methods to calculate area and perimeter. Import the package in
another program and display results.
package shapes;
public class Circle {
private double radius;
public Circle(double radius) {
[Link] = radius;
public double area() {
return [Link] * radius * radius;
public double perimeter() {
return 2 * [Link] * radius;
package shapes;
public class Square {
private double side;
public Square(double side) {
[Link] = side;
public double area() {
return side * side;
public double perimeter() {
return 4 * side;
package shapes;
public class Rectangle {
private double length, width;
public Rectangle(double length, double width) {
[Link] = length;
[Link] = width;
public double area() {
return length * width;
public double perimeter() {
return 2 * (length + width);
import [Link];
import [Link];
import [Link];
public class TestShapes {
public static void main(String[] args) {
Circle circle = new Circle(5);
Square square = new Square(4);
Rectangle rectangle = new Rectangle(6, 3);
[Link]("Circle Area: " + [Link]());
[Link]("Circle Perimeter: " + [Link]());
[Link]("Square Area: " + [Link]());
[Link]("Square Perimeter: " + [Link]());
[Link]("Rectangle Area: " + [Link]());
[Link]("Rectangle Perimeter: " + [Link]());
[Link] program to print lowercase letters and uppercase
letters from two different threads with suitable delay.
classLowerCasePrinter extendes Thread {
public void run() {
for (char a = 'a'; a <= 'z'; a++) {
[Link](c + " ");
try{
[Link](200);
} catch (InterruptedException e) {
[Link]("LowerCase thread interrupted");
class UpperCasePrinter extends Thread {
public void run() {
for (char c= 'A'; c<= 'Z'; c++) {
[Link]( + " ");
try{
[Link](300);
} catch (InterruptedExaeption e) {
[Link]("UpperCase thread interrupted");
Public class LetterPrinter {
public static void main(String[] args) {
LowerCasePrinter lower = new LowerCasePrinter();
UpperCasePrinter upper = new UpperCasePrinter();
[Link]();
[Link]();
21. print odd and even numbers less than 100 using multithreading
class OddThread extends Thread {
public void run() {
[Link]("Odd Numbers:");
for (int i = 1; i < 100; i += 2) {
[Link](i + " ");
try {
[Link](50);
} catch (InterruptedException e) {
[Link](e);
[Link]();
class EvenThread extends Thread {
public void run() {
[Link]("Even Numbers:");
for (int i = 2; i < 100; i += 2) {
[Link](i + " ");
try {
[Link](50);
} catch (InterruptedException e) {
[Link](e);
[Link]();
public class OddEvenMultithread {
public static void main(String[] args) {
OddThread odd = new OddThread();
EvenThread even = new EvenThread();
[Link]();
[Link]();
}}
[Link] program to accept an integer in a textbox then reverse that number and
display the result in the second textbox
import [Link].*;
import [Link].*;
public class ReverseNumberAWT extends Frame implements ActionListener {
TextField inputField, outputField;
Button reverseButton;
public ReverseNumberAWT() {
setLayout(new FlowLayout());
Label inputLabel = new Label("Enter Number: ");
inputField = new TextField(10);
Label outputLabel = new Label("Reversed Number: ");
outputField = new TextField(10);
[Link](false);
reverseButton = new Button("Reverse");
add(inputLabel);
add(inputField);
add(outputLabel);
add(outputField);
add(reverseButton);
[Link](this);
setTitle("Reverse Number - AWT");
setSize(300, 150);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});
public void actionPerformed(ActionEvent e) {
try {
int num = [Link]([Link]());
int reversed = 0;
while (num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
[Link]([Link](reversed));
} catch (NumberFormatException ex) {
[Link]("Invalid Input");
public static void main(String[] args) {
new ReverseNumberAWT();
21..Write a swing program to accept an integer in a textbox then reverse that
number and display the result in the second textbox?
import [Link].*;
import [Link].*;
public class ReverseNumberSwing {
public static void main(String[] args) {
JFrame frame = new JFrame("Reverse Number");
[Link](300, 200);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](null);
JLabel lblInput = new JLabel("Enter number:");
[Link](20, 20, 100, 30);
[Link](lblInput);
JTextField txtInput = new JTextField();
[Link](130, 20, 120, 30);
[Link](txtInput);
JLabel lblOutput = new JLabel("Reversed:");
[Link](20, 70, 100, 30);
[Link](lblOutput);
JTextField txtOutput = new JTextField();
[Link](130, 70, 120, 30);
[Link](false);
[Link](txtOutput);
JButton btnReverse = new JButton("Reverse");
[Link](90, 120, 100, 30);
[Link](btnReverse);
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
int num = [Link]([Link]());
int reversed = 0;
while (num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
[Link]([Link](reversed));
} catch (NumberFormatException ex) {
[Link](frame, "Please enter a valid integer");
});
[Link](true);
22. human face
import [Link];
import [Link].*;
public class HumanFace extends Applet {
public void paint(Graphics g) {
setBackground([Link]);
[Link]([Link]);
[Link](100, 50, 200, 250);
[Link]([Link]);
[Link](150, 120, 40, 30);
[Link](210, 120, 40, 30);
[Link]([Link]);
[Link](165, 130, 10, 10);
[Link](225, 130, 10, 10);
[Link](145, 115, 190, 110);
[Link](210, 110, 255, 115);
[Link]([Link]);
[Link](200, 150, 190, 190);
[Link](190, 190, 200, 190);
[Link]([Link]);
[Link](160, 200, 80, 40, 0, -180);
[Link]([Link]);
[Link](85, 150, 25, 50);
[Link](290, 150, 25, 50);
<html>
<body>
<applet code="[Link]" width="400" height="400">
</applet>
</body>
</html>
23. national flag of India.
import [Link];
import [Link].*;
public class IndianFlag extends Applet {
public void paint(Graphics g) {
setBackground([Link]);
[Link]([Link]);
[Link](50, 50, 10, 250);
[Link]([Link]);
[Link](60, 50, 200, 50);
[Link]([Link]);
[Link](60, 150, 200, 50);
[Link]([Link]);
[Link](60, 150, 200, 50);
[Link]([Link]);
[Link](145, 105, 30, 30);
for (int i = 0; i < 24; i++) {
double angle = 2 * [Link] * i / 24;
int x1 = 160;
int y1 = 120;
int x2 = (int) (160 + 15 * [Link](angle));
int y2 = (int) (120 + 15 * [Link](angle));
[Link](x1, y1, x2, y2);
<html>
<body>
<applet code="[Link]" width="350" height="350">
</applet>
</body>
</html>
24. traffic signal
import [Link];
import [Link].*;
public class TrafficSignal extends Applet {
public void paint(Graphics g) {
setBackground([Link]);
[Link]([Link]);
[Link](100, 50, 100, 260);
[Link]([Link]);
[Link](120, 70, 60, 60);
[Link]([Link]);
[Link](120, 150, 60, 60);
[Link]([Link]);
[Link](120, 230, 60, 60);
[Link]([Link]);
[Link](145, 310, 10, 100);
<html>
<body>
<applet code="[Link]" width="300" height="450">
</applet>
</body>
</html>
25. print star
import [Link];
import [Link];
import [Link];
import [Link];
/* <applet code="StarApplet" width=400 height=400>
</applet> */
public class StarApplet extends Applet {
public void paint(Graphics g) {
int x[] = {200, 220, 270, 230, 240, 200, 160, 170, 130, 180};
int y[] = {100, 160, 160, 190, 250, 210, 250, 190, 160, 160};
Polygon star = new Polygon(x, y, [Link]);
[Link]([Link]); // Fill with yellow
[Link](star);
[Link]([Link]);
[Link](star);
<body>
<applet code="[Link]" width="400" height="400">
</applet>
</body>
</html>
[Link] ring
import [Link];
import [Link];
import [Link];
<applet code "[Link]" width="500" height="300">
</applet>
public class OlympicRings extends Applet {
public void paint(Graphics g) {
[Link]([Link]);
[Link](50, 50, 100, 100);
[Link]([Link]); [Link](170, 50, 100, 100);
[Link]([Link]); [Link](290, 50, 100, 100);
[Link]([Link]);
[Link] (110, 100, 100, 100);
[Link]([Link]); [Link](230, 100, 100, 100);
<html>
<body>
<applet code "[Link]" width="500" height="300">
</applet>
</body>
</html>