Question

In: Computer Science

On the following code that contains a method which returns a new matrix (2-d array) with...

On the following code that contains a method which returns a new matrix (2-d array) with same number of rows and columns as has its parameter (matrix). Each entry in the returned array should equal the result of multiplying the entry at the same row and column in matrix by the value of scalar. code:

package edu.buffalo.cse116;

/** * Class which contains a method which takes in a 2-dimensional array and a scalar value and returns their product. */ public class ScalarMult { /** * Allocates a new 2-d array and then sets each entry to be the product of {@code scalar} and the entry in * {@code matrix} at the same row and column. This was inspired by the only "joke" told by my calculus teacher:
* Why can't you cross a mountain climber and a grape? Because you cannot cross a scalar.
*
* Yeah, I did not find it funny either. * * @param matrix 2-d array which we would like to have multiplied by the given value. * @param scalar The new matrix will be equal to having each entry in {@code matrix} multipled by this value. * @return The product of this matrix and constant value. */

public int[][] scalarMult(int[][] matrix, int scalar) { } }

Solutions

Expert Solution

public class ScalarMult {

   public ScalarMult() {
       // TODO Auto-generated constructor stub
   }

   public int[][] scalarMult(int[][] matrix, int scalar){
      
       for(int i=0;i<matrix.length;i++){
           for(int j=0;j<matrix[i].length;j++){
               matrix[i][j] = scalar*matrix[i][j];
           }
       }
       return matrix;
   }
  
   public static void main(String[] args) {
      
       ScalarMult sm = new ScalarMult();
       int[][] matrix = {{2,3,4},{5,6,7}};
       System.out.println("Original Matrix:");
       for(int i=0;i<matrix.length;i++){
           for(int j=0;j<matrix[i].length;j++){
               System.out.print(matrix[i][j]+" ");
           }
           System.out.println();
       }
       sm.scalarMult(matrix, 2);
       System.out.println("After Multiplication: ");
       for(int i=0;i<matrix.length;i++){
           for(int j=0;j<matrix[i].length;j++){
               System.out.print(matrix[i][j]+" ");
           }
           System.out.println();
       }
   }

}


Related Solutions

Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
Write Matrix Addition 2 D (dimensional) Array program in c++.
Write Matrix Addition 2 D (dimensional) Array program in c++.
Create a “Main” method that contains two 2-Dimensional arrays of characters. The first array, which we...
Create a “Main” method that contains two 2-Dimensional arrays of characters. The first array, which we will call our visible field, needs to be 5 by 5 and should initially hold the underscore character “_”.  This will be used in our game of minesweeper to represent the possible locations the player can check. The second array, which we will call our hidden field, should also be 5 by 5 but filled with the capital letter "S” which means safety. However, we...
Create a class named RemoveDuplicates and write code: a. for a method that returns a new...
Create a class named RemoveDuplicates and write code: a. for a method that returns a new ArrayList, which contains the nonduplicate elements from the original list public static ArrayList removeDuplicates(ArrayList list) b. for a sentinel-controlled loop to input a varying amount of integers into the original array (input ends when user enters 0) c. to output the original array that displays all integers entered d. to output the new array that displays the list with duplicates removed Use this TEST...
45. Which statement correctly passes the array items to method takeArray? Array items contains 10 elements....
45. Which statement correctly passes the array items to method takeArray? Array items contains 10 elements. a. takeArray(items[9]) b. takeArray(items[]) c. takeArray(items) d. Arrays cannot be passed to methods – each item must be sent to the method separately. 46. When an argument is passed by reference, ____________. a. a copy of the argument’s value is passed to the called method b. the original value is removed from memory c. changes to the argument do not affect the original variable’s...
IN JAVA Write a program with a method that returns an array. The method should accept...
IN JAVA Write a program with a method that returns an array. The method should accept as input a comma-delimited string with three values from a user. The array should store each value in a different element. Use Try..Catch error handling and print any failure messages, or print success from within method if the execution is successful (see Chapter 13 in the text). Call the method from the main method of the program to demonstrate its functionality by looping through...
Question 2. The following code defines an array size that sums elements of the defined array...
Question 2. The following code defines an array size that sums elements of the defined array through the loop. Analyze the following code, and demonstrate the type of error if found? What we can do to make this code function correctly? #include <stdio.h> #define A 10 int main(int argc, char** argv) { int Total = 0; int numbers[A]; for (int i=0; i < A; i++) numbers[i] = i+1; int i =0; while (i<=A){    Total += numbers[i];    i++; }...
Write a Java method that returns the index of the largest element in an array of...
Write a Java method that returns the index of the largest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header: 
 public static int indexOfLargestElement(double[] array)
 Write a test program that prompts the user to enter ten numbers, invokes this
method to return the index of the largest element, and displays the index.
Complete the class code below and include a method named partition which takes an integer array...
Complete the class code below and include a method named partition which takes an integer array and a partition integer as parameters and then partitions the original array into two new arrays where elements of the first array are all less than or equal to the partition integer and the second array are all greater than the partition integer. The method should print the original array, followed by the lower partition and finally the upper partition. Nothing should be returned...
Using Java Write a method that returns the index of the smallest element in an array...
Using Java Write a method that returns the index of the smallest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header:   public static int indexOfSmallestElement (double[] array)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT