Two matrices are said to be equal if they have the same dimension and their
corresponding elements are equal.
For example , the two matrices A and B given below are equal:
Matrix A Matrix B
1 2 3 1 2 3
2 4 5 2 4 5
3 5 6 3 5 6
Design a class EqMat to check if tow matrices are equal or not. Assume that the two
matrices have the same dimension.
Class name : EqMat
Data members:
a[][] : to store integer elements
m, n : to store the number of rows and columns
Member functions:
EqMat(int mm, int nn) : initialize the data members m=mm and n=nn
void readarray() : to enter the elements in the array
int check(EqMat P, EqMat Q) : checks if the parameterized objects P and Q are equal
and returns 1 if true,otherwise returns 0.
void print() : displays the array elements
Define the class and define main() to create objects and call the functions accordingly
to enable the task.
Program:
import java.util.*;
class EqMat{
int a[][], m,n;
EqMat( int mm, int nn)
{
m=mm;
n=nn;
a=new int[m][n];
}
public void readarray()
{
Scanner sc=new Scanner(System.in);
System.out.println(“Enter the elements for the array:”);
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=sc.nextInt();
}
}
}
public int check(EqMat P, EqMat Q)
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(P.a[i][j]!=Q.a[i][j])
return 0;
}
}
return 1;
}
Report this ad
public void print()
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(a[i][j]+”\t”);
}
System.out.println();
}
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println(“Enter the row and column size for the array:”);
int a=sc.nextInt();
int b=sc.nextInt();
EqMat A =new EqMat(a,b);
EqMat B=new EqMat(a,b);
A.readarray();
B.readarray();
A.print();
System.out.println();
B.print();
if(A.check(A,B)==1)
System.out.println(“Both are equal matrix”);
else
System.out.println(“Both are unequal matrix”);
}
}
Output:
Enter the row and column size for the array:
3
3
Enter the elements for the array:
2
3
4
5
6
7
8
9
1
Enter the elements for the array:
2
3
4
5
6
7
8
9
2
First matrix
234
567
891
Second matrix
234
567
892
Both are unequal matrix
Given are two strings, input string and a mask string that remove all the characters of
the mask string from the original string.
Example:
INPUT:
ORIGINAL STRING: communication
MASK STRING: mont
OUTPUT: cuicai
A class StringOp is defined as follows to perform above operation.
Some of the members of the class are given below:
Class name: StringOp
Data members/instance variables:
str: to store the original string
msk: to store the mask string
nstr: to store the resultant string
Methods/Member functions:
StringOp(): default constructor to initialize the data members with legal initial values
void accept(): to accept the original string str and the mask string msk in lowercase
void form(): to form the new string nstr after removal of characters present in mask string
from the original string.
void display(): to display the original string nstr and the newly formed string nstr
Specify the class StringOp giving details of the constructor, void accept(), void form()
and void display(). Define a main() function to create an object and call all the
functions accordingly to enable the task.
import java.util.Scanner;
class StringOp{
String str;
String msk;
String nstr;
public StringOp(){
str = new String();
msk = new String();
nstr = new String();
public void accept(){
Scanner in = new Scanner(System.in);
System.out.print("ORIGINAL STRING: ");
str = in.nextLine().toLowerCase();
System.out.print("MASK STRING: ");
msk = in.nextLine().toLowerCase();
public void form(){
for(int i = 0; i < str.length(); i++){
char ch = str.charAt(i);
if(msk.indexOf(ch) == -1)
nstr += ch;
public void display(){
System.out.println("ORIGINAL STRING: " + str);
System.out.println("NEWLY FORMED STRING: " + nstr);
public static void main(String[] args){
StringOp obj = new StringOp();
obj.accept();
obj.form();
obj.display();
public static String convert(int number, int base){
int quotient = number / base;
int remainder = number % base;
if (quotient == 0) // base case
{
return Integer.toString(remainder);
else {
return convert(quotient, base) + Integer.toString(remainder);