Java Practicals

·

11 min read

1. Write a Java Program find the area of circle.

2. Write a Java Program that will display factorial of the given number.

3. Write a Java Program that will find the largest no from the given two nos.

4. Write a Java Program that will find the largest no from the given three nos.

5. Write a Java Program that shows the use of switch Statement.

6. Write a Java Program to find the sum of the digits of given number.

7. Write a Java Program that will display the Sum of 1+1/2+1/3.....+1/n.

8. Write a Java Program that check weather the given no is prime or not.

9. Write a Java Program that implements the use of break statement.

10. Write a Java Program that implements the use of continue statement.

11. Write a Java Program that will accept Command-line Arguments and display the same.

12. Write a Java Program to sort the elements of an array in Ascending Order.

13. Write a Java Program to create a Student class and generate result of student (Total, Per, Grade).

14. Write a Java Program to create an Employee class and generate Salary Slip for the employee.

15. Write a java program which shows the use of Static Members.

16. Write a java program which shows the Nesting of Methods.

17. Write a java program which shows the use of Methods Overloading.

18. Write a java program which implements the Default Constructors.

19. Write a java program which implements the Parameterized Constructors.

20. Write a java program which implements the Overloading of Constructors.

21. Write a java program which explains the concept of Single Inheritance.

22. Write a java program which explains the concept of Multilevel Inheritance.

23. Write a java program which explains the concept of Hierarchical Inheritance.

24. Write a java program which shows the Method Overriding.

25. Write a Java Program to implement final class and final method.

26. Write a Java Program to implement abstract class and abstract method.

27. Write a java program which implements Interface.

28. Write a java program which implements Multiple Interfaces.

29. Write a java program which shows importing of classes from other packages.

30. Write a Java Program to implement the methods of Math Class.

31. Write a Java Program to implement the methods of String Class.

32. Write a Java Program to implement the methods of Vector Class.

33. Write a Java Program to implement the methods of Stack Class.

34. Write a Java Program which will read a text and count all occurrences of a particular word.

35. Write a Java Program which will read a string and rewrite it in the alphabetical order. eg.The word “STRING” should be written a “GINRST”.

36. Write a java program which creates threads using the Thread Class.

37. Write a java program which shows the use of yield(), stop() and sleep() Methods.

38. Write a java program which shows the Priority in Threads.

39. Write a java program which use of Runnable Interface.

40. Write a java program which uses try and catch for Exception Handling.

41. Write a java program which uses Multiple catch Blocks.

42. Write a java program which uses finally Statement.

43. Write a java program which uses Nested try Statements.

44. Write a java program which shows throwing our own Exception.

45. Create an Applet program that print Hello Applet.

46. Create an applet that use init(),start(),stop() and destroy() methods of applet.

47. write an applet program to implement the concept of passing parameter to applet.

48. Write a applet program to implement various methods of Graphics class.


// Practical 1 ----------------------------------
import java.util.Scanner;
class areacircle
{
    int r;
    void getdata()
    {
        Scanner in=new Scanner(System.in);
        System.out.print("Enter radius:");
        r=in.nextInt();
    }
    void findarea()
    {
        double area,PI=3.14;
        area=PI*r*r;
        System.out.println("Area of circle is:"+area);
    }   
}
class java1
{
    public static void main(String args[])
    {
        areacircle c1=new areacircle();
        c1.getdata();
        c1.findarea();
    }
}

Output :

image.png // Practical 2 ----------------------------------

import java.util.Scanner;
class factorial
{
 int fact=1,no,i;
 void getdata()
 {
  Scanner in=new Scanner(System.in);
  System.out.print("Enter no for find factorial:");
  no=in.nextInt();
 }
 void findarea()
 {
  for(i=1;i<=no;i++)
  fact=fact*i;
  System.out.println("Factorial of given no is:"+fact);


 }  
}
class java2
{
 public static void main(String args[])
 {
  factorial f1=new factorial();
  f1.getdata();
  f1.findarea();
 } 
}

Output :

image.png // Practical 3 ----------------------------------

import java.util.Scanner;
class java3
{
    public static void main(String args[])
    {
        int no1,no2;
        Scanner in=new Scanner(System.in);
        System.out.println("Enter no1:");
        no1=in.nextInt();
        System.out.println("Enter no2:");
        no2=in.nextInt();
        if(no1>no2)
        {
            System.out.println("no1 is larger");
        }
        else
        {
            System.out.println("no2 is larger");
        }
    }
}

Output :

image.png

// Practical 4 ----------------------------------

import java.util.Scanner;
class java4
{
    public static void main(String args[])
    {
        int a,b,c;

        Scanner in=new Scanner(System.in);
        System.out.println("Enter value of a:");
        a=in.nextInt();
        System.out.println("Enter value of b:");
        b=in.nextInt();
        System.out.println("Enter value of c:");
        c=in.nextInt();
        if(a>b)
        {
            if(a>c)
            {
                System.out.println("value of a is larger");
            }
            else
            {
                System.out.println("value of c is larger");
            }
        }
        else
        {
            if(b>c)
            {
                System.out.println("value of b is larger");
            }
            else
            {
                System.out.println("value of c is larger");
            }
        }
    }
}

Output :

image.png // Practical 5 ----------------------------------

import java.util.Scanner;
class java5
{
    public static void main(String args[])
    {
        int choice;
        Scanner in=new Scanner(System.in);

        System.out.println("Enter choice:");
        choice=in.nextInt();
        switch(choice)
        {
            case 1: System.out.println("This is chocie 1");

            case 2: System.out.println("This is chocie 2");

            case 3: System.out.println("This is chocie 3");
                break;
            case 4: System.out.println("This is chocie 4");
                break;
            case 5: System.out.println("This is chocie 5");
                break;
            default:    System.out.println("Please enter right choice");
        }
    }
}

Output: image.png

// Practical 6 ----------------------------------

import java.util.Scanner;
class java6
{
    public static void main(String args[])
    {
        Scanner in=new Scanner(System.in);
        int no,r,sum=0;
        System.out.println("Enter the no:");
        no=in.nextInt();

        while(no>0)
        {
            r=no%10;
            no=no/10;
            sum=sum+r;
        }
        System.out.println("Sum of the digit is:"+sum);

    }

Output image.png

// Practical 7 ----------------------------------

import java.util.Scanner;
class java7
{
    public static void main(String args[])
    {
        int no,i,c;
        int a=0,b=1;

        float sum=0;
        System.out.print(a+""+b);

        for(i=1;i<=10;i++)
        {
            c=a+b;
            System.out.print(c);
            a=b;
            b=c;

        }

    }
}

Output image.png

// Practical 8 ----------------------------------

import java.util.Scanner;
class java8
{
    public static void main(String args[])
    {
        int no,i;
        int temp=0;
        Scanner in=new Scanner(System.in);
        System.out.println("Enetr the no:");
        no=in.nextInt();

        for(i=2;i<=no-1;i++)
        {
            if(no%i==0)
            {
                temp=temp+1;

            }
        }

        if(temp>0)
        {
            System.out.print("no is not prime");
        }
        else
        {
            System.out.print("no is prime");
        }        
    }
}

Output

image.png

// Practical 9 ----------------------------------

import java.util.Scanner;
class java9
{
    public static void main(String args[])
    {
        int no,i;
        Scanner in=new Scanner(System.in);
        System.out.println("Enetr the no:");
        no=in.nextInt();
        for(i=1;i<=no;i++)
        {
            if(i==7)
            {
                break;
            }
            System.out.println(i);
        }
    }
}

Output: image.png

// Practical 10 ----------------------------------

import java.util.Scanner;
class java10
{
    public static void main(String args[])
    {
        int no,i;
        Scanner in=new Scanner(System.in);
        System.out.println("Enetr the no:");
        no=in.nextInt();
        for(i=1;i<=no;i++)
        {
            if(i==11)
            {
                continue;
            }
            System.out.println(i);
        }
    }
}

Output image.png

// Practical 11

class java11
{
    public static void main(String args[])
    {
        for(String s:args)
        {
            System.out.println(s);
        }
    }
}

Output

image.png

// Practical 12 ----------------------------------

import java.util.Scanner;
class java12
{
    public static void main(String args[])
    {
        Scanner in=new Scanner(System.in);
        int i,j,element,temp,no;
        int arr[]=new int[10];

        System.out.println("Enter the number of an array:- ");
        no=in.nextInt();

        for(i=1;i<=no;i++)
        {
            System.out.println("Enter the element:");
            arr[i]=in.nextInt();
        }

        for(i=1;i<=no;i++)
            {
                   for(j=i;j<=no;j++)
                   {
                           if(arr[i] > arr[j])
                         {
                                 temp=arr[i];
                                 arr[i]=arr[j];
                                 arr[j]=temp;
                    }
                       }
            }
        System.out.println("Sorted list in ascending order:");
         for(i=1;i<=no;i++)
        {
            System.out.println(arr[i]);
        }

    }
}

Output

image.png

// Practical 13 ----------------------------------

import java.util.Scanner;
class Student
{
    int java,se,ec,net,t;
    float per;
    String result,grd;
    void getmarks()
    {
        Scanner in=new Scanner(System.in);

        System.out.println("Enter the java marks:");
        java=in.nextInt();

        System.out.println("Enter the software engineering marks:");
        se=in.nextInt();

        System.out.println("Enter the e-commerce marks:");
        ec=in.nextInt();

        System.out.println("Enter the net framework marks:");
        net=in.nextInt();
    }
    void total()
    {
        t=java+se+ec+net;
        System.out.println("From Total 400 get:"+t);
    }

    void percentage()
    {
        per=t/4;
        System.out.println("Percentage:"+per);
    }

    void result()
    {
        if(java<35 || se<35 || ec<35 || net<35)
        {
            System.out.println(result="Fail");
        }
        else
        {
            System.out.println(result="Pass");
        }
    }

    void grade()
    {
        if(result=="Pass")
        {
            if(per>=70.0 && per<=100.0)
            {
                System.out.println("Student Grade:A");  
            }
            else if(per>=60.0 && per<70.0)
            {
                System.out.println("Student Grade:B");
            }
            else if(per>=50.0 && per<60.0)
            {
                System.out.println("Student Grade:C");
            }
            else if(per>=40.0)
            {
                System.out.println("Student Grade:D");
            }
            else if(per>=35.0 && per<40.0)
            {
                System.out.println("Student Grade:E");
            }
        }
        else
        {
            System.out.println("Student Grade:F");      
        }   
    }
}
class java13
{
    public static void main(String args[])
    {
        Student s1=new Student();
        s1.getmarks();
        s1.total();
        s1.percentage();
        s1.result();
        s1.grade();
    }
}

Output

image.png

// Practical 14 ----------------------------------

import java.util.Scanner;
class empsalary
{   

    String name;
    int id,salary,t,p,h;
    Scanner in=new Scanner(System.in);      
    void heading()
    {
        String s="Employee Salary Sleep";   
        System.out.println("\t\t"+s);
    }
    void underline()
    {
        System.out.println("--------------------------------------------------------------");
    }

    void getdata()
    {
        System.out.println("Enter emp name:");
        name=in.nextLine();

        System.out.println("Enter emp ID:");
        id=in.nextInt();

        System.out.println("Enter emp salary:");
        salary=in.nextInt();            
    }

    void pflogic()
    {
        int pf=5;
        p=salary*pf/100;
        System.out.println("\t\tEmployee salary pf is:"+p);
    }

    void tddalogic()
    {
        int td=10;
        t=salary*td/100;
        System.out.println("\t\tEmployee salary tdda is:"+t);
    }

    void hralogic()
    {
        int hr=12;
        h=salary*hr/100;
        System.out.println("\t\tEmployee salary hra is:"+h);
    }

    void totalsalary()
    {
        int total;
        total=salary-p+t+h;
        System.out.println("The total salary of employee is:"+total);
    }
    void alldisplay()
    {
        System.out.println("\t\tEmployee id:"+id);
        System.out.println("\t\tEmployee name:"+name);
        System.out.println("\t\tEmployee salary:"+salary);
    }
    void nesting()
    {
        underline();
        heading();
        underline();
        getdata();
        heading();
        underline();
        alldisplay();
        pflogic();
        tddalogic();
        hralogic();
        underline();
        underline();
        totalsalary();
        underline();
    }
}


class java14
{
    public static void main(String args[])
    {
        empsalary emp1=new empsalary();
        empsalary emp2=new empsalary(); 
        empsalary emp3=new empsalary();
        emp1.nesting();
        emp2.nesting();
        emp3.nesting();
    }
}

Output image.png

// Practical 15 ----------------------------------

import java.util.Scanner;
class staticmembers
{
    static String sem="Semester 5";
    String name;
    int rno;
    void rno()
    {
        Scanner in=new Scanner(System.in);
        System.out.println("Enter rollno:-");
        rno=in.nextInt();
    }

}
class java15
{
    public static void main(String args[])
    {
        staticmembers s1=new staticmembers();
        s1.rno();
        String a=staticmembers.sem;
        System.out.println("Your semester of BCA is:-"+a);
    }   
}

Output

image.png

// Practical 16 ----------------------------------

import java.util.Scanner;
class nesting
{
    Scanner in=new Scanner(System.in);
    int a,b;
    void getdata()
    {
        System.out.println("Enter first value:-");
        a=in.nextInt();

        System.out.println("Enter second value:-");
        b=in.nextInt();
    }   

    void putdata()
    {
        int sum;
        sum=a+b;
        System.out.println("The sum is:-"+sum);
    }

    void nestingmethod()
    {
        getdata();
        putdata();
    }
}

class java16
{
    public static void main(String args[])
    {
        nesting n1=new nesting();
        n1.nestingmethod(); 
    }
}

// Practical 17 ----------------------------------

class overloading
{
    int x,y;
    void mul(int a)
    {
        x=a;
        System.out.println("The number is:"+x);
    }
    void mul(int a,int b)
    {
        x=a+b;

        System.out.println("The sum is:"+x);            
    }
}   
class java17
{
    public static void main(String args[])
    {
        overloading o1=new overloading();
        o1.mul(4);
        o1.mul(5,6);    
    }
}

// Practical 18 ----------------------------------

class Tester {
    int a;
    String b;

 }
 class java18
 {
     public static void main(String[] args) {

       //Default constructor
       //is called to create a new object
       Tester t = new Tester();
       //print the default values
       System.out.println(t.a);
       System.out.println(t.b);
    }
 }

Output

// Practical 19 ----------------------------------

class pconstructor 
{
    pconstructor(int x,int y)
    {
        System.out.println("Hi! I am parameterized constructor");
        int a,b,c;

        a=x;
        b=y;

        c=a+b;

        System.out.println("The sum is:"+c);        
    }
}

class java19
{
    public static void main(String args[])
    {
        pconstructor p1=new pconstructor(10,20);
    }
}

// Practical 20 ----------------------------------

class oconstructor 
{
    oconstructor()
    {
        System.out.println("Hi! I am default constructor"); 
    }

    oconstructor(int x)
    {
        int a=x;
        int b;
        b=a*a;
        System.out.println("The return value from single arg constructor:"+b);

    }

    oconstructor(int p,int q)
    {
        int j,k,s;
        j=p;k=q;
        s=j*k;
        System.out.println("The return value from double args constructor:"+s);
    }
}

class java20

{
    public static void main(String args[])
    {
        oconstructor o1=new oconstructor();
        oconstructor o2=new oconstructor(4);
        oconstructor o3=new oconstructor(4,5);
    }
}

// Practical 21

import java.util.Scanner;
class Singleinheritance
{
    Scanner in=new Scanner(System.in);
    String sname,hobby,sem;
    int rno;

    void student()
    {
        System.out.println("Enter Student Name:");
        sname=in.nextLine();

        System.out.println("Enter Student Hoby:");
        hobby=in.nextLine();

        System.out.println("Enter Student Semester:");
        sem=in.nextLine();

        System.out.println("Enter Student Rno:");
        rno=in.nextInt();               
    }

}

class Sports extends Singleinheritance
{
    int sw;

    void extra()
    {

        System.out.println("Enter Student Sport weightage:");
        sw=in.nextInt();
    }
}

class java21
{
    public static void main(String args[])
    {
        Sports s1=new Sports();
        s1.student();   
        s1.extra(); 
    }
}

// Practical 22

class X
{
   public void methodX()
   {
     System.out.println("Class X method");
   }
}
class Y extends X
{
public void methodY()
{
System.out.println("class Y method");
}
}
class java22 extends Y
{
   public void methodZ()
   {
     System.out.println("class Z method");
   }
   public static void main(String args[])
   {
     java22 obj = new java22();
     obj.methodX(); //calling grand parent class method
     obj.methodY(); //calling parent class method
     obj.methodZ(); //calling local method
  }
}

// Practical 23

import java.util.Scanner;
class Grandfather
{
    Scanner in=new Scanner(System.in);
    String gfname;

    void gf()
    {
        System.out.println("Hii! This is grand father class");

        System.out.println("Enter GrandFather's Name:");
        gfname=in.nextLine();               
    }

}

class Father extends Grandfather 
{
    String fname;

    void father()
    {
        System.out.println("Hii! This is father class from Grandfather class");

        System.out.println("Enter Father's Name:");
        fname=in.nextLine();    
    }
}

class Uncle extends Grandfather
{
    String uname;

    void uncle()
    {
        System.out.println("Hii! This is uncle class from Grandfather class");

        System.out.println("Enter Uncle's Name:");
        uname=in.nextLine();    
    }
}

class Son extends Father
{
    String sname;

    void son()
    {
        System.out.println("Hii! This is Son class from Father class");

        System.out.println("Enter Son's Name:");
        sname=in.nextLine();    
    }
}


class Daughter extends Uncle
{
    String dname;

    void daughter()
    {
        System.out.println("Hii! This is Daughter class from Uncle class");

        System.out.println("Enter Daughter's Name:");
        dname=in.nextLine();    
    }
}

class java23
{
    public static void main(String args[])
    {
        Son s1=new Son();
        s1.gf();
        s1.father();
        s1.son();
        Daughter d1=new Daughter(); 
        d1.gf();
        d1.uncle();
        d1.daughter();
    }
}

// Practical 24

class Super
{
    int x;
    Super(int a)
    {
        x=a;
    }

    void display()
    {
        System.out.println("Super x:"+x);               
    }
}

class Sub extends Super
{
    int y;
    Sub(int a,int b)    
    {
        super(a);
        y=b;
    }
    void display()
    {   
        System.out.println("Super x:"+x);
        System.out.println("Sub y:"+y); 
    }
}

class java24
{
    public static void  main(String args[])
    {
        Super s1=new Super(100);
        s1.display(); 
    }
}

// Practical 25

class Super
{
     int x;
    Super(final int a)
    {
        x=a;
    }

 final void display()
    {
        System.out.println("Super x:"+x);               
    }
}

class Sub extends Super
{
    int y;
    Sub(int a,int b)    
    {
        super.x=10;
        y=b;
    }
    void display()//Display method must not override in the subclass of superclass.
    {   
        System.out.println("Super x:"+x);
        System.out.println("Sub y:"+y); 
    }
}

class java25
{
    public static void  main(String args[])
    {
        Sub s1=new Sub(100,300);
        s1.display(); 
    }
}

// Practical 45

import java.applet.*;
import java.awt.*;

public class java45 extends Applet {
    public void paint(Graphics g) {
        g.drawString("Hello Applet", 50, 50);
    }
}

// <applet code="java45.class" width=300 height=300></applet>

45 Output image.png

// Practical 46 java46.java

import java.applet.Applet;
import java.awt.Graphics;

public class java46 extends Applet {
    public void init() {
        System.out.println("1.I am init()");
    }

    public void start() {
        System.out.println("2.I am start()");
    }

    public void paint(Graphics g) {
        System.out.println("3.I am paint()");
    }

    public void stop() {
        System.out.println("4.I am stop()");
    }

    public void destroy() {
        System.out.println("5.I am destroy()");
    }
}

java46.html

<html>
<body>
    <applet code="java46.class" width="300" height="300"></applet>
</body>
</html>

46 Output

image.png

// Practical 47

import java.applet.Applet;
import java.awt.Graphics;

public class java47 extends Applet {
    public void paint(Graphics g) {
        String str = getParameter("msg");
        g.drawString(str, 50, 50);
    }
}

// <applet code="java47.class" width="400" height="400">
// <param name="msg" value="Welcome to applet">
// </applet>

47 Output image.png

// Practical 48

import java.applet.Applet;
import java.awt.*;

//<applet code="java48.class" width="300" height="300">  
//</applet> 
public class java48 extends Applet {

    public void paint(Graphics g) {
        g.setColor(Color.red);
        g.drawString("Welcome", 50, 50);
        g.drawLine(20, 30, 20, 300);
        g.drawRect(70, 100, 30, 30);
        g.fillRect(170, 100, 30, 30);
        g.drawOval(70, 200, 30, 30);

        g.setColor(Color.pink);
        g.fillOval(170, 200, 30, 30);
        g.drawArc(90, 150, 30, 30, 30, 270);
        g.fillArc(270, 150, 30, 30, 0, 180);

    }
}

48 Output image.png