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.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

Add a comment
Know the answer?
Add Answer to:
Write a class named RowSort which has the following members. Private: (1) double matrix 0O (a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In this assignment, you are asked to: 1. create a Matrix class that stores and operate...

    In this assignment, you are asked to: 1. create a Matrix class that stores and operate on a dynamic two-dimensional array. The class has the following structure: Private member variables: - int ** mat; - int rows; - int cols; Public member functions: +Default constructor: sets both rows and cols to 3 and creates 3x3 matrix dynamically +Parameterized constructor: sets the rows and cols to the values passed to the constructor and create a matrix with the given dimensions. +Destructor:...

  • Write a java program that creates a class Car. Class Car contains the following private member...

    Write a java program that creates a class Car. Class Car contains the following private member fields: make, model, year, and miles. The class Car has a constructor that takes as arguments the car make, model, and year. In addition, the constructor sets miles to 100 as default. Class Car has accessors and mutators to get and set the make, model, year and miles on the car. The program should complete the following tasks: Ask the user to enter make,...

  • Write a C# application that implements a class ‘Prism’ with the following members. Five private data...

    Write a C# application that implements a class ‘Prism’ with the following members. Five private data members ‘length’, ‘height’, ‘width’, ‘Volume’ and ‘Area’ of double data type. Implement the constructor Prism( ) to accept the length, height, and width of the rectangular prism from the user. Implement three methods, Vol(), Area(), and Result() Vol() – to calculate the volume of the prism using the formulae Volume= length ×height ×width Area() – to calculate the surface area of the prism using...

  • In C++, Step 1: Implement the Student Class and write a simple main() driver program to...

    In C++, Step 1: Implement the Student Class and write a simple main() driver program to instantiate several objects of this class, populate each object, and display the information for each object. The defintion of the student class should be written in an individual header (i.e. student.h) file and the implementation of the methods of the student class should be written in a corresponding source (i.e. student.cpp) file. Student class - The name of the class should be Student. -...

  • Java Code: 2. Define a class ACCOUNT with the following members: Private members Acno of type...

    Java Code: 2. Define a class ACCOUNT with the following members: Private members Acno of type int Name of type String Balance of type float Public members void Init) method to enter the values of data members void Show) method to display the values of data members void Deposit(int Amt) method to increase Balance by Amt void Withdraw(int Amt) method to reduce Balance by Amt (only if required balance exists in account) float RBalance() member function that returns the value...

  • Create the class Book which has the following members: 1. private members: 1. title as String...

    Create the class Book which has the following members: 1. private members: 1. title as String 2. isbn as String 3. author as String 4. price as double 2. public members: 1. default constructor which initializes all data members to their default values. 2. non-default constructor which initializes all data members to parameters values. 3. toString which returns a representing String of the Book. 4. add all the setters and getters. Create the class EBook which is a subclass of...

  • (The Account class) Design a class named Account that contains: A private int data field named...

    (The Account class) Design a class named Account that contains: A private int data field named id for the account (default 0). A private double data field named balance for the account (default 0). A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default...

  • C++ design a class named Technician that contains private data members to store the following: -technician's...

    C++ design a class named Technician that contains private data members to store the following: -technician's name (a string) - number of service calls - total time of all service calls - average service call time the technician class should also have the following public member functions: - a constructor function that initializes all data members by obtaining their values from the users. with the exception of the average service call time which will be calculated as: total_time/number_of_call - a...

  • Your will write a class named Gasket that can be used to build Gasket objects (that...

    Your will write a class named Gasket that can be used to build Gasket objects (that represent Sierpinski gaskets) that you can display graphically.  Your Gasket class is defined in the provided file Gasket.h.   DO NOT CHANGE the file Gasket.h.  The attributes and methods defined for class Gasket are described below.     ·sideLength            an int that holds the length of each side of the Gasket.  The length of the side is measured as a number of pixels ·xLocation              an int that holds the x coordinate (in pixels)...

  • in c++ Define and implement the class Employee with the following requirements: private data members string...

    in c++ Define and implement the class Employee with the following requirements: private data members string type name a. b. double type hourlyRate 2. public member functions a. default constructor that sets the data member name to blank"and hourlyRate to zero b. A constructor with parameters to initialize the private data members c. Set and get methods for all private data members d. A constant method weeklyPay() that receives a parameter representing the number of hours the employee worked per...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT