VB icon

Matrix Addition using Class & Inheritance

Email
Submitted on: 7/15/2016 6:24:50 PM
By: Harikrishnan  
Level: Intermediate
User Rating: Unrated
Compatibility: Java (JDK 1.1), Java (JDK 1.2), Java (JDK 1.3), Java (JDK 1.4), Java (JDK 1.5)
Views: 2075
author picture
 
     Share , Project & review A class Application using Inheritance. In this two class is created and the second class extends the base class. One object is created with main class . it shows reusability Refer Code

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Matrix Addition using Class & Inheritance
// Description:Share , Project & review
A class Application using Inheritance. In this two class is created and the second class extends the base class. One object is created with main class . it shows reusability
Refer Code
// By: Harikrishnan
//**************************************

import java.util.*;
class Sum_first_matrix
{
Scanner obj;
int m,n,i,j;
int [][] a;
int [][] b;
int [][] c;
public void matrix_create()
{
 obj=new Scanner(System.in);
System.out.println("Enter The rows and columns");
 m=obj.nextInt();
 n=obj.nextInt();
System.out.println("Enter the array elements");
 a=new int[m][n];
 b=new int [m][n];
 c=new int[m][n];
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=obj.nextInt();
}
}
}
 void display()
{
System.out.println("The array is");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.print("\t" + a[i][j]);
}
System.out.println();
}
}
public void second_matrix_create()
{
 
System.out.println("Enter the Second array elements");
 
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
b[i][j]=obj.nextInt();
}
}
}
 void second_display()
{
System.out.println("The array is");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.print("\t" + b[i][j]);
}
System.out.println();
}
}
}
class Second_matrix extends Sum_first_matrix
{
public void add()
{
 
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
c[i][j]= a[i][j] + b[i][j];
}
}
}
void add_display()
{
System.out.println("The Summed array is");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.print("\t" + c[i][j]);
}
System.out.println();
}
}
}
class Main
{
public static void main(String args[])
{
Second_matrix obj1=new Second_matrix();
obj1.matrix_create();
obj1.display();
obj1.second_matrix_create();
obj1.second_display();
obj1.add();
obj1.add_display();
}
}
***Output***
------------
C:\Users\user\My Documents\java\jdk1.7.0\bin>javac Main.java
C:\Users\user\My Documents\java\jdk1.7.0\bin>java Main
Enter The rows and columns
2 2
Enter the array elements
1 2
3 4
The array is
12
34
Enter the Second array elements
4
5
6
7
The array is
45
67
The Summed array is
57
911


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.