ARAVALI COLLEGE OF ENGINEERING & MANAGEMENT
Jasana Tigoan Road Greater Faridabad Haryana, 121006
JAVA LANGUAGE LAB
COURSE FILE
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(2020-2023)
FACULTY INCHARGE: STUDENT NAME:
Ms. Tanu Aman Daswal
(20BCA05)
`
Sr Programs Date Signature
No.
1. Write a Java program that prompts the user for
an integer and then prints out all prime numbers
up to that integer.
2. Write a Java program to multiply two given
matrices
3. Write a Java program that checks whether a
given string is a palindrome or not.
4. Write a program of find the area of rectangle
using constructor.
5. Write a program of find the area of rectangle and
square using inheritance.
6. Write a Java program for sorting a given list of
names in ascending order
7. Write a Java program of Thread.
8. write a java program of exception handling using
try catch block.
9. Write a java program of user defind package.
10. Write a Java program that displays the number of
characters, lines and words in a text file.
Program 1
1. Write a Java program that prompts the user for an integer and then
prints out all prime numbers up to that integer.
Syntax :-
import [Link]; class
PrimeNumbers
{
Public static void main(String[] args)
{
int a;
int b;
Scanner d=new Scanner([Link]);
[Link]("Enter a number: "); a=[Link]();
for(int i=2; i<a ;i++)
{
b=0;
for(int j=2; j<i; j++)
{
If (i%j==0)
b=1;
}
If (b==0)
[Link](i);
}
}
}
Output :-
Program 2
2. Write a Java program to multiply two given matrices.
Syntax :- import [Link]; public class Main
{
static void printMatrix(int M[][], int rowSize, int colSize)
{
for (int i = 0; i < rowSize; i++)
{
for (int j = 0; j < colSize; j++)
{
[Link](M[i][j] + " ");
}
[Link]();
} }
static void multiplyMatrix(int p,int q, int a[][], int m, int n, int b[][])
{
int i, j, k;
[Link]("First Matrix:"); printMatrix(a,
p, q);
[Link]("Second Matrix:");
printMatrix(b, m, n); if
(m != q)
{
[Link]("Multiplication Not Possible");
return; }
int c[][] = new int[q][n]; for (i = 0;
i < p; i++)
{
for (j = 0; j < n; j++)
{
for (k = 0; k < m; k++) c[i][j]
+= a[i][k] * b[k][j];
}
}
[Link]("\nResultant Matrix:"); printMatrix(c,
p, n);
}
public static void main(String[] args)
{
int p, q, m, n;
Scanner sc = new Scanner([Link]);
[Link]("Enter the number of rows in the first matrix:"); p
= [Link]();
[Link]("Enter the number of columns in the first matrix:"); q
= [Link]();
[Link]("Enter the number of rows in the second matrix:"); m
= [Link]();
[Link]("Enter the number of columns in the second matrix:");
n = [Link](); int a[][] = new int[p][q]; int b[][] = new int[m][n];
//Initialize the first Matrix
[Link]("Enter all the elements of first matrix:"); for
(int i = 0; i < p; i++)
{
for (int j = 0; j < q; j++)
{
a[i][j] = [Link]();
}
}
[Link]("");
[Link]("Enter all the elements of second matrix:"); for
(int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
b[i][j] = [Link]();
}
} multiplyMatrix(p ,q, a, m,
n, b);
}
}
Output :-
Program 3
3. Write a Java program that checks whether a given string is
a palindrome or not. Syntax :- import [Link];
class palindrome
{
public static void main(String[] args) {
Scanner scanner=new Scanner([Link]);
[Link]("Enter a string as an input to check whether it is
palindrome or not");
String input= [Link]();
if(isPalindrome(input))
{
[Link](input+" is a palindrome string");
}
else
{
[Link](input+" is not a palindrome string");
}
} public static boolean isPalindrome(String
str) { int left = 0, right = [Link]() - 1;
while(left < right)
{ if([Link](left) !=
[Link](right))
{ return
false;
}
left++;
right--; }
return true;
}
}
Output :-
Program 4
4. Write a program of find the area of rectangle using constructor.
Syntax :-
import [Link];
public class Area_Perimeter
public static void main(String[] args)
int l, b, area;
Scanner s = new Scanner([Link]); [Link]("Enter
length of rectangle:");
l = [Link]();
[Link]("Enter breadth of rectangle:");
b = [Link]();
area = l * b;
[Link]("Area of rectangle:"+area);
Output :-
Program 5
5. Write a program of find the area of rectangle and square using
inheritance. Syntax :- class FindLargestShape {
public static void main(String arg[]) { Rectangle r =
new Rectangle(10, 4);
Square s = new Square(7);
[Link]("Rectangle Area : " + [Link]());
[Link]("Square Area : " + [Link]()); [Link]();
if (([Link]() ) && ([Link]() > [Link]())) {
[Link]("Rectangle has the largest area.");
}
else if( [Link]() ) {
[Link]("Square has the largest area.");
}
}
}
class Rectangle
{ double length;
double breadth;
Rectangle(double length, double breadth { [Link]
= length;
[Link] = breadth;
} double getArea()
{ return length * breadth;
}
}
class Square { double side;
Square(double side)
{ [Link] = side; }
double getArea()
{ return side * side;
}
}
Output :-
Program 6
6. Write a Java program for sorting a given list of names in ascending
order.
Syntax :-
import [Link].*;
class GFG {
public static void main(String[] args)
{ int n =
4;
String names[ ]
= { "Umesh", "Deepak", "Gourav", "Shubham" }; String
temp;
for (int i = 0; i < n; i++) { for
(int j = i + 1; j < n; j++) {
if (names[i].compareTo(names[j]) > 0) {
temp =
names[i]; names[i] =
names[j]; names[j] =
temp;
}
}
}
[Link](
"The names in Ascending order are: ");
for (int i = 0; i < n; i++) {
[Link](names[i]);
}
}
}
Output :-
Program 7
7. Write a Java program of Thread.
Syntax :- public class Main {
public static void main(String[] args) {
for (int i=1; i<=10; i++){
Calculator calculator=new Calculator(i); Thread
thread=new Thread(calculator);
[Link]();/*from
}
}
}
class Calculator implements Runnable {
private int number;
public Calculator(int number)
{ [Link]=number;
}
@Override public
void run() {
for (int i=2; i<=5; i++){
[Link]("%s: %d * %d =
%d\n",[Link]().getName(),number,i,i*number);
}
}
}
Output- :
Program 8
8. write a java program of exception hanling using try catch block.
Syntax :-
class Main {
public static void main (String args[])
try
[Link] ("::Try Block::");
int data = 125 / 5;
[Link] ("Result:" + data);
}
catch (NullPointerException e) {
[Link] ("::Catch Block::");
[Link] (e);
}
finally {
[Link] (":: Finally Block::");
[Link] ("No Exception::finally block executed");
[Link] ("rest of the code...");
Output :-
Program 9
9. Write a java program of user defind package
Syntax :- package vehicles; interface Vehicle
{ public void run();
public void speed(); package
vehicles; public class Car
implements Vehicle
{ public void
run()
[Link]("Car is running.");
} public void
speed()
[Link]("Speed of Car: 50 Km/h");
public static void main(String args[])
Car Car = new Car();
[Link]();
[Link]();
[Link]("Hello World!");
Output :-
Program 10
10. Write a Java program that displays the number of characters, lines
and words in a text file. Syntax :- import [Link].*; class New {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
[Link]("Enter the name of the file: ");
String file_name = [Link]();
File f = new File(file_name) int
nchars=0,nlines=0,nwords=0; try {
FileInputStream fs = new FileInputStream(f);
InputStreamReader isr = new InputStreamReader(fs);
BufferedReader r = new BufferedReader(isr);
StreamTokenizer st = new StreamTokenizer(r);
[Link](true);
while([Link]()!=StreamTokenizer.TT_EOF)
{ switch([Link]) { case StreamTokenizer.TT_EOL:
nlines++ ; nchars++
; break ; case
StreamTokenizer.T
T_WORD:
nwords++; default:
if([Link]==StreamTokenizer.TT_WORD)
nchars+=[Link](); break; }
} }
catch(FileNotFoundException e) catch(IOException e){
[Link]("Error occured reading file:");
}
[Link]("The no of charecters are: " +nchars);
[Link]("The no of words are: " +nwords);
[Link]("The no of lines are: " +nlines);
}}
Output :-