Computer Applications
Write a class program with the following specifications:
Class name — Matrix
Data members — int array m[][] with 3 rows and 3 columns
Member functions:
- void getdata() — to accept the numbers in the array
- void rowsum() — to find and print the sum of the numbers of each row
- void colsum() — to find and print the sum of numbers of each column
Use a main function to create an object and call member methods of the class.
Java
Encapsulation & Inheritance in Java
14 Likes
Answer
import java.util.Scanner;
public class Matrix
{
private final int ARR_SIZE = 3;
private int[][] m;
public Matrix() {
m = new int[ARR_SIZE][ARR_SIZE];
}
public void getdata() {
Scanner in = new Scanner(System.in);
for (int i = 0; i < ARR_SIZE; i++) {
System.out.println("Enter elements of row "
+ (i+1) + ":");
for (int j = 0; j < ARR_SIZE; j++) {
m[i][j] = in.nextInt();
}
}
}
public void rowsum() {
for (int i = 0; i < ARR_SIZE; i++) {
int rSum = 0;
for (int j = 0; j < ARR_SIZE; j++) {
rSum += m[i][j];
}
System.out.println("Row " + (i+1) + " sum: " + rSum);
}
}
public void colsum() {
for (int i = 0; i < ARR_SIZE; i++) {
int cSum = 0;
for (int j = 0; j < ARR_SIZE; j++) {
cSum += m[j][i];
}
System.out.println("Column " + (i+1) + " sum: " + cSum);
}
}
public static void main(String args[]) {
Matrix obj = new Matrix();
obj.getdata();
obj.rowsum();
obj.colsum();
}
}Variable Description Table
Program Explanation
Output
![BlueJ output of Write a class program with the following specifications: Class name — Matrix Data members — int array m[][] with 3 rows and 3 columns Member functions: (a) void getdata() — to accept the numbers in the array (b) void rowsum() — to find and print the sum of the numbers of each row (c) void colsum() — to find and print the sum of numbers of each column Use a main function to create an object and call member methods of the class. BlueJ output of Write a class program with the following specifications: Class name — Matrix Data members — int array m[][] with 3 rows and 3 columns Member functions: (a) void getdata() — to accept the numbers in the array (b) void rowsum() — to find and print the sum of the numbers of each row (c) void colsum() — to find and print the sum of numbers of each column Use a main function to create an object and call member methods of the class.](https://cdn1.knowledgeboat.com/img/apc10/chap_8-p4.jpg)
Answered By
5 Likes
Related Questions
Write a program by using a class with the following specifications:
Class name — Factorial
Data members — private int n
Member functions:
- void input() — to input a number
- void fact() — to find and print the factorial of the number
Use a main function to create an object and call member methods of the class.
Write a program by using a class with the following specifications:
Class name — Salary
Data members — private int basic
Member functions:
- void input() — to input basic pay
- void display() — to find and print the following:
da = 30% of basic
hra = 10% of basic
gross = basic + da + hra
Use a main function to create an object and call member methods of the class.
Write a program to use a class Account with the following specifications:
Class name — Account
Data members — int acno, float balance
Member Methods:
- Account (int a, int b) — to initialize acno = a, balance = b
- void withdraw(int w) — to maintain the balance with withdrawal (balance - w)
- void deposit(int d) — to maintain the balance with the deposit (balance + d)
Use another class Calculate which inherits from class Account with the following specifications:
Data members — int r,t ; float si,amt;
Member Methods:
- void accept(int x, int y) — to initialize r=x,t=y,amt=0
- void compute() — to find simple interest and amount
si = (balance * r * t) / 100;
a = a + si; - void display() — to print account number, balance, interest and amount
main() function need not to be used
Write a program by using class with the following specifications:
Class name — Sale
Data members/ Instance variables:
- String title, author,publication
- double price
Member methods:
- void input() — to accept title, author name and publication name and price of a book
- void display() — to display title, author name and publication name and price of a book
Now, create another class 'Purchase' that inherits class 'Sale' having the following specifications:
Class name — Purchase
Data members/ Instance variables:
- int noc
- int amount;
Member methods:
- void accept() — to enter the number of copies purchased
- void calculate( ) — to find the amount by multiplying number of copies ordered and price (i.e., noc * price)
- void show() — to display the elements describes in base class along with the number of copies purchased and amount to be paid to the shopkeeper