[go: up one dir, main page]

0% found this document useful (0 votes)
9 views53 pages

Java Experiments

The document contains a series of Java programming experiments authored by Deepak Yadav, each demonstrating different programming concepts such as printing messages, calculating sums and averages, determining odd/even numbers, reversing numbers, and implementing Fibonacci series. It also includes experiments on factorial calculations (both iterative and recursive), matrix operations, employee details input, palindrome checking, and prime number generation. Each experiment is structured with a package declaration, class definition, and a main method showcasing the respective functionality.

Uploaded by

vmwtl8gsp6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views53 pages

Java Experiments

The document contains a series of Java programming experiments authored by Deepak Yadav, each demonstrating different programming concepts such as printing messages, calculating sums and averages, determining odd/even numbers, reversing numbers, and implementing Fibonacci series. It also includes experiments on factorial calculations (both iterative and recursive), matrix operations, employee details input, palindrome checking, and prime number generation. Each experiment is structured with a package declaration, class definition, and a main method showcasing the respective functionality.

Uploaded by

vmwtl8gsp6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 53

Experiment 1

Write an experiment to print “ HELLO WORLD ” using java.

package Deepak;

public class Myclass {

public static void main(String[] args) {

System.out.println("Hello Dino");

1 Deepak Yadav(231302069)
Experiment 2

Write an experiment to print ODD or EVEN of a given number.

package Deepak;

import java.util.Scanner;

public class Oddeven {

public static void main(String[]args) {

java.util.Scanner scanner = new Scanner(System.in);

int number = scanner.nextInt();

if(number % 2 == 0) {

System.out.println(number + "is Even.");

} else {

System.out.println(number + "is Odd.");

scanner.close();

}
}

2 Deepak Yadav(231302069)
Experiment 3
Write an experiment to print sum and average of 3 numbers.

package Deepak;

import java.util.Scanner;

public class sumandavg {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Enter First Number:");

int num1 = scanner.nextInt();

System.out.println("Enter Second Number:");

int num2 = scanner.nextInt();

System.out.println("Enter Third Number:");

int num3 = scanner.nextInt();

int sum = num1 + num2 + num3;

double avg = sum / 3.0;

System.out.println("Sum = " + sum);

3 Deepak Yadav(231302069)
System.out.println("Average = " + avg);

scanner.close();

}
}

Experiment 4

4 Deepak Yadav(231302069)
Write an experiment to print reverse of a number.

package Deepak;
import java.util.Scanner;

public class Reverseofnum{


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter a number: ");


int number = scanner.nextInt();

int reverse = 0;

while (number != 0) {
int digit = number % 10;
reverse = reverse * 10 + digit;
number /= 10;
}

System.out.println("Reversed Number: " + reverse);

scanner.close();
}
}

Experiment 5

5 Deepak Yadav(231302069)
Write an experiment to print sum of digit of a given number.
package Deepak;
import java.util.Scanner;

public class Sumofdigits {


public static void main(String[]args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Number: ");

int number = scanner.nextInt();

int sum = 0;
while (number != 0) {
int digit = number % 10;
sum += digit;
number /= 10;
}

System.out.println("Sum of digits: " + sum);

scanner.close();

Experiment 6
Write an experiment for Fibonacci series

6 Deepak Yadav(231302069)
package Deepak;
import java.util.Scanner;

public class Fibonacci {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of terms: ");
int n = scanner.nextInt();

int first = 0, second = 1;


System.out.println("Fibonacci Series: ");

for (int i = 0; i < n; i++) {


System.out.print(first + " ");
int next = first + second;
first = second;
second = next;
}

scanner.close();
}
}

Experiment 7

7 Deepak Yadav(231302069)
Write an experiment for Factorial of a given number (Using Iteration).
package Deepak;

import java.util.Scanner;

public class factoiteration{

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter a number to find its factorial: ");

int n = scanner.nextInt();

long factorial = 1;

if (n < 0) {

System.out.println("Factorial is not defined for negative numbers.");

} else {

for (int i = 1; i <= n; i++) {

factorial *= i;

System.out.println("Factorial of " + n + " is: " + factorial);

scanner.close();

8 Deepak Yadav(231302069)
Experiment 8

Write an experiment for Factorial of a given number (Using Recursion).

package Deepak;

9 Deepak Yadav(231302069)
import java.util.Scanner;

public class factorecur {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter a number to find its factorial: ");

int n = scanner.nextInt();

if (n < 0) {

System.out.println("Factorial is not defined for negative numbers.");

} else {

System.out.println("Factorial of " + n + " is: " + factorial(n));

scanner.close();

public static long factorial(int n) {

if (n == 0 || n == 1) {

return 1;

return n * factorial(n - 1);

10 Deepak Yadav(231302069)
Experiment 9

Write an experiment for multiplication of a number.


package Deepak;

import java.util.Scanner;

public class multnum {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter a number to print its multiplication : ");

11 Deepak Yadav(231302069)
int number = scanner.nextInt();

System.out.println("Multiplication of " + number + ":");


for (int i = 1; i <= 10; i++) {
System.out.println(number + " x " + i + " = " + (number * i));
}

scanner.close();
}
}

Experiment 10

Write an experiment for square series from 1 to 100.


package Deepak;

public class numsq {


public static void main(String[] args) {
System.out.println("Square Series from 1 to 100:");

for (int i = 1; i <= 100; i++) {


int square = i * i;
System.out.println("Square of " + i + " = " + square);

12 Deepak Yadav(231302069)
}
}
}

13 Deepak Yadav(231302069)
14 Deepak Yadav(231302069)
Experiment 11

Write an experiment for multiplication of an array.

package Deepak;

import java.util.Scanner;

public class multarray {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number of elements in the array: ");

int n = scanner.nextInt();

int[] arr = new int[n];

System.out.println("Enter " + n + " elements:");

for (int i = 0; i < n; i++) {

arr[i] = scanner.nextInt();

long product = 1;

for (int i = 0; i < n; i++) {

product *= arr[i];

15 Deepak Yadav(231302069)
System.out.println("Product of all elements in the array: " + product);

scanner.close();

16 Deepak Yadav(231302069)
Experiment 12

Write an experiment to find the Fibonacci series using recursive and non-
recursive functions.
package Deepak;

import java.util.Scanner;

public class FibonacciExample {

// Recursive Method

public static int fibonacciRecursive(int n) {

if (n <= 1) {

return n;

return fibonacciRecursive(n - 1) + fibonacciRecursive(n - 2);

// Non-Recursive (Iterative) Method

public static void fibonacciNonRecursive(int n) {

int first = 0, second = 1;

System.out.print("Fibonacci Series (Non-Recursive): ");

for (int i = 0; i < n; i++) {

System.out.print(first + " ");

int next = first + second;

17 Deepak Yadav(231302069)
first = second;

second = next;

System.out.println();

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number of terms for the Fibonacci series: ");

int n = scanner.nextInt();

fibonacciNonRecursive(n);

System.out.print("Fibonacci Series (Recursive): ");

for (int i = 0; i < n; i++) {

System.out.print(fibonacciRecursive(i) + " ");

scanner.close();

18 Deepak Yadav(231302069)
19 Deepak Yadav(231302069)
Experiment 13

Write an experiment to multiply two given matrices.


package Deepak;

import java.util.Scanner;

public class Matricesmult {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter number of rows for first matrix: ");

int rows1 = scanner.nextInt();

System.out.print("Enter number of columns for first matrix / rows for second matrix: ");

int cols1 = scanner.nextInt();

System.out.print("Enter number of columns for second matrix: ");

int cols2 = scanner.nextInt();

int[][] matrix1 = new int[rows1][cols1];

int[][] matrix2 = new int[cols1][cols2];

int[][] result = new int[rows1][cols2];

20 Deepak Yadav(231302069)
System.out.println("Enter elements of first matrix:");

for (int i = 0; i < rows1; i++) {

for (int j = 0; j < cols1; j++) {

matrix1[i][j] = scanner.nextInt();

System.out.println("Enter elements of second matrix:");

for (int i = 0; i < cols1; i++) {

for (int j = 0; j < cols2; j++) {

matrix2[i][j] = scanner.nextInt();

for (int i = 0; i < rows1; i++) {

for (int j = 0; j < cols2; j++) {

result[i][j] = 0;

for (int k = 0; k < cols1; k++) {

result[i][j] += matrix1[i][k] * matrix2[k][j];

21 Deepak Yadav(231302069)
System.out.println("Resultant Matrix after Multiplication:");

for (int i = 0; i < rows1; i++) {

for (int j = 0; j < cols2; j++) {

System.out.print(result[i][j] + " ");

System.out.println();

scanner.close();

22 Deepak Yadav(231302069)
Experiment 14

Write an experiment to display the employee details using Scanner class.

package Deepak;

import java.util.Scanner;

public class EmployeeDetails {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter Employee ID: ");

int empId = scanner.nextInt();

scanner.nextLine();

System.out.print("Enter Employee Name: ");

String empName = scanner.nextLine();

System.out.print("Enter Employee Department: ");

String empDepartment = scanner.nextLine();

System.out.print("Enter Employee Salary: ");

double empSalary = scanner.nextDouble();

23 Deepak Yadav(231302069)
System.out.println("\n--- Employee Details ---");

System.out.println("Employee ID: " + empId);

System.out.println("Employee Name: " + empName);

System.out.println("Employee Department: " + empDepartment);

System.out.println("Employee Salary: Rs" + empSalary);

scanner.close();

24 Deepak Yadav(231302069)
Experiment 15

Write an experiment to check whether a given string is a palindrome or not.


package Deepak;

import java.util.Scanner;

public class palindrome {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter a string to check if it is a palindrome: ");

String str = scanner.nextLine();

str = str.toLowerCase();

if (isPalindrome(str)) {

System.out.println("The given string is a palindrome.");

} else {

System.out.println("The given string is not a palindrome.");

scanner.close();

25 Deepak Yadav(231302069)
}

public static boolean isPalindrome(String str) {

int left = 0;

int right = str.length() - 1;

while (left < right) {

if (str.charAt(left) != str.charAt(right)) {

return false;

left++;

right--;

return true;

26 Deepak Yadav(231302069)
Experiment 16

Write an experiment which gives the sum of two matrices.


package Deepak;

import java.util.Scanner;

public class sumofmatrices {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number of rows: ");

int rows = scanner.nextInt();

System.out.print("Enter the number of columns: ");

int cols = scanner.nextInt();

int[][] matrix1 = new int[rows][cols];

int[][] matrix2 = new int[rows][cols];

int[][] sumMatrix = new int[rows][cols];

System.out.println("Enter elements of first matrix:");

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {


27 Deepak Yadav(231302069)
matrix1[i][j] = scanner.nextInt();

System.out.println("Enter elements of second matrix:");

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

matrix2[i][j] = scanner.nextInt();

// Matrix Addition

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

sumMatrix[i][j] = matrix1[i][j] + matrix2[i][j];

System.out.println("Sum of the two matrices:");

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

System.out.print(sumMatrix[i][j] + " ");

28 Deepak Yadav(231302069)
System.out.println();

scanner.close();

29 Deepak Yadav(231302069)
Experiment 17

Write an experiment to print Prime numbers till 100.

package Deepak;

public class primenum {

public static void main(String[] args) {

System.out.println("Prime numbers between 1 and 100 are:");

for (int num = 2; num <= 100; num++) {

if (isPrime(num)) {

System.out.print(num + " ");

public static boolean isPrime(int num) {

if (num <= 1) {

return false;

for (int i = 2; i <= Math.sqrt(num); i++) {

if (num % i == 0) {

return false;

30 Deepak Yadav(231302069)
return true;

31 Deepak Yadav(231302069)
EXPERIMENT - 18
Aim: Write a program to implement for each loop

package Deepak;

public class foreachloop {

public static void main(String[] args) {

int[] numbers = {2021, 2022, 2023, 2024, 2025};

System.out.println("Using for-each loop to print array elements:");

for (int num : numbers) {

System.out.println("Number: " + num);

32 Deepak Yadav(231302069)
EXPERIMENT - 19
Aim: Write a program to implement a constructor.

package Deepak;

public class constructor{

// Instance variables

String name;

int age;

// Constructor

constructor(String n, int a) {

name = n;

age = a;

void displayDetails() {

System.out.println("Name: " + name);

System.out.println("Age: " + age);

// Main method

public static void main(String[] args) {

// Creating an object and calling the constructor

constructor obj = new constructor("DinoHelic", 20);

obj.displayDetails();

33 Deepak Yadav(231302069)
34 Deepak Yadav(231302069)
EXPERIMENT - 20
Aim: Write a program to implement a destructor.

package Deepak;

public class Destructor {

String model;

int year;

// Constructor

public Destructor(String model, int year) {

this.model = model;

this.year = year;

void display() {

System.out.println("Car Model: " + model);

System.out.println("Manufacturing Year: " + year);

// Destructor (simulated using finalize)

@Override

protected void finalize() throws Throwable {

System.out.println("Destructor called: Object is being garbage collected");

public static void main(String[] args) {

Destructor car = new Destructor("Mercedes AMG g63", 2024);

car.display();

35 Deepak Yadav(231302069)
// Making the object eligible for garbage collection

car = null;

System.gc(); // Requesting garbage collection

EXPERIMENT - 21
36 Deepak Yadav(231302069)
Aim: Write a program to implement a method overloading.

package Deepak;

public class overloading {

// Method with 2 integer parameters

void display(int a, int b) {

System.out.println("Sum of two integers: " + (a + b));

// Method with 2 double parameters

void display(double a, double b) {

System.out.println("Sum of two doubles: " + (a + b));

// Method with 3 integer parameters

void display(int a, int b, int c) {

System.out.println("Sum of three integers: " + (a + b + c));

// ✅ Overloaded method to merge two strings

void display(String str1, String str2) {

System.out.println("Merged String: " + str1 + str2);

// Main method

public static void main(String[] args) {

overloading obj = new overloading();

// Calling overloaded methods

37 Deepak Yadav(231302069)
obj.display(20, 25);

obj.display(2.5, 2.5);

obj.display(2, 0, 5);

obj.display("Good, ","Morning!");

EXPERIMENT - 22
Aim:Write a program to implement a constructor overloading

38 Deepak Yadav(231302069)
package Deepak;

//Filename: Car.java

public class constructoroverloading {

String model;

String brand;

int year;

// Constructor 1: Only model

constructoroverloading(String model) {

this.model = model;

this.brand = "Unknown";

this.year = 0;

// Constructor 2: Model and brand

constructoroverloading(String model, String brand) {

this.model = model;

this.brand = brand;

this.year = 0;

// Constructor 3: Model, brand and year

constructoroverloading(String model, String brand, int year) {

this.model = model;

this.brand = brand;

this.year = year;

39 Deepak Yadav(231302069)
}

// Method to display car details

void display() {

System.out.println("Car Model: " + model);

System.out.println("Car Brand: " + brand);

System.out.println("Manufacturing Year: " + year);

System.out.println("------------------------------");

// Main method

public static void main(String[] args) {

// Creating objects using different constructors

constructoroverloading car1 = new constructoroverloading("Model X");

constructoroverloading car2 = new constructoroverloading("Civic", "Honda");

constructoroverloading car3 = new constructoroverloading("Mustang", "Ford", 2023);

// Display details

car1.display();

car2.display();

car3.display();

40 Deepak Yadav(231302069)
EXPERIMENT – 23
41 Deepak Yadav(231302069)
Aim: Write a program to implement copy constructor.

package Deepak;

//Copy Cunstructor

public class Officer {

String name;

String department;

int age;

// Parameterized constructor

Officer(String n, String dept, int a) {

name = n;

department = dept;

age = a;

// ✅ Copy constructor

Officer(Officer o) {

name = o.name;

department = o.department;

age = o.age;

// Method to display officer details

void display() {

System.out.println("Officer Name: " + name);

System.out.println("Department : " + department);

42 Deepak Yadav(231302069)
System.out.println("Age : " + age);

System.out.println("----------------------------");

public static void main(String[] args) {

// Creating first object using parameterized constructor

Officer officer1 = new Officer("Rahul", "Traffic", 35);

// Creating second object using copy constructor

Officer officer2 = new Officer(officer1);

// Displaying details

System.out.println("Original Officer:");

officer1.display();

System.out.println("Copied Officer:");

officer2.display();

43 Deepak Yadav(231302069)
44 Deepak Yadav(231302069)
EXPERIMENT – 24
Aim: Write a program to implement a method overriding.

package Deepak;

class Tv

void showDetails()

System.out.println("Tv");

class SmartTv extends Tv

@Override

void showDetails()

System.out.println("SmartTv");

public class methodoverriding {

public static void main(String[] args) {

SmartTv t=new SmartTv();

t.showDetails();

45 Deepak Yadav(231302069)
}

46 Deepak Yadav(231302069)
EXPERIMENT – 25
Aim: Write a program to implement Hierarchical Inheritance.

package Deepak;

//Superclass

class Vehicle {

public void start() {

System.out.println("Vehicle is starting...");

//Subclass 1

class Car extends Vehicle {

public void drive() {

System.out.println("Car is driving...");

//Subclass 2

class Bike extends Vehicle {

public void ride() {

System.out.println("Bike is riding...");

//Main class to run the program

47 Deepak Yadav(231302069)
public class HierarchicalInheritance {

public static void main(String[] args) {

// Car object

Car car = new Car();

car.start(); // Inherited from Vehicle

car.drive(); // Specific to Car

// Bike object

Bike bike = new Bike();

bike.start(); // Inherited from Vehicle

bike.ride(); // Specific to Bike

48 Deepak Yadav(231302069)
EXPERIMENT – 26
Aim: Write a program to implement an Abstract class.

package Deepak;

// Abstract class

abstract class Shape {

// Abstract method (no body)

public abstract void draw();

// Concrete method

public void display() {

System.out.println("This is a shape.");

// Subclass 1

class Circle extends Shape {

// Implementing abstract method

@Override

public void draw() {

System.out.println("Drawing a Circle.");

// Subclass 2

class Rectangle extends Shape {

49 Deepak Yadav(231302069)
// Implementing abstract method

@Override

public void draw() {

System.out.println("Drawing a Rectangle.");

// Main class to run the program

public class Abstract {

public static void main(String[] args) {

// Using abstract class reference to refer to subclass objects

Shape shape1 = new Circle();

shape1.display();

shape1.draw();

Shape shape2 = new Rectangle();

shape2.display();

shape2.draw();

50 Deepak Yadav(231302069)
EXPERIMENT – 26
Aim: Write a program to implement Interfaces using Extends.

package Deepak;

// Base interface

interface Device {

void powerOn();

// Extended interface

interface SmartDevice extends Device {

void connectToInternet();

// Class implementing the extended interface

class Mobile implements SmartDevice {

@Override

public void powerOn() {

System.out.println("Mobile is powering on...");

@Override

public void connectToInternet() {

System.out.println("Mobile is connected to the internet.");

51 Deepak Yadav(231302069)
// Main class to run the program

public class interfaces {

public static void main(String[] args) {

Mobile myPhone = new Mobile();

myPhone.powerOn(); // From Device interface

myPhone.connectToInternet(); // From SmartDevice interface

52 Deepak Yadav(231302069)
EXPERIMENT – 27
Aim: Write a program to implement an Abstract class.

53 Deepak Yadav(231302069)

You might also like