0% found this document useful (0 votes)
3 views21 pages

Java Prog Lab Ses3

The document outlines a live session for a Java Programming Lab as part of a Master of Computer Application program, detailing learning objectives and exercises. Key exercises include performing matrix multiplication using 2D arrays, string operations, and demonstrating superclass and subclass concepts. Additionally, it provides example codes and algorithms for checking palindromes and using exception handling methods.

Uploaded by

robosiddhibihar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views21 pages

Java Prog Lab Ses3

The document outlines a live session for a Java Programming Lab as part of a Master of Computer Application program, detailing learning objectives and exercises. Key exercises include performing matrix multiplication using 2D arrays, string operations, and demonstrating superclass and subclass concepts. Additionally, it provides example codes and algorithms for checking palindromes and using exception handling methods.

Uploaded by

robosiddhibihar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CENTRE FOR DISTANCE AND ONLINE EDUCATION

CENTRE FOR DISTANCE AND ONLINE EDUCATION

LIVE SESSION – SESSION NO. 3


O02LA502– JAVA PROGRAMMING LAB.

MASTER OF COMPUTER APPLICATION

JOYASHRI BASAK
ASSISTANT PROFESSOR
At the end of this session, you should be able to:

Learning
• Perform Java coding using 2 D arrays for
Objectives Matrix Multiplication
• Perform string operations using various
methods of String class – equals(), charAt(),
length()
• Make use of superclass, subclass and extends
keyword.
Exercises in Java
1 Write a Java program to display employee details (employee name, salary) using class and method invocation

2 Write a java program to demonstrate the use of (i) increment (++) and decrement operator (--) (ii) IF-ELSE
Statement (iii) While loop.
3 Write a java program to perform matrix multiplication using 2-dimenstional array

4 Write a Java Program to check whether given String is Palindrome or not

5 Write a java program to demonstrate the use of super class and sub class concept.

6 Write a Java program using exception handling methods

7 Write a Java program to create a class and a subclass and demonstrate that a method can override.

8 Write a Java program to demonstrate the use of byte stream.

9. Write a Java program to create and start multiple threads that increment a shared counter variable
concurrently.
Exercise 3:

Write a Java program to perform matrix multiplication using 2-


dimensional array

• The product of two matrices A and B is defined if the number of


columns of A is equal to the number of rows of B.
• Let A = [aij] be an m × n matrix and B = [bjk] be an n × p matrix. Then
the product of the matrices A and B is the matrix C of order m × p.
• To get the (i, k)th element c of the matrix C, we take the ith row of A
and kth column of B, multiply them element-wise and take the sum
of all these products.
Exercise 3:

Write a Java program to perform matrix multiplication using 2-dimensional array

In other words, if A = [aij]m×n , B = [bjk]n×p , then the ith row of


A = [ai1 ai2 …ain ] and the kth column of

then, c = ai1b1k + ai2b2k + ai3b3k + … + ainbnk = ∑aijbjk.


The matrix C = Σ[cik]m×p is the product of A and B.
Exercise 3:

Write a Java program to perform matrix multiplication using 2-dimensional array


Let’s consider a simple 2 × 2 matrix multiplication:
3 7 6 2
𝐴= 𝑎𝑛𝑑 𝐵=
4 9 5 8

Now each of the elements of product matrix C = AB can be calculated as follows:


• C11 = AB11 = 3 × 6 + 7 ×5 = 53
• C12 = AB12 = 3 × 2 + 7 × 8 = 62
• C21 = AB21 = 4 × 6 + 9 × 5 = 69
• C22 = AB22 = 4 × 2 + 9 × 8 = 80

Therefore,
53 62
C = AB =
69 80
Exercise 3: (Example code)

Write a Java program to perform matrix multiplication using 2-dimensional array


// program to Multiply two matrices using 2 D arrays
public class Main{
public static void main(String args[]){
//creating two matrices
int a[][]={{3, 7}, {4, 9}};
int b[][]={{6, 2}, {5, 8}};

//creating another matrix to store the multiplication of two matrices


int c[][]=new int[2][2]; //2 rows and 2 columns

//multiplying and printing multiplication of 2 matrices


for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
c[i][j]=0;
for(int k=0;k<2;k++)
{
c[i][j]+=a[i][k]*b[k][j];
} //end of k loop
[Link](c[i][j]+" "); //printing matrix element
} //end of j loop
[Link]();//new line
}
}
}
Exercise 3: (Example code)

Write a Java program to perform matrix multiplication using 2-dimensional array

Exercise:

Write a Java Program to multiply following two matrices using 2-D array.

1 3
1 2 3 23 35
(i) A = and B = 5 7 Ans: C = AB =
4 5 6 53 83
4 6
Exercise 4:

Write a Java Program to check whether given String is Palindrome or not

• A palindrome is a word, sentence, verse, or even number that reads the same
backward or forward.

Example:
• madam, radar, noon, civic, racecar, level, mom
• 12321, 1001, 53735

Note: Since Java is case sensitive, Radar, radaR, rAdar are different strings.
Exercise 4:

Write a Java Program to check whether given String is Palindrome or not

Algorithm: (An example)


1. Get the string to check for palindrome
2. Hold the string in temporary variable
3. Reverse the string
4. Compare the temporary string with reversed string
5. If both string are same, print “string is a palindrome"
6. Else print “string is not a palindrome "
Exercise 4:

Write a Java Program to check whether given String is Palindrome or not

• The equals() method in Java String class compares the two given strings
based on the content of the string.
• If any character is not matched, it returns false.
• If all characters are matched, it returns true.

usage:
Public boolean equals(Object anotherObject)
Parameter
anotherObject : another object, i.e., compared with this string.

Returns:
true if characters of both strings are equal otherwise false.
Exercise 4:

Write a Java Program to check whether given String is Palindrome or not

[Link]() method :
• The charAt() method returns the character at the specified index in a string.
• The Index of the first character in a string is 0, the second character is 1, and so on.
• The index value should lie between 0 and length() – 1.

[Link]() method:
• The length() method is applicable to string objects.
• The length() method returns the number of characters present in the string.
• The length() method is suitable for string objects but not for arrays.
Exercise 4: (Example code)

Write a Java Program to check whether given String is Palindrome or not


import [Link];
class Main
{
public static void main(String args[]) Important class and methods to remember:
{ • Scanner
String str, rev = ""; • nextLine()
Scanner sc = new Scanner([Link]); • length()
[Link]("Enter a string:"); • charAt()
str = [Link](); • equals()
int length = [Link]();
for ( int i = length - 1; i >= 0; i-- )
rev = rev + [Link](i);

if ([Link](rev))
[Link](" Entered string is a palindrome");
else
[Link]("Entered string is not a palindrome");

}
}
Exercise 4: (Palindrome -- Convert string to lowercase)
Write a Java Program to check whether given String is Palindrome or not (converting case to lowercase)

import [Link];
class Main
{
public static void main(String args[]) Important method to remember:
{ • toLowerCase()
String str, rev = "";
Scanner sc = new Scanner([Link]);
[Link]("Enter a string:");
str = [Link]();
//use toLowerCase() method to convert string to lowercase
str = [Link]();
int len = [Link]();

for ( int i = len -1 ; i >= 0; i-- )


rev = rev + [Link](i);

if ([Link](rev))
[Link](" Entered string is a palindrome");
else
[Link]("Entered string is not a palindrome");
}
}
Exercise 5:

Write a Java program to demonstrate the use of superclass and subclass


concept.

• Super Class/Parent Class: The class whose features are inherited is known
as a superclass (or a base class or a parent class).

• Sub Class/Child Class: The class that inherits the other class is known as a
subclass(or a derived class, extended class, or child class). The subclass
can add its own attributes(fields) and methods in addition to the superclass
attributes and methods.
Exercise 5:

Write a Java program to demonstrate the use of superclass and subclass


concept.

• A subclass inherits all the members (fields, methods, and nested classes)
from its superclass.
• Constructors are not members, so they are not inherited by subclasses, but
the constructor of the superclass can be invoked from the subclass.

• The extends keyword extends a class (indicates that a class is inherited from
another class). To inherit from a class, use the extends keyword.
Exercise 5: (Example)
Write a java program to demonstrate the use of superclass and subclass concept.

// Parent class or Super class or Base class


class Parent { // Driver class
int money = 10000; public class Main {
public void displayParent() // Main function
{ public static void main(String[] args)
[Link]("I am a Parent."); {
}
Child obj = new Child();
}
// Child class or sub class or derived class [Link]([Link]);
class Child extends Parent { [Link]([Link]);
int pocketmoney = 2000; [Link]();
public void displayChild() [Link]();
{ }
[Link]("I am a Child."); }
}
}
Key Learnings






STUDENT SUPPORT
Thank You !!

You might also like