Array,String,Recursive
Array,String,Recursive
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
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.
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 = in.nextLine().toLowerCase();
char ch = str.charAt(i);
if(msk.indexOf(ch) == -1)
nstr += ch;
obj.accept();
obj.form();
obj.display();
{
return Integer.toString(remainder);
else {