Java File

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

EXPERIMENT -1

Objective:
a) Write a JAVA program to print “Hello World.”
b) WAP in java to print the values of various primitive data types.

Author: Aditya Narendra


Date: 08-11-22
Purpose: The purpose of this program is to get accustomed with data types and basic
printing in Java.
class Ist{ publicstatic void main(String []args)
{ System.out.println("Hello World"+"\n");
int a=23; double d=23.45;
float f=23/45; char c='a';
String s="Aditya Narendra";
boolean b=true;
System.out.println("Integer -->"+a);

System.out.println("Double -->"+d);

System.out.println("Float -->"+f);

System.out.println("Character -->"+c);

System.out.println("String -->"+s);

System.out.println("Boolean -->"+b);

}
EXPERIMENT-2
Objective:
WAP in java to demonstrate operator precedence.
Author: Aditya Narendra
Date: 08-11-22
Purpose: The purpose of this program is to show the priority order in which
the compiler processes the operators in Java.
class IInd { public static void main(String []args)

{ int a = 10, b = 2, c = 3, result;

result = a-c*b;

//The operator precedence of * multiplication operator is higher than that of - subtraction operator.
System.out.println("Result = "+result);

}
EXPERIMENT-3
Objective:
WAP in java to create a class Addition and a method add() to add two
numbers.
Author: Aditya Narendra
Date: 08-11-22
Purpose: The purpose of this program is to get accustomed to the use of
methods/functions in Java.
class addition { public static void main(String []args)

{ System.out.println("Addition Result = "+add(3,4));

public static int add(int x, int y) { int result=0;

result = x+y;

return result;

}
EXPERIMENT-4
Objective:

Write a program that uses length property for displaying any number of command line arguments.
Author: Aditya Narendra

Date: 08-11-22

Purpose: The purpose of this program is to show the use of command line arguments in Java and use
length property of arrays.

class IVth

{ public static void main(String []args)

{ for(int i=0;i <args.length;i++)

{ System.out.println(args[i]);

}
EXPERIMENT-5
Objective:
WAP in java to find the average of N numbers
Author: Aditya Narendra
Date: 08-11-22
Purpose: Here we find the average of N numbers using arrays in Java.
import java.util.*;

class Vth

{ public static void main(String []args)

{ Scanner in =new Scanner(System.in);

int n=0; int sum=0;

System.out.print("Enter the size of array :");

n=in.nextInt();

int [] ar=new int [n];

System.out.println("Enter the elements of array :");

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

sum+=ar[i];

double avg = sum/n;

System.out.println("Average = "+avg);

}
EXPERIMENT-6

Objective:
WAP in java to find out factorial of a number
Author : Aditya Narendra
Date : 08-11-22
Purpose : Here we find the factorial of a number in Java using a loop.
import java.util.*;

class VIth

{ public static void main(String []args)

{ Scanner in = new Scanner(System.in);

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

int n=in.nextInt(); int fact=1;

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

{ fact*=i;

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

}
EXPERIMENT-7
Objective:
WAP in java to find out the Fibonacci series
Author: Aditya Narendra
Date: 08-11-22
Purpose: Here we print the Fibonacci series upto 10 terms using loop in Java.
class VIIth

{ public static void main(String []args)

{ int n1=0,n2=1,n3,count=10;

System.out.println("Fibonacci series upto "+count+" terms :");

System.out.print(n1+" "+n2); for(int i=2;i<count;++i)

n3=n1+n2;

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

n1=n2;

n2=n3;

}
EXPERIMENT-8
Objective: WAP in java to sort elements in 1D array in ascending order.
Author: Aditya Narendra
Date: 08-11-22
Purpose: In this program we input numbers in 1D arrays and sort the elements
in ascending order using bubble sort.
import java.util.*;
class VIIIth
{ public static void main(String []args)
{ Scanner in = new Scanner(System.in);
System.out.print("Enter array size :");
int n=in.nextInt();

int []ar=new int[n];


System.out.println("Enter array elements");
for(int i=0;iar[j+1])
{ int temp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=temp;
}
}
}
System.out.println("Sorted array : ");
for(int i=0;i <n;i++)
{
System.out.print(ar[i]+"\t");
}

}
}
EXPERIMENT-9
Objective: WAP in java to perform matrix addition using two 2D arrays.
Author: Aditya Narendra
Date: 08-11-22
Purpose: In this program we input numbers in two 2D arrays and calculate
their sum according to mathematical rules.
import java.util.*;

class IXth

{ public static void main(String []args)

{ Scanner in=new Scanner(System.in);

int r=0;

int c=0;

System.out.println("Enter the matrix dimensions: ");

r=in.nextInt();

c=in.nextInt();

int ar[][]=new int[r][c];

int ar1[][]=new int[r][c];

int result[][]=new int[r][c];

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

for(int i=0;i <r;i++)

for(int j=0;j <c;j++)

ar[i][j]=in.nextInt();

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

for(int i=0;i <r;i++)

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

{
ar1[i][j]=in.nextInt();

for(int i=0;i<r;i++)

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

result[i][j]=ar[i][j]+ar1[i][j];

System.out.println("Result matrix : ");

for(int i=0;i <r;i++)

for(int j=0;j<c;j++)

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

System.out.println();

}
EXPERIMENT-10
Objective: WAP in java to find out the number of characters in a string.
Author: Aditya Narendra
Date : 08-11-22
Purpose : This program is written to calculate the number of character in a
string inputted by the user.
import java.util.*;

class Xth

{ public static void main(String []args)

{ Scanner in = new Scanner(System.in);

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

String s=in.nextLine();

System.out.println("Number of characters in String, including spaces, are: "+s.length());

s.trim();

int a=0;

for(int i=0;i<s.length();i++)

if(s.charAt(i)!=' ')

a++;

System.out.println("Number of characters in String, excluding spaces, are: "+a);

}
EXPERIMENT-11
Objective: WAP in java that implements method overloading.
Author: Aditya Narendra
Date: 08-11-22
Purpose: This program is written to depict the concept of method overloading
in Java,i.e, functions having same names but different parameters.
class XIth

{ public static int xyz(String a,String b)

{ System.out.println("This isn't a integer related function "+(a+b));

return 0;

public static String xyz(int a,int b)

{ System.out.println("This is an integer related function "+(a+b));

return "a";

public static void main(String []args)

{ xyz("2","3");

xyz(2,3);

}
EXPERIMENT-12
Objective: Create a class Shape and override area() method to calculate area of
rectangle, square and circle
Author: Aditya Narendra
Date: 08-11-22
Purpose: This program is written to depict the concept of method overloading
in Java,i.e, functions having same names but different parameters.

import java.util.*;

class Shape

public static double area(double l,double b)

double area = l*b;

return area;

public static double area(double s)

double area = s*s;

return area;

public static float area(float r,float pi)

float area = pi*r*r;

return area;

public static void main(String []args)

System.out.println("Area of rectangle = "+area(2.0,3.0));


System.out.println("Area of square = "+area(2.0));

System.out.println("Area of circle = "+area(2,22/7));

}
EXPERIMENT-13
Objective: WAP that illustrates method overriding
Author: Aditya Narendra
Date: 08-11-22
Purpose: This program is written to depict the concept of method overriding in
Java,i.e., when there are functions with the same name in both child and
parent class, so the latter one gets suppressed.
//parent class

class Vehicle{

void run(){System.out.println("Vehicle is running");

//child class

class Bike extends Vehicle{

void run(){System.out.println("Bike is running safely");

public static void main(String args[]){

Bike obj = new Bike(); obj.run();

}
EXPERIMENT-14
Objective: a) WAP in java demonstrating arithmetic exception and ArrayOutOf
Bounds Exception using try catch block b) WAP in java to demonstrate the use
of nested try block and nested catch block
Author: Aditya Narendra
Date: 08-11-22
Purpose: This program uses Try-Catch blocks to handle exceptions which keep
the program running even when an error occurs.
class XIVth

{ public static void main(String []args)

int a=0;

try

int r = 2/a;

catch(Exception e) { System.out.println(e);

System.out.println("Nested Try-Catch");

Try

int []ar=new int[3];

System.out.println(ar[5]);

catch(Exception e)

System.out.println(e);

try{

try{
int b =39/0;

catch(ArithmeticException e)

System.out.println(e);

try{ int arr[]=new int[5];

arr[5]=4;

catch(ArrayIndexOutOfBoundsException e)

{ System.out.println(e);

catch(Exception e)

System.out.println("handled the exception outer catch");

}
EXPERIMENT-15
Objective: Write a program to count the number of times a character appears
in a File.
Author: Aditya Narendra
Date: 08-11-22
Purpose: This program accesses a file using java.io.File and counts the
frequency of every character that occurs in the file .
import java.util.*;

import java.io.File;

class XVIth

public static void main(String []args)

Try

File f = new File("D://aham.txt");

Scanner in = new Scanner(f);

if(f.exists())

while(in.hasNextLine())

String data = in.nextLine();

int[] freq = new int[data.length()];

char data1[] = data.toCharArray();

for(int i = 0; I <data.length(); i++)

if(data1[i] == data1[j])

freq[i]++; data1[j] = '0';

}
}

System.out.println("Frequencies of the characters in the File are: ");

System.out.println("Characters frequencies");

for(int i = 0; i < freq.length; i++)

if(data1[i] != ' ' && data1[i] != '0')

System.out.println(data1[i] + " " + freq[i]);

catch(Exception e)

e.printStackTrace();

}
EXPERIMENT-16
Objective: WAP in java in two different ways that creates a thread by

(i) extending thread class (ii) implementing runnable interface which displays 1st 10 natural
numbers.

Author: Aditya Narendra

Date: 08-11-22

Purpose: The purpose of this program is to show the usage of threads in Java using two different
methods,i.e., using Thread class by extending it & implementing runnable interface, both of these
have their advantages and disadvantages.

The only difference being that we can’t extend any other class while using Thread extension and
we can do this while using runnable interface.

class XVIIth extends Thread

public void run()

for(int i=1;i<=10;i++)

System.out.println(i);

public static void main(String args[])

XVIIth t1=new XVIIth();

t1.start();

class XVIIth1 implements Runnable

public void run() { for(int i=1;i<=10;i++)

System.out.println(i);
}

public static void main(String args[]){

XVIIth1 m1=new XVIIth1();

Thread t1 =new Thread(m1);

t1.start();

You might also like