0% found this document useful (0 votes)
16 views6 pages

Matrix Equality Checker Class

Uploaded by

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

Matrix Equality Checker Class

Uploaded by

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

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 [Link].*;
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([Link]);
[Link](“Enter the elements for the array:”);
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=[Link]();
}
}
}
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++)
{
[Link](a[i][j]+”\t”);
}
[Link]();
}
}
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
[Link](“Enter the row and column size for the array:”);
int a=[Link]();
int b=[Link]();
EqMat A =new EqMat(a,b);
EqMat B=new EqMat(a,b);
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
if([Link](A,B)==1)
[Link](“Both are equal matrix”);
else
[Link](“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 [Link];

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([Link]);

[Link]("ORIGINAL STRING: ");

str = [Link]().toLowerCase();

[Link]("MASK STRING: ");


msk = [Link]().toLowerCase();

public void form(){

for(int i = 0; i < [Link](); i++){

char ch = [Link](i);

if([Link](ch) == -1)

nstr += ch;

public void display(){

[Link]("ORIGINAL STRING: " + str);

[Link]("NEWLY FORMED STRING: " + nstr);

public static void main(String[] args){

StringOp obj = new StringOp();

[Link]();

[Link]();

[Link]();

public static String convert(int number, int base){

int quotient = number / base;

int remainder = number % base;

if (quotient == 0) // base case

{
return [Link](remainder);

else {

return convert(quotient, base) + [Link](remainder);

Common questions

Powered by AI

The convert method recursively divides the input number by the target base, capturing the remainder at each step. When the quotient becomes zero (base case), the recursion stops and the digits are concatenated in reverse order of computation to give the number in the new base .

Two matrices are considered equal if they have the same dimension and their corresponding elements are equal .

Error handling could be implemented using try-catch blocks around the input operations to catch exceptions such as InputMismatchException or illegal argument checks, like verifying if row and column sizes are positive. This would prevent runtime errors and prompt the user for valid inputs .

The StringOp class removes all characters in the mask string from the original string by iterating over the original string and checking for each character whether it exists in the mask string. If it does not exist, the character is added to a new string, effectively removing the masked characters .

The form method is crucial for string manipulation in the StringOp class as it performs the core operation of stripping unwanted characters from the original string based on the mask string. It iterates over the original string, constructs a new string with characters not found in the mask, and sets it as the resultant string .

The check method in the EqMat class compares two EqMat objects to determine if they are equal by comparing their corresponding elements. It returns 1 if the matrices are equal and 0 if they are not .

The display method enhances usability by providing a straightforward way to visualize the matrix's elements after they are entered. This immediate feedback helps in verifying inputs or diagnosing data-related issues during runtime .

Polymorphism can be employed by creating subclasses of EqMat that override its methods for customized equality checks, perhaps including tolerance levels for floating point matrices or additional criteria like weighted matrix elements, extending its functionality beyond simple element comparison .

In the EqMat class, Java uses the Scanner class to read integer inputs for the array elements. Each element is read in nested loops corresponding to the row and column indices of the matrix .

Initializing data members in the StringOp constructor ensures that the strings str, msk, and nstr are set to legal initial values, preventing null references and providing a default state for the object before any operations are performed .

You might also like