Java File
Java File
Java File
Objective:
a) Write a JAVA program to print “Hello World.”
b) WAP in java to print the values of various primitive data types.
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)
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)
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
{ 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
n=in.nextInt();
sum+=ar[i];
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
for(int i=1;i<=n;i++)
{ fact*=i;
}
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
{ int n1=0,n2=1,n3,count=10;
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();
}
}
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
int r=0;
int c=0;
r=in.nextInt();
c=in.nextInt();
ar[i][j]=in.nextInt();
{
ar1[i][j]=in.nextInt();
for(int i=0;i<r;i++)
result[i][j]=ar[i][j]+ar1[i][j];
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
String s=in.nextLine();
s.trim();
int a=0;
for(int i=0;i<s.length();i++)
if(s.charAt(i)!=' ')
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
return 0;
return "a";
{ 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
return area;
return area;
return area;
}
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{
//child class
}
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
int a=0;
try
int r = 2/a;
catch(Exception e) { System.out.println(e);
System.out.println("Nested Try-Catch");
Try
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);
arr[5]=4;
catch(ArrayIndexOutOfBoundsException e)
{ System.out.println(e);
catch(Exception e)
}
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
Try
if(f.exists())
while(in.hasNextLine())
if(data1[i] == data1[j])
}
}
System.out.println("Characters frequencies");
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.
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.
for(int i=1;i<=10;i++)
System.out.println(i);
t1.start();
System.out.println(i);
}
t1.start();