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
Write a phython program to display a menu with the following options: (1) add, (2) subtract,...
Write a phython program to display a menu with the following options: (1) add, (2) subtract, (3) multiply, (4) divide (5) mod, and (6) do nothing. If the user enters something else (except 1-6), the program should display an error message. Otherwise, it should ask the user for two numbers, perform the calculation, and display the result.
Write a Java program to determine the larger of 2 numbers and the largest of 3...
Write a Java program to determine the larger of 2 numbers and the largest of 3 numbers. Each section must use its own method. Method overloading must be used for this exercise. [10] Method for 2 numbers public static double max(double p, double q) Method for 3 numbers public static double max(double p, double q, double r)
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...
Please make a Java program that has a menu method and includes dialog boxes. The program...
Please make a Java program that has a menu method and includes dialog boxes. The program will call the proper method, but each method will be completed later. Your program will keep track of information for a Hospital. The program will be menu-driven. Your menu is to look something like the following: UPMC Medical Center Add/Modify Patient Information Add/Modify Physician Information Add/Modify Medical Information Hospital Report Section Exit the Medical System Please Make your selection > In this first phase,...
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);...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT