Aman JAva
Aman JAva
Aman JAva
(20BCA05)
`
Sr Programs Date Signature
No.
Syntax :-
int b;
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();
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;
Syntax :-
import java.util.Scanner;
int l, b, area;
length of rectangle:");
l = s.nextInt();
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(
System.out.println(names[i]);
}
}
}
Output :-
Program 7
7. Write a Java program of Thread.
Syntax :-
class Main {
try
}
catch (NullPointerException e) {
System.out.println (e);
}
finally {
Output :-
Program 9
9. Write a java program of user defind package
implements Vehicle
{ public void
run()
System.out.println("Car is running.");
} public void
speed()
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 :-