Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Aman JAva

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 24

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 java.util.Scanner; class


PrimeNumbers
{
Public static void main(String[] args)
{
int a;

int b;

Scanner d=new Scanner(System.in);

System.out.println("Enter a number: "); a=d.nextInt();

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)
System.out.println(i);
}
}
}

Output :-
Program 2
2. Write a Java program to multiply two given matrices.
Syntax :- import java.util.Scanner; 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++)
{
System.out.print(M[i][j] + " ");
}
System.out.println();
} }
static void multiplyMatrix(int p,int q, int a[][], int m, int n, int b[][])
{
int i, j, k;
System.out.println("First Matrix:"); printMatrix(a,
p, q);
System.out.println("Second Matrix:");
printMatrix(b, m, n); if
(m != q)
{
System.out.println("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];
}
}
System.out.println("\nResultant Matrix:"); printMatrix(c,
p, n);
}
public static void main(String[] args)
{
int p, q, m, n;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows in the first matrix:"); p
= sc.nextInt();

System.out.print("Enter the number of columns in the first matrix:"); q


= sc.nextInt();
System.out.print("Enter the number of rows in the second matrix:"); m
= sc.nextInt();
System.out.print("Enter the number of columns in the second matrix:");
n = sc.nextInt(); int a[][] = new int[p][q]; int b[][] = new int[m][n];
//Initialize the first Matrix
System.out.println("Enter all the elements of first matrix:"); for
(int i = 0; i < p; i++)
{
for (int j = 0; j < q; j++)
{
a[i][j] = sc.nextInt();
}
}
System.out.println("");
System.out.println("Enter all the elements of second matrix:"); for
(int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
b[i][j] = sc.nextInt();
}
} 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 java.util.Scanner;
class palindrome
{
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("Enter a string as an input to check whether it is
palindrome or not");
String input= scanner.nextLine();

if(isPalindrome(input))
{
System.out.println(input+" is a palindrome string");
}
else
{
System.out.println(input+" is not a palindrome string");
}
} public static boolean isPalindrome(String
str) { int left = 0, right = str.length() - 1;

while(left < right)


{ if(str.charAt(left) !=
str.charAt(right))
{ return
false;
}
left++;
right--; }
return true;
}
}
Output :-
Program 4
4. Write a program of find the area of rectangle using constructor.

Syntax :-
import java.util.Scanner;

public class Area_Perimeter

public static void main(String[] args)

int l, b, area;

Scanner s = new Scanner(System.in); System.out.print("Enter

length of rectangle:");

l = s.nextInt();

System.out.print("Enter breadth of rectangle:");

b = s.nextInt();
area = l * b;

System.out.println("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);
System.out.println("Rectangle Area : " + r.getArea());
System.out.println("Square Area : " + s.getArea()); System.out.println();
if ((r.getArea() ) && (r.getArea() > s.getArea())) {
System.out.println("Rectangle has the largest area.");
}
else if( s.getArea() ) {
System.out.println("Square has the largest area.");
}
}
}
class Rectangle
{ double length;
double breadth;
Rectangle(double length, double breadth { this.length
= length;

this.breadth = breadth;
} double getArea()
{ return length * breadth;
}
}
class Square { double side;
Square(double side)
{ this.side = 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 java.io.*;
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;
}
}
}
System.out.println(

"The names in Ascending order are: ");


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

System.out.println(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);
thread.start();/*from
}
}
}
class Calculator implements Runnable {

private int number;

public Calculator(int number)


{ this.number=number;
}
@Override public
void run() {

for (int i=2; i<=5; i++){


System.out.printf("%s: %d * %d =
%d\n",Thread.currentThread().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

System.out.println ("::Try Block::");

int data = 125 / 5;

System.out.println ("Result:" + data);

}
catch (NullPointerException e) {

System.out.println ("::Catch Block::");

System.out.println (e);
}

finally {

System.out.println (":: Finally Block::");

System.out.println ("No Exception::finally block executed");

System.out.println ("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()

System.out.println("Car is running.");

} public void

speed()

System.out.println("Speed of Car: 50 Km/h");

public static void main(String args[])

Car Car = new Car();


Car.run();

Car.speed();

System.out.println("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 java.io.*; class New {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the name of the file: ");
String file_name = input.nextLine();
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);
st.eolIsSignificant(true);
while(st.nextToken()!=StreamTokenizer.TT_EOF)
{ switch(st.ttype) { case StreamTokenizer.TT_EOL:
nlines++ ; nchars++
; break ; case
StreamTokenizer.T
T_WORD:
nwords++; default:
if(st.ttype==StreamTokenizer.TT_WORD)
nchars+=st.sval.length(); break; }
} }
catch(FileNotFoundException e) catch(IOException e){
System.out.println("Error occured reading file:");
}
System.out.println("The no of charecters are: " +nchars);
System.out.println("The no of words are: " +nwords);
System.out.println("The no of lines are: " +nlines);
}}

Output :-

You might also like