Question

In: Computer Science

To the TwoDArray class, add a method called transpose() that generates the transpose of a 2D...

To the TwoDArray class, add a method called transpose() that generates the transpose of a 2D array with the same number of rows and columns (i.e., a square matrix). Add appropriate code in main() of the TwoDArrayApp class to execute the transpose() method and use the display() method to print the transposed matrix.

/**
* TwoDArray.java
*/

public class TwoDArray
{
   private int a[][];
   private int nRows;

public TwoDArray(int maxRows, int maxCols)        //constructor
{
   a = new int[maxRows][maxCols];
   nRows = 0;
}
//******************************************************************
   public void insert (int[] row)
   {
       a[nRows] = row;
       nRows++;
   }
//******************************************************************
   public void display()
   {
       for (int i = 0; i < nRows; i++)
       {
           for (int j = 0; j < a[0].length; j++)
               System.out.format("%5d", a[i][j]);
           System.out.println(" ");
       }
   }
}

public class TwoDArrayApp{
  
public static void main(String[] args )
{
   int maxRows = 20;
   int maxCols = 20;
  
   TwoDArray arr = new TwoDArray(maxRows, maxCols);    
   int b[][] = {{1, 2, 3, 4}, {11, 22, 33, 44}, {2, 4, 6, 8},{100, 200, 300, 400}};
  
  
   arr.insert(b[0]); arr.insert(b[1]); arr.insert(b[2]); arr.insert(b[3]);
  

  
   System.out.println("The original matrix: ");
   arr.display();
  
   System.out.println("Column sum: ");
   arr.sumCols();  
  
   System.out.println(" ");
   System.out.println("\nThe matrix after being transposed: ");
   arr.transpose();
  
  
  

  
}
}

Solutions

Expert Solution

/**
 * TwoDArray.java
 */
public class TwoDArray {
    private int a[][];
    private int nRows;

    public TwoDArray(int maxRows, int maxCols) //constructor
    {
        a = new int[maxRows][maxCols];
        nRows = 0;
    }

    //******************************************************************
    public void insert(int[] row) {
        a[nRows] = row;
        nRows++;
    }

    //******************************************************************
    public void display() {
        for (int i = 0; i < nRows; i++) {
            for (int j = 0; j < a[0].length; j++)
                System.out.format("%5d", a[i][j]);
            System.out.println(" ");
        }
    }

    public void sumCols() {
        for (int i = 0; i < a[0].length; i++) {
            int sum = 0;
            for (int j = 0; j < nRows; j++) {
                sum += a[j][i];
            }
            System.out.println("Sum of all elements of column " + i + " is " + sum);
        }
    }

    public TwoDArray transpose() {
        TwoDArray result = new TwoDArray(a[0].length, nRows);
        for (int i = 0; i < a[0].length; i++) {
            int[] row = new int[nRows];
            for (int j = 0; j < nRows; j++)
                row[j] = a[j][i];
            result.insert(row);
        }
        return result;
    }
}

/**
 * TwoDArrayApp.java
 */
public class TwoDArrayApp {
    public static void main(String[] args) {
        int maxRows = 20;
        int maxCols = 20;

        TwoDArray arr = new TwoDArray(maxRows, maxCols);
        int b[][] = {{1, 2, 3, 4}, {11, 22, 33, 44}, {2, 4, 6, 8}, {100, 200, 300, 400}};


        arr.insert(b[0]);
        arr.insert(b[1]);
        arr.insert(b[2]);
        arr.insert(b[3]);


        System.out.println("The original matrix: ");
        arr.display();

        System.out.println("Column sum: ");
        arr.sumCols();

        System.out.println(" ");
        System.out.println("\nThe matrix after being transposed: ");
        arr.transpose();
        arr.display();
    }
}


Related Solutions

Given the LinkedStack Class as covered in class add a method to the LinkedStack class called...
Given the LinkedStack Class as covered in class add a method to the LinkedStack class called PushBottom which will add a new item to the bottom of the stack. The push bottom will increase the number of items in the stack by 1
Add a public method called CountChildren to the OurPriorityQueue class that receives a priority value and...
Add a public method called CountChildren to the OurPriorityQueue class that receives a priority value and returns the number of children nodes below the node with the priority value. It must call a private recursive method that does the actual counting of children nodes. The recursive private method must count and return the number of nodes within a subtree where the subtree’s “root” node has a Value that equals the value passed into the method. Do not include the parent...
2.1) To the HighArray class in the highArray.java program, add a method called getMax() that returns...
2.1) To the HighArray class in the highArray.java program, add a method called getMax() that returns the value of the highest key in the array, or a -1 if the array is empty. Add some code in main() to exercise this method. You can assume all the keys are positive numbers. 2.2) Modify the method in Programming project 2.1 so that the item with the highest key is not only returned by the method but also removed from the array....
To the TwoDArray class, add a method called sumCols() that sums the elements in each column...
To the TwoDArray class, add a method called sumCols() that sums the elements in each column and displays the sum for each column. Add appropriate code in main() of the TwoDArrayApp class to execute the sumCols() method. /** * TwoDArray.java */ public class TwoDArray { private int a[][]; private int nRows; public TwoDArray(int maxRows, int maxCols) //constructor { a = new int[maxRows][maxCols]; nRows = 0; } //****************************************************************** public void insert (int[] row) { a[nRows] = row; nRows++; } //****************************************************************** public...
Add the method getTelephoneNeighbor to the SmartPhone class. Make this method return a version of the...
Add the method getTelephoneNeighbor to the SmartPhone class. Make this method return a version of the phone number that's incremented. Given Files: public class Demo4 { public static void main(String[] args) { SmartPhone test1 = new SmartPhone("Bret", "1234567890"); SmartPhone test2 = new SmartPhone("Alice", "8059226966", "[email protected]"); SmartPhone test3 = new SmartPhone(); SmartPhone test4 = new SmartPhone("Carlos", "8189998999", "[email protected]"); SmartPhone test5 = new SmartPhone("Dan", "8182293899", "[email protected]"); System.out.print(test1); System.out.println("Telephone neighbor: " + test1.getTeleponeNeighbor()); System.out.println(); System.out.print(test2); System.out.println("Telephone neighbor: " + test2.getTeleponeNeighbor()); System.out.println(); System.out.print(test3); System.out.println("Telephone...
Create a project called rise. Add a class called GCD. Complete a program that reads in...
Create a project called rise. Add a class called GCD. Complete a program that reads in a numerator (as an int) and a denominator (again, as an int), computes and outputs the GCD, and then outputs the fraction in lowest terms. Write your program so that your output looks as close to the following sample output as possible. In this sample, the user entered 42 and 56, shown in green. Enter the numerator: 42 Enter the denominator: 56 The GCD...
Please add the following method as a part of the UnorderedList class definition: •print_list: the method...
Please add the following method as a part of the UnorderedList class definition: •print_list: the method prints the elements of the list using the same format as a Python list (square brackets and commas as separators). In the main function of the modified UnorderedList.py, please test the new method to demonstrate that it works as expected. Please leave comments in the code to show what is done UnorderList.py file # Implementation of an Unordered List ADT as a linked list....
Write a class in Java called 'RandDate' containing a method called 'getRandomDate()' that can be called...
Write a class in Java called 'RandDate' containing a method called 'getRandomDate()' that can be called without instantiating the class and returns a random Date between Jan 1, 2000 and Dec 31, 2010.
What is the issue is in the add method in the SongList Class? It doesn't seem...
What is the issue is in the add method in the SongList Class? It doesn't seem to add the songs when running testing. public class Song { // instance variables private String m_artist; private String m_title; private Song m_link; // constructor public Song(String artist, String title) { m_artist = artist; m_title = title; m_link = null; } // getters and setters public void setArtist(String artist) { m_artist = artist; } public String getArtist() { return m_artist; } public void setTitle(String...
Add code to the Account class and create a new class called BalanceComparator. import java.util.*; public...
Add code to the Account class and create a new class called BalanceComparator. import java.util.*; public final class Account implements Comparable {     private String firstName;     private String lastName;     private int accountNumber;     private double balance;     private boolean isNewAccount;     public Account(             String firstName,             String lastName,             int accountNumber,             double balance,             boolean isNewAccount     ) {         this.firstName = firstName;         this.lastName = lastName;         this.accountNumber = accountNumber;         this.balance = balance;         this.isNewAccount = isNewAccount;     }     /**      * TO DO: override equals      */     @Override     public boolean equals(Object other) {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT