Question

Write a java 3x3 matrix program that does the following: 1) multiply 2)add 3)subtract 4)scalar product...

Write a java 3x3 matrix program that does the following:

1) multiply

2)add

3)subtract

4)scalar product

5)does calculation for a^3 - b^3

6)does calculation for 2a^ + 3b^2

an example for the adding method will look something like this

public static double[][] multiplyMatrix(double [][] a, double [][] b)

Homework Answers

Answer #1

Code -

import java.util.Scanner;

public class Main
{ public static int row = 3;
public static int cols = 3;
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
  

int[][] matrix1 = {{1,2,3},{4,5,6},{7,8,9}};

int[][] matrix2 = {{4,24,8},{3,4,8},{5,3,3}};


System.out.println("First Matrix = ");

for (int i = 0; i < row; i++)
{
for (int j = 0; j < cols; j++)
{
System.out.print(matrix1[i][j]+"\t");
}

System.out.println();
}

System.out.println("Second Matrix = ");

for (int i = 0; i < row; i++)
{
for (int j = 0; j < cols; j++)
{
System.out.print(matrix2[i][j]+"\t");
}

System.out.println();
}
int [][]sum = addMatrix(matrix1,matrix2);
System.out.println("Addition ");
displayOutput(sum);
int [][]sub = subMatrix(matrix1,matrix2);
System.out.println("Subtraction ");
displayOutput(sub);
int [][]mult =multiplyMatrix(matrix1,matrix2);
System.out.println("Multiplication ");
displayOutput(mult);
scalarProductMat(matrix1,5);
int [][]aSquare = multiplyMatrix(matrix1,matrix2);
int [][]aCube = multiplyMatrix(aSquare,matrix1);
int [][]bSquare = multiplyMatrix(matrix2,matrix2);
int [][]bCube = multiplyMatrix(bSquare,matrix2);
int [][]aCubeMinuBcube = subMatrix(aCube,bCube);
System.out.println("a^3 - b^3 = ");
displayOutput(aCubeMinuBcube);
  

}
  
public static void displayOutput(int a[][] ){
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
System.out.print(a[i][j]+"\t");
}

System.out.println();
}
}
public static int [][] addMatrix(int [][] matrix1, int [][] matrix2){
  
int[][] sum = new int[3][3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
sum[i][j] = matrix1[i][j] + matrix2[i][j];

// System.out.print(sum[i][j]+"\t");
}

//System.out.println();
}
return sum;
}
public static int [][] subMatrix(int [][] matrix1, int [][] matrix2){
  

int[][] sum = new int[3][3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
sum[i][j] = matrix1[i][j] - matrix2[i][j];

// System.out.print(sum[i][j]+"\t");
}

//System.out.println();
}
return sum;
}
public static int [][] multiplyMatrix(int [][] a, int [][] b){
int multi[][]=new int[3][3]; //3 rows and 3 columns
  
//multiplying and printing multiplication of 2 matrices
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
multi[i][j]=0;
for(int k=0;k<3;k++)
{
multi[i][j]+=a[i][k]*b[k][j];
}//end of k loop
//System.out.print(multi[i][j]+"\t"); //printing matrix element
}//end of j loop
//System.out.println();//new line
}
return multi;
}
static void scalarProductMat(int a[][],
int k)
{
int mat[][] = a;
System.out.println("Scaler product by "+k+" is ");
// scalar element is multiplied
// by the matrix
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
mat[i][j] = mat[i][j] * k;
  
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{

System.out.print(mat[i][j]+"\t");
}

System.out.println();
}
}
}

Screenshots -

Know the answer?
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
Similar Questions
IN JAVA Complete the following program. Add codes in the main method to:1) call method average(double[]...
IN JAVA Complete the following program. Add codes in the main method to:1) call method average(double[] gpaarr ) to calculate the average of gpaArray; 2) add codes in the for loop to calculate sum 3) print the average . public class Test { public static void main (String args[]) { double[ ] gpaArray = { 2.5, 4.0, 3.8, 3.2, 2.9, 4.0 } ;   //add your codes here (you can put your codes in the Answer area with/without copying the original...
For Java programming code: Write methods to add and multiply 1) Two integers 2) Two decimals...
For Java programming code: Write methods to add and multiply 1) Two integers 2) Two decimals 3) One integer and one decimal
4) Write a Java program using Conditions: Write a program where it will ask user to...
4) Write a Java program using Conditions: Write a program where it will ask user to enter a number and after that it will give you answer how many digits that number has. Steps: 1) Create Scanner object and prompt user to enter the number and declare variable integer for input 2) Use if else condition with && condition where you will specify the digits of numbers by writing yourself the digit number that should display: Example(num<100 && num>=1), and...
Language: JAVA(Netbeans) Write a generic class MyMathClass with at type parameter T where T is a...
Language: JAVA(Netbeans) Write a generic class MyMathClass with at type parameter T where T is a numeric object (Integer, Double or any class that extends java.lang.number) Add a method standardDeviation (stdev) that takes an ArrayList of type T and returns a standard deviation as type double. Use a for each loop where appropriate. Hard code a couple of test arrays into your Demo file. You must use at least 2 different types such as Double and Integer. Your call will...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
In Java. Write a program that reads-in a times table-number. The program, using this table-number will...
In Java. Write a program that reads-in a times table-number. The program, using this table-number will produce the following report (as shown). The first item in the report is a number with starting value 1. Second column is the word “ X ” (representing the times symbol). Third column is the table-number (itself). Following is an equal sign “ = “ (representing a result). Last column is the result of the operation for that line or row which is the...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...
Write a function (java static method) that computes the maximum of two integer. maxOfTwo(1, 2) →...
Write a function (java static method) that computes the maximum of two integer. maxOfTwo(1, 2) → 2 maxOfTwo(2, 1) → 2 maxOfTwo(1, 1) → 1 **(to start use): public int maxOfTwo(int a, int b) { And then complete the function (java static method)MaxOfThree so that it returns the maximum (highest) of the three int values passed as the parameters. maxOfThree(1, 2, 3) → 3 maxOfThree(0, 2, 1) → 2 maxOfThree(88, 4, 5) → 88 **(to start use): public int maxOfThree(int...
WRITE A JAVA PROGRAM: The last digit of a credit card number is the check digit,...
WRITE A JAVA PROGRAM: The last digit of a credit card number is the check digit, which protects against transaction errors. The following method is used to veryfy credit card numbers. For the simplicity we can assume that the credit card has 8 digits instead of 16. Following steps explains the algorithm in determining if a credit card number is a valid card.  Starting from the right most digit, form the sum of every other digit. For example, if...
What is the output of the following Java program? public class Food {     static int...
What is the output of the following Java program? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { s = flavor; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         pepper.setFlavor("spicy");         System.out.println(pepper.getFlavor());     } } Select one: a. sweet b. 1 c. The program does not compile. d. 2 e. spicy...