Java

Java Practical 1 - Check Prime Number

Program

import java.util.Scanner;

public class Assi2
{
    public static void main(String[] args)
    {
        int n;

        System.out.print("Enter the number ");
        Scanner sc = new Scanner(System.in);

        n = sc.nextInt();

        boolean isPrime = true;

        for(int i = 2; i <= n / 2; i++)
        {
            if(n % i == 0)
            {
                isPrime = false;
                break;
            }
        }

        if(isPrime)
        {
            System.out.println("Given number " + n + " is Prime");
        }
        else
        {
            System.out.println("Given number " + n + " is Not Prime");
        }
    }
}

Output

Enter the number : 7
Given number 7 is Prime

Java Practical 2 - Fibonacci Series

Program

import java.util.Scanner;

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

        System.out.print("Enter the number ");
        int n = sc.nextInt();

        int a = 0;
        int b = 1;

        System.out.print(a + " " + b);

        for(int i = 3; i <= n; i++)
        {
            int c = a + b;

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

            a = b;
            b = c;
        }

        System.out.println();
    }
}

Output

Enter the number : 10
0 1 1 2 3 5 8 13 21 34

Java Practical 3 - Factorial Using Recursion

Program

import java.util.Scanner;

public class Assi4
{
    public static void main(String[] args)
    {
        System.out.print("Enter value of n : ");

        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        System.out.println("Factorial of " + n + " : " + fact(n));
    }

    static long fact(int num)
    {
        if(num == 0 || num == 1)
        {
            return 1;
        }

        return num * fact(num - 1);
    }
}

Output

Enter value of n : 5
Factorial of 5 : 120

Java Practical 4 - Command Line Arguments

Program

public class Assi5
{
    public static void main(String[] args)
    {
        int n = args.length;

        for(int i = 0; i < n; i++)
        {
            System.out.println("Argument no : " + (i + 1) + " " + args[i]);
        }
    }
}

Output

Argument no : 1 Two
Argument no : 2 Two
Argument no : 3 Three

Java Practical 5 - Class and Object

Program

class Car
{
    String brand;
    long avg;

    void display()
    {
        System.out.println("Brand name = " + brand);
        System.out.println("Average = " + avg + " km/h");
    }
}

class Assi6
{
    public static void main(String[] args)
    {
        Car car1 = new Car();

        car1.brand = "Toyota";
        car1.avg = 23;

        car1.display();

        return;
    }
}

Output

Brand name = Toyota
Average = 23 km/h

Java Practical 6 - Default and Parameterized Constructor

Program

class Student
{
    String name;
    double per;

    Student()
    {
        name = "Ajay";
        per = 72.5;
    }

    Student(String n, double p)
    {
        name = n;
        per = p;
    }

    void display()
    {
        System.out.println("Name      : " + name);
        System.out.println("Percentage : " + per);
    }
}

class Assi7
{
    public static void main(String[] args)
    {
        Student s1 = new Student();
        Student s2 = new Student("Sanjay", 85.50);

        System.out.println("Object s1 :");
        s1.display();

        System.out.println("Object s2 :");
        s2.display();
    }
}

Output

Object s1 :
Name      : Ajay
Percentage : 72.5

Object s2 :
Name      : Sanjay
Percentage : 85.5

Java Practical 7 - Method Overloading

Program

class calculator
{
    int add(int a, int b)
    {
        return a + b;
    }

    int add(int a, int b, int c)
    {
        return a + b + c;
    }

    double add(double a, double b)
    {
        return a + b;
    }
}

class Assi8
{
    public static void main(String[] args)
    {
        calculator c1 = new calculator();

        System.out.println("Addition of two int add(5,7) : " + c1.add(5,7));

        System.out.println("Addition of three int add(2,4,7) : " + c1.add(2,4,7));

        System.out.println("Addition of two double add(50.25,25.70) : " + c1.add(50.25,25.70));
    }
}

Output

Addition of two int add(5,7) : 12
Addition of three int add(2,4,7) : 13
Addition of two double add(50.25,25.70) : 75.95

Java Practical 8 - User Defined Package

Program 1 - Main Class

import mypac.myclass;

class Assi9
{
    public static void main(String[] args)
    {
        myclass m1 = new myclass();

        m1.say_hi();
    }
}

Program 2 - Package Class

package mypac;

public class myclass
{
    public void say_hi()
    {
        System.out.println("Hi TYBSc 2026-27");
    }
}

Output

Hi TYBSc 2026-27

Java Practical 9 - Inheritance

Program

class Animal
{
    void eat()
    {
        System.out.println("The Animal eat food");
    }
}

class Dog extends Animal
{
    void bark()
    {
        System.out.println("The Dog barks");
    }
}

class Pug extends Dog
{
    void play()
    {
        System.out.println("The Pug is playing");
    }
}

class Cow extends Animal
{
    void milk()
    {
        System.out.println("The Cow gives milk");
    }
}

class Cat extends Animal
{
    void meow()
    {
        System.out.println("The Cat sound as meow");
    }
}

public class Assi10
{
    public static void main(String[] args)
    {
        System.out.println("Simple Inheritance: class Dog inherit class Animal");

        Dog d = new Dog();
        d.eat();
        d.bark();

        System.out.println("Multilevel Inheritance: class Dog inherit class Animal,while class Pug inherit Dog");

        Pug p = new Pug();
        p.eat();
        p.bark();
        p.play();

        System.out.println("Hierarchical Inheritance: class Animal inherit by classes Dog,Cow,Cat");

        Cow cw = new Cow();
        cw.eat();
        cw.milk();

        Cat ct = new Cat();
        ct.eat();
        ct.meow();
    }
}

Output

Simple Inheritance: class Dog inherit class Animal
The Animal eat food
The Dog barks

Multilevel Inheritance: class Dog inherit class Animal,while class Pug inherit Dog
The Animal eat food
The Dog barks
The Pug is playing

Hierarchical Inheritance: class Animal inherit by classes Dog,Cow,Cat
The Animal eat food
The Dog barks
The Cow gives milk
The Cat sound as meow

Java Practical 10 - Sum of Array Elements

Program

public class Assi11
{
    public static void main(String[] args)
    {
        int[] number = {10,20,30,40,50};

        int sum = 0;

        for(int num : number)
        {
            sum = sum + num;
        }

        for(int i = 0; i < number.length; i++)
        {
            System.out.print(number[i] + " ");
        }

        System.out.println();

        System.out.println("Sum of all elements = " + sum);
    }
}

Output

10 20 30 40 50
Sum of all elements = 150

Java Practical 11 - Division by Zero Exception

Program

import java.util.Scanner;

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

        try
        {
            System.out.print("Enter numerator ");
            int a = sc.nextInt();

            System.out.print("Enter denominator ");
            int b = sc.nextInt();

            int result = a / b;

            System.out.println("Result : " + result);
        }
        catch(ArithmeticException e)
        {
            System.out.println("Error : Division by Zero is not allowed");
        }
        finally
        {
            System.out.println("Program exception completed");
        }

        sc.close();
    }
}

Output

Enter numerator : 10
Enter denominator : 0
Error : Division by Zero is not allowed
Program exception completed

Comments