Question

cant understand why my toString method wont work... public class Matrix { private int[][] matrix; /**...

cant understand why my toString method wont work... public class Matrix { private int[][] matrix; /** * default constructor -- * Creates an matrix that has 2 rows and 2 columns */ public Matrix(int [][] m) { boolean valid = true; for(int r = 1; r < m.length && valid; r++) { if(m[r].length != m[0].length) valid = false; } if(valid) matrix = m; else matrix = null; } public String toString() { String output = "[ "; for (int i = 0; i < matrix.length; i++) { output += matrix[i]; if (i != matrix.length - 1) { output += ", "; } } return output + " ]"; } public Matrix add(Matrix om) { Matrix newMatrix = null; if(om.matrix[0].length == this.matrix[0].length) { newMatrix = new Matrix(new int[matrix.length][matrix.length]); for (int i = 0; i < om.matrix[0].length; i++) for (int j = 0; j < om.matrix[0].length; j++) newMatrix.matrix[i][j] = om.matrix[i][j] + this.matrix[i][j]; } return newMatrix; } public Matrix sub(Matrix om) { { Matrix newMatrix = null; if(om.matrix[0].length == this.matrix[0].length) { newMatrix.matrix = new int[matrix.length][matrix.length]; for (int i = 0; i < om.matrix[0].length; i++) for (int j = 0; j < om.matrix[0].length; j++) newMatrix.matrix[i][j] = om.matrix[i][j] - this.matrix[i][j]; } return newMatrix; } } public Matrix multi(Matrix om) { return null; } public Matrix scalar(Matrix om) { return null; } public boolean equal(Matrix om) { return false; } public Matrix transpose(Matrix om) { return null; } class Dimension{ private int row, column; public Dimension(int[][] a){ row = a.length; column = a[0].length; } public int getRow() { return row; } public int getColumn() { return column; } public boolean equals(Dimension d) { return d.row == this.row && d.column == this.column; } } in App class public class Lab1App { public static void main(String[] args) { Matrix a = new Matrix(new int [][] {{1,2},{5,3}}); Matrix b = new Matrix(new int [][] {{3,2},{4,5}}); System.out.println(a.add(b)); } }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Matrix.java

public class Matrix {

   private int[][] matrix;

   /**

   * * default constructor -- * Creates an matrix that has 2 rows and 2

   * columns

   */

   public Matrix(int[][] m) {

       boolean valid = true;

       for (int r = 1; r < m.length && valid; r++) {

           if (m[r].length != m[0].length)

               valid = false;

       }

       if (valid)

           matrix = m;

       else

           matrix = null;

   }

   public String toString() {

       String output = "[ ";

       for (int i = 0; i < matrix.length; i++) {

  

          

           for (int j = 0; j < matrix.length; j++) {

               output += matrix[i][j];

               if(i==matrix.length-1 && j==matrix.length-1)

                   continue;

                   output += " , ";

              

              

           }

          

       }

       return output + " ]";

   }

   public Matrix add(Matrix om)

   {

       Matrix newMatrix = null;

       if (om.matrix[0].length == this.matrix[0].length) {

           newMatrix = new Matrix(new int[matrix.length][matrix.length]);

           for (int i = 0; i < om.matrix[0].length; i++)

               for (int j = 0; j < om.matrix[0].length; j++)

                   newMatrix.matrix[i][j] = om.matrix[i][j] + this.matrix[i][j];

       }

       return newMatrix;

   }

   public Matrix sub(Matrix om) {

       {

           Matrix newMatrix = null;

           if (om.matrix[0].length == this.matrix[0].length) {

               newMatrix.matrix = new int[matrix.length][matrix.length];

               for (int i = 0; i < om.matrix[0].length; i++)

                   for (int j = 0; j < om.matrix[0].length; j++)

                       newMatrix.matrix[i][j] = om.matrix[i][j] - this.matrix[i][j];

           }

           return newMatrix;

       }

   }

   public Matrix multi(Matrix om) {

       return null;

   }

   public Matrix scalar(Matrix om) {

       return null;

   }

   public boolean equal(Matrix om) {

       return false;

   }

   public Matrix transpose(Matrix om) {

       return null;

   }

   class Dimension {

       private int row, column;

       public Dimension(int[][] a) {

           row = a.length;

           column = a[0].length;

       }

       public int getRow() {

           return row;

       }

       public int getColumn() {

           return column;

       }

       public boolean equals(Dimension d) {

           return d.row == this.row && d.column == this.column;

       }

   }

}


============================================================

public class Lab1App

{

   public static void main(String[] args) {

       Matrix a = new Matrix(new int[][] { { 1, 2 }, { 5, 3 } });

       Matrix b = new Matrix(new int[][] { { 3, 2 }, { 4, 5 } });

       System.out.println(a.add(b));

   }

}


===========================================================
See Output : I have corrected toString method
1 2 public class Lab1App <terminated> Lab1App [Java A C4, 4 ,9,8 ] public static void main(StringC] args)t Matrix a -new Matr






valid false; 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 if (valid) matrix m; else matrixnull; public String toS
Thanks, let me know if there is any concerns

Add a comment
Know the answer?
Add Answer to:
cant understand why my toString method wont work... public class Matrix { private int[][] matrix; /**...
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
  • Hi, can someone please help me implement the transpose, multiply(Matrix b), multiply(Matrix m, int threads), and...

    Hi, can someone please help me implement the transpose, multiply(Matrix b), multiply(Matrix m, int threads), and equals(Object in) methods? (This is Java) I really need help so please don't just refund the question. public class Matrix {1 public int[] [] matrix; 1 int[] [] trans; 1 public int x, y; 1 private boolean transposed; 1 1 public Matrix(int x, int y){1 matrix = new int[x][y1;1 this.x = x; this.y = y; 1 } 1 9 /*1 * This method takes...

  • Chapter 8 Exercise 36, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY. 8.36 (Latin square)...

    Chapter 8 Exercise 36, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY. 8.36 (Latin square) A Latin square is an n-by-n array filled with n different Latin letters, each occurring exactly once in each row and once in each column. Write a program that prompts the user to enter the number n and the array of characters, as shown in the sample output, and checks if the input array is a Latin square. The characters are the first n...

  • Please use my code to implement the above instructions. My Grid class: import java.util.ArrayList; import java.util.Collections;...

    Please use my code to implement the above instructions. My Grid class: import java.util.ArrayList; import java.util.Collections; class Grid { private boolean bombGrid[][]; private int countGrid[][]; private int numRows; private int numColumns; private int numBombs; public Grid() { this(10, 10, 25); }    public Grid(int rows, int columns) { this(rows, columns, 25); }    public Grid(int rows, int columns, int numBombs) { this.numRows = rows; this.numColumns = columns; this.numBombs = numBombs; createBombGrid(); createCountGrid(); }    public int getNumRows() { return numRows;...

  • import java.util.*; public class Movie {    private String movieName; private int numMinutes; private boolean isKidFriendly;...

    import java.util.*; public class Movie {    private String movieName; private int numMinutes; private boolean isKidFriendly; private int numCastMembers; private String[] castMembers;    // default constructor public Movie() { movieName = "Flick"; numMinutes = 0; isKidFriendly = false; numCastMembers = 0; castMembers = new String[10]; }    // overloaded parameterized constructor public Movie(String movieName, int numMinutes, boolean isKidFriendly, String[] castMembers) { this.movieName = movieName; this.numMinutes = numMinutes; this.isKidFriendly = isKidFriendly; numCastMembers = castMembers.length; this.castMembers = new String[numCastMembers];    for(int i=0;i<castMembers.length;i++)...

  • /** * A collection of methods related to multi-dimensional arrays. */ public class Array2Lab { /**...

    /** * A collection of methods related to multi-dimensional arrays. */ public class Array2Lab { /** * Return whether k is in list. * Precondition: the elements of list are not null. * @param list the array to be searched. * @param k the number to search for. * @return true if k is an element of list, and false otherwise. */ public static boolean contains(Object[][] list, Object k) { return false; }    /** * Create a String that...

  • import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; public class FindWordInMaze { private char grid[][]; private...

    import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; public class FindWordInMaze { private char grid[][]; private ArrayList<String> words; private HashSet<String> foundWords = new HashSet<String>(); public FindWordInMaze(char[][] grid) { this.grid = grid; this.words = new ArrayList<>();    // add dictionary words words.add("START"); words.add("NOTE"); words.add("SAND"); words.add("STONED");    Collections.sort(words); } public void findWords() { for(int i=0; i<grid.length; i++) { for(int j=0; j<grid[i].length; j++) { findWordsFromLocation(i, j); } }    for(String w: foundWords) { System.out.println(w); } } private boolean isValidIndex(int i, int j, boolean visited[][]) { return...

  • Override toString() for the class HighScores. It should return a String that contains all of the...

    Override toString() for the class HighScores. It should return a String that contains all of the high scores in entries, formatted nicely. Note that toString is already implemented for the Score class. Then, write a driver program (in a file named Main.java) that thoroughly tests HighScores (i.e. add and remove scores from the list, add scores to an already full list, make sure the list remains sorted, etc). Score.java public class Score { protected String name; // name of the...

  • I have a little problem about my code. when i'm working on "toString method" in "Course"...

    I have a little problem about my code. when i'm working on "toString method" in "Course" class, it always say there is a mistake, but i can not fix it: Student class: public class Student {    private String firstName;    private String lastName;    private String id;    private boolean tuitionPaid;       public Student(String firstName, String lastName, String id, boolean tuitionPaid)    {        this.firstName=firstName;        this.lastName=lastName;        this.id=id;        this.tuitionPaid=tuitionPaid;    }   ...

  • Java 1. Write a getCount method in the IntArrayWorker class that returns the count of the...

    Java 1. Write a getCount method in the IntArrayWorker class that returns the count of the number of times a passed integer value is found in the matrix. There is already a method to test this in IntArrayWorkerTester. Just uncomment the method testGetCount() and the call to it in the main method of IntArrayWorkerTester. 2. Write a getLargest method in the IntArrayWorker class that returns the largest value in the matrix. There is already a method to test this in...

  • I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][]...

    I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][] t) A method that returns true if from left to right in any row, the integers are increasing, otherwise false. - columnValuesIncrease(int[][] t) A method that returns true if from top to bottom in any column, the integers are increasing, otherwise false. - isSetOf1toN(int[][] t) A method that returns true if the set of integers used is {1, 2, . . . , n}...

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