Pages

Friday, 1 July 2022

JAVA LAB (2022) PROGRAMS

JAVA RECORD (2022) PROGRAMS  :
1.Write a program  to read Student Name, Register No, Marks[5], and calculate Total, Percentage, Result. Display all the details of students.  
import java.util.Scanner;
public class Studentdetails
{
      public static void main(String args[])
      {
          String name;
          int roll, math, stat, comp,seclang,eng;
          Scanner SC=new Scanner(System.in);
          System.out.print("Enter Name: ");
          name=SC.nextLine();
          System.out.print("Enter Roll Number: ");
          roll=SC.nextInt();
          System.out.print("Enter marks in English,Seclang, Maths, Stat and computers: ");
          eng=SC.nextInt();
          seclang=SC.nextInt();  
          math=SC.nextInt();
          stat=SC.nextInt();
          comp=SC.nextInt();
          int total = eng+seclang+math+stat+comp;
          float perc=(float)total/500*100;
          System.out.println("Roll Number:" + roll);
          System.out.println("Student  name :" +name);  
          System.out.println("Marks (Eng, Seclang,Maths, stat, comp):   " +eng+" "+seclang+"   "+math+","+stat+","+comp);
          System.out.println("Total: "+total +"\t\tPercentage: "+perc);
      } 
  }   

Output:
C:\Users\Student\Desktop\java prg>javac Studentdetails.java
C:\Users\Student\Desktop\java prg>java Studentdetails
Enter Name: raj kumar
Enter Roll Number: 323
Enter marks in English,Seclang, Maths, Stat and computers: 
22
33
44
121
33
Roll Number:323
Student  name :raj kumar
Marks (Eng, Seclang,Maths, stat, comp):   22 33   44,121,33
Total: 253              Percentage: 50.6

2.Write a Java Program Addition of Two Matrices:
import java.util.Scanner;
public class MatrixAdditionExample
{  
  public static void main(String args[])
  {  
    int row1, col1, row2, col2;
    Scanner s = new Scanner(System.in);
    System.out.print("Enter number of rows in first matrix:");
    row1 = s.nextInt();
    System.out.print("Enter number of columns in first matrix:");
    col1 = s.nextInt();
    System.out.print("Enter number of rows in second matrix:");
    row2 = s.nextInt();
    System.out.print("Enter number of columns in second matrix:");
    col2 = s.nextInt();
    if (col1 != row2)
     {
     System.out.println("Matrix Addition is not possible");
     }
    else
       {
        int a[][] = new int[row1][col1];
        int b[][] = new int[row2][col2];
        int c[][] = new int[row1][col2];
        System.out.println("Enter values for matrix A : \n");
        for (int i = 0; i < row1; i++)
          {
           for (int j = 0; j < col1; j++)
                a[i][j] = s.nextInt();
          }
    System.out.println("Entered values in matrix A : \n");
    for (int i = 0; i < row1; i++)
      {
        for (int j = 0; j < col1; j++)
        {
        System.out.print(a[i][j]+ "  ");
        }
       System.out.println( );
      }
    System.out.println("Enter values for matrix B : \n");
    for (int i = 0; i < row2; i++)
      {
       for (int j = 0; j < col2; j++)
                b[i][j] = s.nextInt();
       }
    System.out.println("Entered values in matrix B : \n");
    for (int i = 0; i < row2; i++)
    {
      for (int j = 0; j < col2; j++)
        {
        System.out.print(b[i][j]+ "  ");
        }
      System.out.println( );
    }
    for(int i = 0; i < row1; i++)
      {    
           for(int j = 0; j < col2; j++)
         {    
              c[i][j] = a[i][j] + b[i][j];      
             }
          }
    System.out.println("Matrix addition is : \n");
    for(int i = 0; i < row1; i++)
     {    
      for(int j = 0; j < col2; j++)
        {
        System.out.print(c[i][j]+ " ");      
         }
      System.out.println();          
     }
    }
  }
}
Output:
C:\Users\Student\Desktop\java prg>javac MatrixAdditionExample.java
C:\Users\Student\Desktop\java prg>java MatrixAdditionExample
Enter number of rows in first matrix:2
Enter number of columns in first matrix:2
Enter number of rows in second matrix:2
Enter number of columns in second matrix:2
Enter values for matrix A :

11
22
33
44
Entered values in matrix A :

11  22
33  44
Enter values for matrix B :

1
2
3
4
Entered values in matrix B :

1  2
3  4
Matrix addition is :

12 24
36 48

3.Write a Java Program Multiplication of Two Matrices:
import java.util.Scanner;
public class MatrixMultiplicationExample
{  
  public static void main(String args[])
  {  
    int row1, col1, row2, col2;
    Scanner s = new Scanner(System.in);
    System.out.print("Enter number of rows in first matrix:");
    row1 = s.nextInt();
    System.out.print("Enter number of columns in first matrix:");
    col1 = s.nextInt();
    System.out.print("Enter number of rows in second matrix:");
    row2 = s.nextInt();
    System.out.print("Enter number of columns in second matrix:");
    col2 = s.nextInt();
    if (col1 != row2)
     {
     System.out.println("Matrix multiplication is not possible");
     }
    else
    {
        int a[][] = new int[row1][col1];
        int b[][] = new int[row2][col2];
        int c[][] = new int[row1][col2];
        System.out.println("Enter values for matrix A : \n");
        for (int i = 0; i < row1; i++)
          {
          for (int j = 0; j < col1; j++)
               a[i][j] = s.nextInt();
          }
    System.out.println("Entered values in matrix A : \n");
    for (int i = 0; i < row1; i++)
      {
       for (int j = 0; j < col1; j++)
          {
          System.out.print(a[i][j]+ "  ");
          }
      System.out.println( );
      }
    System.out.println("Enter values for matrix B : \n");
    for (int i = 0; i < row2; i++)
        {
        for (int j = 0; j < col2; j++)
                b[i][j] = s.nextInt();
        }
    System.out.println("Entered values in matrix B : \n");
     for (int i = 0; i < row2; i++)
       {
        for (int j = 0; j < col2; j++)
           {
           System.out.print(b[i][j]+ "  ");
          }
       System.out.println( );
       }
   System.out.println("Matrix multiplication is : \n");
   for(int i = 0; i < row1; i++)
     {    
    for(int j = 0; j < col2; j++)
       {    
        c[i][j]=0;      
        for(int k = 0; k < col1; k++)
          {      
          c[i][j] += a[i][k] * b[k][j];      
          }
         System.out.print(c[i][j] + " ");  
       }
        System.out.println();
     }    
    }
  }
}

Output:
C:\Users\Student\Desktop\java prg>javac MatrixMultiplicationExample.java
C:\Users\Student\Desktop\java prg>java MatrixMultiplicationExample
Enter number of rows in first matrix:2
Enter number of columns in first matrix:2
Enter number of rows in second matrix:2
Enter number of columns in second matrix:2
Enter values for matrix A :

11
22
33
44
Entered values in matrix A :

11  22
33  44
Enter values for matrix B :

1
2
3
4
Entered values in matrix B :

1  2
3  4
Matrix multiplication is :

77 110
165 242


4. Write a java program  to implement constructors 
class Constructor
{
    double width;
    double height;
    double depth;
    Constructor() 
    {
System.out.println("Constructor without parameter");
width = 10;
height = 10;
depth = 10;
    }
    Constructor(int a, int b, int c) 
    {
System.out.println("Constructor with parameter");
width = a;
height = b;
depth = c;
    }
    double volume() 
    {
return width * height * depth;
    }
}
class ConstructorDemo
{
    public static void main(String args[]) 
    {
Constructor cuboid1 = new Constructor();
        double vol;
vol = cuboid1.volume();
System.out.println("Volume is " + vol);
Constructor cuboid2 = new Constructor(8,11,9);
vol = cuboid2.volume();
System.out.println("Volume is " + vol);
    }
}
Output:
C:\Users\Student\Desktop\java prg>javac ConstructorDemo.java
C:\Users\Student\Desktop\java prg>java ConstructorDemo
Constructor without parameter
Volume is 1000.0
Constructor with parameter
Volume is 792.0

5.Write a java program to calculate area of the following shapes using method overloading - Square, Rectangle,Circle,Triangle.
class MethodOverload 
{
    public static void main(String[] args)
    {
       CalculateArea ob = new CalculateArea();
   ob.area(4);
   ob.area(10f,12f);
   ob.area(5.5);
           ob.area(18.0,25.4);
    }
}
class CalculateArea
{
    void area(int x)
    {  
        int area=x*x;
        System.out.println("The area of the Square is "+area+" sq units");
    }
    void area(float x, float y)
    { 
        float area=x*y;
        System.out.println("The area of the Rectangle is "+area+" sq units");
    }
    void area(double x)
    {
        double z = 3.14 * x * x;
        System.out.println("The area of the Circle is "+z+" sq units");
    }
    void area(double b,double  h)
     {  
       double  t=(b*h)/2;
       System.out.println("The area of the Triangle "+t);
    }
}

Output:
C:\Users\Student\Desktop\java prg>javac MethodOverload.java
C:\Users\Student\Desktop\java prg>java MethodOverload
The area of the Square is 16 sq units
The area of the Rectangle is 120.0 sq units
The area of the Circle is 94.985 sq units
The area of the Triangle 228.6

6. Implement inheritance between person(Aadhar, Surname, Name, DOB, and Age) and Student (Admission Number, college, course, Year) classes where ReadData() and Display Data()are overriding methods.
import java.util.Scanner;
import java.util.Date;
class Person
{
   int Age;  
   long Aadhar;
   String DOB,sname,name;
    void Readdata()
   {
      Scanner sc=new Scanner(System.in);
      System.out.println("enter Aadhar no:");
      Aadhar = sc.nextLong();
      System.out.println("enter NAME "); 
       name = sc.next();
      System.out.println("enter Surname ");
       sname = sc.next();
      System.out.println("Enter your date of birth");
      DOB = sc.next();
      System.out.println("enter AGE:");
      Age=sc.nextInt();
     }
  void display()
  {
    System.out.println("person  details are:");
    System.out.println("---------------------:");
    System.out.println("ur Aadhar no is :"+Aadhar);
    System.out.println("ur Name and Surname is:"+name+" "+sname);
    System.out.println("ur Date Of birth is:"+DOB);
    System.out.println("ur Age is:"+Age);
  
    }
}
 class Student extends Person
 {
   int Adno,year;
   String clgname,course;
   void Readdata()
   {
    Scanner sc=new Scanner(System.in);
    System.out.println("-------------------:");
    System.out.println("enter Admission no:");
     Adno=sc.nextInt();
    System.out.println("enter clg name:");
     clgname=sc.next();
    System.out.println("enter course name :");
     course=sc.next();
    System.out.println("enter year of join:");
     year=sc.nextInt();
    }
  void display()
  {
    System.out.println("Student details are:");
    System.out.println("---------------------:");
    System.out.println("Admission no is:"+Adno );
    System.out.println("clgname is:"+clgname);   
    System.out.println("course is:"+course);
    System.out.println("year is "+year);
    }
}
class inhoverride
{
  public static void main(String args[]) 
  {
    Person p=new Person();
    p.Readdata();
    p.display();
   Student s=new Student();      
   s.Readdata();
    s.display();
   }
 }
Output:
C:\Users\Student\Desktop\java prg>javac inhoverride.java
C:\Users\Student\Desktop\java prg>java inhoverride
enter Aadhar no:
2356743456
enter NAME
raj kumar
enter Surname
Enter your date of birth
22-04-2001
enter AGE:
33
person  details are:
---------------------:
ur Aadhar no is :2356743456
ur Name and Surname is:raj kumar
ur Date Of birth is:22-04-2001
ur Age is:33
-------------------:
enter Admission no:
1121
enter clg name:
gvp
enter course name :
msc
enter year of join:
12-6-2001
Exception in thread "main" java.util.InputMismatchException
        at java.util.Scanner.throwFor(Scanner.java:819)
        at java.util.Scanner.next(Scanner.java:1431)
        at java.util.Scanner.nextInt(Scanner.java:2040)
        at java.util.Scanner.nextInt(Scanner.java:2000)
        at Student.Readdata(inhoverride.java:48)
        at inhoverride.main(inhoverride.java:68)

7. Java Program to Implementing Interface.
import java.util.Scanner;
interface area
{
    public void dimensions();
    public void area();
}
public class Interface_Implementation implements area
{
    int length,breadth,area;
    public void dimensions() 
    {
        Scanner s=new Scanner(System.in);
        System.out.print("Enter length:");
        length=s.nextInt();
        System.out.print("Enter breadth:");
        breadth=s.nextInt();
    }
    public void area() 
    {
        area=length*breadth;
        System.out.print("Area:"+area);
    }
    public static void main(String[] args) 
    {
        Interface_Implementation obj=new Interface_Implementation();
        obj.dimensions();
        obj.area();
    }
}
Output:
C:\Users\Student\Desktop\java prg>javac Interface_Implementation.java
C:\Users\Student\Desktop\java prg>java Interface_Implementation
Enter length:22
Enter breadth:44
Area:968

8.Java Program on Multiple Inheritance with interface
import java.lang.*;
interface Shape
 {
double area();
double perimeter();
}
class Rectangle implements Shape 
{
private double length;
private double breadth;
public Rectangle(double length, double breadth)
                {
this.length = length;
this.breadth = breadth;
        }
public double area()
                {
return length * breadth;
        }
public double perimeter() 
{
return 2 * (length + breadth);
}
}
class Circle implements Shape 
{
private double radius;
public Circle(double radius) 
{
this.radius = radius;
}
public double area() 
{
return Math.PI * radius * radius;
}
public double perimeter() 
{
return 2 * Math.PI * radius;
}
}
public class MInterface 
{
public static void main(String[] args)
{
// Rectangle area and parameter
double length = 2.0;
double breadth = 3.0;
Rectangle r = new Rectangle(length, breadth);
System.out.println("Rectangle - Area: " + r.area());
System.out.println("Rectangle - perimeter: " + r.perimeter());
// Circle area and parameter
double radius = 2.0;
Circle c = new Circle(radius);
System.out.println("Circle - Area: " + c.area());
System.out.println("Circle - perimeter: " + c.perimeter());
}
}
Output:
C:\Users\Student\Desktop\java prg>javac MInterface.java
C:\Users\Student\Desktop\java prg>java MInterface
Rectangle - Area: 6.0
Rectangle - perimeter: 10.0
Circle - Area: 12.566370614359172
Circle - perimeter: 12.566370614359172

9. Write a Java Program to perform the following String Operations.
Read a String
Find out whether there is a given substring or not
Compare existing string by another string and display status
Replace existing string character with another character
Count number of works in a string

import java.util.Scanner;
class read
{
void input()
{
System.out.println("READING A STRING FROM CONSOLE\n");
System.out.println("Enter a string: ");
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
System.out.println("String read from console is:\n "+s);
System.out.println("==================================\n\n");
}
}
class sub 
{
Scanner s=new Scanner(System.in);
void check()
{
System.out.println("CHECKING SUBSTRING:\n");
System.out.println("Enter the string:");
String txt=s.nextLine();
System.out.println("Enter the sub string:");
String st=s.nextLine();
boolean result = txt.contains(st);
if(result)
{
System.out.println(st + " is present in the string.");
}
else 
{
System.out.println(st + " is not present in the string.");
}
System.out.println("======================================\n\n");
}

}
class equal
{
Scanner s=new Scanner(System.in);
void compare()
{
System.out.println("COMPARING SUBSTRINGS:\n");
System.out.println("Enter a String:");
String txt=s.nextLine();
System.out.println("Enter 2nd String:");
String txt2=s.nextLine();
System.out.println("Enter 3rd String:");
String txt3=s.nextLine();
boolean result;
if(result=txt.equals(txt2))
{
                   System.out.println("\nString first and second are equal:"+result);
}
else if(result=txt.equals(txt3))
{
System.out.println("\nString first and third are equal:"+result);
}
else
{
System.out.println("Hence given Strings are not equal:");
}
System.out.println("=========================================\n\n");
}
}
class replace
{
String str="The quick brown fox jumps over the lazy dox";
String newstr;
void another()
{
System.out.println("REPLACING CHARACTERS:\n");
newstr=str.replace('d','f');
System.out.println("charcter Replacement :");
System.out.println("Original String :"+str);
System.out.println("New String is :"+newstr);
System.out.println("==================================\n\n");
}
}
class count
{
Scanner s=new Scanner(System.in);
void number()
{
System.out.println("COUNTING LENGTH OF A STRING:\n");
System.out.println("Enter the String to count the no. of words:");
String str=s.nextLine();
System.out.println("\nThe size of the String is:"+str.length());
System.out.println("==================================\n\n");
}
}
class strfun
{
public static void main(String args[])
{
read r=new read();
r.input();
sub s=new sub();
s.check();
equal e=new equal();
e.compare();
replace re=new replace();
re.another();
count c=new count();
c.number();
}
}
Output:
C:\Users\Student\Desktop\java prg>javac Strfun.java

C:\Users\Student\Desktop\java prg>java Strfun
READING A STRING FROM CONSOLE

Enter a string:
raj kumar
String read from console is:
 raj kumar
==================================


CHECKING SUBSTRING:

Enter the string:
welcome to java
Enter the sub string:
to
to is present in the string.
======================================


COMPARING SUBSTRINGS:

Enter a String:
ram
Enter 2nd String:
sita
Enter 3rd String:
ram

String first and third are equal:true
=========================================


REPLACING CHARACTERS:

charcter Replacement :
Original String :The quick brown fox jumps over the lazy dox
New String is :The quick brown fox jumps over the lazy fox
==================================


COUNTING LENGTH OF A STRING:

Enter the String to count the no. of words:
ram is a boy

The size of the String is:12
==================================

10.Write a java program to implement arithmetic package and perform addition,subtraction, multiplication and division operations with command line agruments

//Program :file name: Rk.java
package pk;
public class Rk
{
    public int add(int a,int b)
    {
        return a+b;
    }
    public int sub(int a,int b)
    {
        return a-b;
    }
    public int mul(int a,int b)
    {
        return a*b;
    }
    public int div(int a,int b)
    {
        return a/b;
    }
}

Step 2: create a file with Kk.java
import Pk.Rk;  // import <Package name>.<Class name>
class Kk
{
    public static void main(String []args)
    {
        Rk c=new Rk();
        int a=Integer.parseInt(args[0]);
        int b=Integer.parseInt(args[1]);
        int z=c.mul(a,b);
        int v=c.sub(a,b);
        System.out.println("Addition is:"+c.add(a,b));
        System.out.println("Division is:"+c.div(a,b));
        System.out.println("Multiplication is:"+z);
        System.out.println("Subtraction is:"+v);    
    }
}

Step 3: use the following command to run the package program 
ubuntu@ubuntu:~/Desktop$ mkdir pk
ubuntu@ubuntu:~/Desktop$ cd pk
ubuntu@ubuntu:~/Desktop/pk$ gedit Rk.java
ubuntu@ubuntu:~/Desktop/pk$ javac -d . Rk.java
ubuntu@ubuntu:~/Desktop/pk$ ls
pk  Rk.class  Rk.java
ubuntu@ubuntu:~/Desktop/pk$ cd ..
ubuntu@ubuntu:~/Desktop$ gedit Kk.java
ubuntu@ubuntu:~/Desktop$ javac -d . Kk.java
ubuntu@ubuntu:~/Desktop$ ls K*
Kk.class  Kk.java
ubuntu@ubuntu:~/Desktop$ java Kk 4 2
Addition is:6
Division is:2
Multiplication is:8
Subtraction is:2

11.Java program demonstrate the following exception handlings:
a.Dividesbyzero
b.ArrayIndexOutOfBound
c.Filenotfound
d.ArithmeticException
e.Userdefined Exception

import java.util.Scanner;
import java.io.*;
class dbz
{
void divide()
{
System.out.println("DIVIDED BY ZERO\n");
Scanner sc=new Scanner(System.in);
System.out.println("Enter P value: ");
int p=sc.nextInt();
System.out.println("enter Q value: ");
int q=sc.nextInt();
try
{
int result=p/q;
System.out.println("Result:"+p+"/"+q+"="+result);
}
catch(ArithmeticException e)
{
System.out.println("Divided by Zero");
}
System.out.println("====================");
}
}
class aiob
{
void array()
{
System.out.println("CHECKING ARRAY INDEX \n");
Scanner sc=new Scanner(System.in);
int a[]={1,2,3,4,5};
try
{
System.out.println("Array fixed size is 1 to 4");
System.out.println("Enter Array size: ");
int i=sc.nextInt();
if(a[i]<=5)
{
System.out.println(" Position of the given number is: "+a[i]);
}
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array Index out of Bound");
}
System.out.println("====================");
}
}
class fnf
{
void file()
{
System.out.println("FILE CHECKING\n");
try
{
FileInputStream fin=new                 FileInputStream("Desktop/stock.txt");
System.out.println("File Found");
}
catch(FileNotFoundException e)
{
System.out.println("File not Found");
}
System.out.println("=======================");
}
}
class arex
{
void arth()
{
System.out.println("ARITHMETIC EXCEPTION\n");
Scanner sc=new Scanner(System.in);
System.out.println("Enter A value: ");
int a=sc.nextInt();
System.out.println("enter B value: ");
int b=sc.nextInt();
try
{
int result=a/b;
System.out.println("Result:"+a+"/"+b+"="+result);
}
catch(ArithmeticException e)
{
System.out.println("Arithmetic Exception");
}
System.out.println("====================");
}
}


class AgeException extends Exception
 {
 public AgeException(String str)
 {
  System.out.println(str);
 }
}

 class udex 
{
void user()
{
System.out.println("USER DEFINED EXCEPTION\n");
Scanner sc=new Scanner(System.in);
try
{
System.out.println("Enter Age of Candidate: ");
int age=sc.nextInt();
                 
                        if(age < 18) 
                  throw new AgeException("Invalid age");
                  else
                   System.out.println("Valid age");
                }
           catch (AgeException a)
         {
             System.out.println(a);
         }
}
}
class main
{
public static void main(String args[])
{
dbz d=new dbz();
d.divide();
aiob a=new aiob();
a.array();
fnf f=new fnf();
f.file();
arex ae=new arex();
ae.arth();
udex u=new udex();
u.user();
}
}

Output:
C:\Users\Student\Desktop\java prg>
C:\Users\Student\Desktop\java prg>javac MainException.java

C:\Users\Student\Desktop\java prg>java MainException
DIVIDED BY ZERO

Enter P value:
11
enter Q value:
0
Divided by Zero
====================
CHECKING ARRAY INDEX

Array fixed size is 1 to 4
Enter Array size:
6
Array Index out of Bound
====================
FILE CHECKING

File not Found
=======================
ARITHMETIC EXCEPTION

Enter A value:
12
enter B value:
2
Result:12/2=6
====================
USER DEFINED EXCEPTION

Enter Age of Candidate:
10
Invalid age
AgeException

No comments:

Post a Comment

Servlet - JSP Programs

JSP(Java Server Pages) Programs : JavaServer Pages (JSP) is a Java standard technology that enables you to write dynamic data-driven pages f...