Write a class named RowSort which has the following members. Private: (1) double
ID: 3880944 • Letter: W
Question
Write a class named RowSort which has the following members. Private: (1) double matrix 0O (a 2D array): (2) int columns; (3) int rows Public: (1) A default constructor that creates a 2D array, 8 6 4 (2) A constructor that takes three values for the three private members, and creates a 2D array accordingly. (3) The "get" and "set" methods for the three private members. (4) A method that displays a 2D array, stored in the private member matrix, on the screen (5) A method that sorts the rows in the private member matrix in the increasing order. .Step1 Call the default constructor of RowSort, and then call proper methods to sort rows as a matrix. Then, write a class that has main method. The main method does the following. and to display the matrix. Step 2 Ask the user to enter two positive integers for columns and rows respectively, and then ask the user to enter real numbers for matrix accordingly .Step 3 Call the other constructor of RowSort, and then call the methods in (5) and (4) to display the matrix with each row sorted.Explanation / Answer
Using JAVA, since no language has been mentioned.
import java.util.*;
class RowSort {
private double arr[][];
private int col, row;
// Default constructor
public RowSort() {
// Initializing new array
arr = new double[3][3];
int i=0,j=0,k=9;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
arr[i][j] = k--;
col = row = 3;
}
// constructor taking 3 values of the private members
public RowSort(double a[][], int b, int c) {
row = b;
col = c;
arr = new double[row][col];
int i,j;
// Copying the array passed inside argument to original array
for( i=0; i<row; i++)
for( j=0; j<col; j++)
arr[i][j] = a[i][j];
}
// Getter functions
public int getRow() {
return row;
}
public int getCol() {
return col;
}
public double[][] getArr() {
return arr;
}
// Setter functions
public void setRow(int a) {
row = a;
}
public void setCol(int a) {
col = a;
}
public void setArr(double a[][]) {
arr = a;
}
// Function for sorting each row of the array
public void sort() {
int q,i,j;
for(q=0;q<row;q++) {
double temp = 0;
// Bubble sort
for(i=0; i < col; i++){
for(j=1; j < (col-i); j++){
if(arr[q][j-1] > arr[q][j]){
//swap elements
temp = arr[q][j-1];
arr[q][j-1] = arr[q][j];
arr[q][j] = temp;
}
}
}
}
}
// Displaying each element of the array.
public void display(){
int i,j;
for(i=0;i<row;i++) {
for(j=0;j<col;j++)
System.out.print(arr[i][j] + " ");
System.out.println();
}
}
public static void main(String[] args) {
RowSort r = new RowSort();
r.sort();
r.display();
int col, row, i, j;
Scanner sc = new Scanner(System.in);
System.out.println("Enter row: ");
row = sc.nextInt();
System.out.println("Enter column: ");
col = sc.nextInt();
double a[][] = new double[10][10];
// Input the 2D array
System.out.println("Enter the values of the matrix:");
for(i=0;i<row;i++)
for(j=0;j<col;j++)
a[i][j] = sc.nextDouble();
// Using the parameterized constructor
RowSort r1 = new RowSort(a,row,col);
r1.sort();
r1.display();
}
}
OUTPUT:
7.0 8.0 9.0
4.0 5.0 6.0
1.0 2.0 3.0
Enter row:
2
Enter column:
4
Enter the values of the matrix:
1 3 2 8
3 0 1 9
1.0 2.0 3.0 8.0
0.0 1.0 3.0 9.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.