Question

In: Computer Science

package applications; public class Matrix { private int[][] m; public Matrix(int x, int y) { m...

package applications;

public class Matrix

{

private int[][] m;

public Matrix(int x, int y)

{

m = new int[x][y];

}

public Matrix(int x, int y, int z)

{

m = new int[x][y];

for(int i = 0; i < x; i++)

{

for(int j = 0; j < y; j++)

{

m[i][j] = z;

}

}

}

public int rowsum(int i) throws IndexOutOfBoundsException

{

if (i < 0 || i > m.length-1)

{

throw new IndexOutOfBoundsException("Invalid Row");

}

int sum = 0;

for(int j = 0; j < m[i].length; j++)

{

sum += m[i][j];

}

return sum;

}

public static void main(String[] args)

{

Matrix A = new Matrix(100, 100, 1);

System.out.println(A.rowsum(99));

System.out.println("Done!");

Matrix B = new Matrix(1000, 1000, 1);

System.out.println("Done!");

Matrix C = new Matrix(10000, 10000, 1);

System.out.println("Done!");

}

}

1)add method columnSum(j)

2)add method max() which returns the largest value that contain in the matrix

3)add method trace() i.e., if matrix is not square, then throw error otherwise compute the sum of the entries on the diagnol.

Solutions

Expert Solution

package applications;

public class Matrix {
    private int[][] m;

    public Matrix(int x, int y) {
        m = new int[x][y];
    }

    public Matrix(int x, int y, int z) {
        m = new int[x][y];
        for (int i = 0; i < x; i++) {
            for (int j = 0; j < y; j++) {
                m[i][j] = z;
            }
        }
    }

    public int rowsum(int i) throws IndexOutOfBoundsException {
        if (i < 0 || i > m.length - 1) {
            throw new IndexOutOfBoundsException("Invalid Row");
        }
        int sum = 0;
        for (int j = 0; j < m[i].length; j++) {
            sum += m[i][j];
        }
        return sum;
    }

    public int colsum(int i) throws IndexOutOfBoundsException {
        if (i < 0) {
            throw new IndexOutOfBoundsException("Invalid Row");
        }
        int sum = 0;
        for (int j = 0; j < m.length; j++) {
            if (i > m[j].length - 1) {
                throw new IndexOutOfBoundsException("Invalid Row");
            }
            sum += m[j][i];
        }
        return sum;
    }

    public int max() {
        int maxValue = m[0][0];
        for (int i = 0; i < m.length; i++) {
            for (int j = 0; j < m[i].length; j++){
                if (m[i][j] > maxValue) {
                    maxValue = m[i][j];
                }
            }
        }
        return maxValue;
    }

    public int trace() {
        int sum = 0;
        for (int i = 0; i < m.length; i++) {
            if (m[i].length != m.length) {
                throw new IllegalStateException("Matrix is not square");
            }
            sum += m[i][i];
        }
        return sum;
    }

    public static void main(String[] args) {
        Matrix A = new Matrix(100, 100, 1);
        System.out.println(A.rowsum(99));
        System.out.println("Done!");
        System.out.println(A.colsum(10));
        System.out.println("Done!");
        System.out.println("Max: " + A.max());
        System.out.println("Done!");
        System.out.println("Trace: " + A.trace());
        System.out.println("Done!");
        Matrix B = new Matrix(1000, 1000, 1);
        System.out.println("Done!");
        Matrix C = new Matrix(10000, 10000, 1);
        System.out.println("Done!");
    }
}

Related Solutions

package dealership; public abstract class Vehicle { private float dealerPrice; private int year; public Vehicle(float d,...
package dealership; public abstract class Vehicle { private float dealerPrice; private int year; public Vehicle(float d, int y) { dealerPrice = d; year = y; } public float getDealerPrice() { return dealerPrice; } public int getYear() { return year; } public abstract float getStickerPrice(); } In the space below write a concrete class Car in the dealership package that is a subclass of Vehicle class given above. You will make two constructors, both of which must call the superclass constructor....
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int,...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int, int); }; class line{ private: point ps; point pe; public: void print()const; void setf(int, int, int, int); }; class rectangle{ private: line length[2]; line breadth[2]; public: void print()const; void setf(int, int, int, int, int, int, int, int); }; int main(){ rectangle r1; r1.setf(3,4,5,6, 7, 8, 9, 10); r1.print(); system("pause"); return 0; } a. Write function implementation of rectangle, line and point. b. What is...
public class SinglyLikedList {    private class Node{        public int item;        public...
public class SinglyLikedList {    private class Node{        public int item;        public Node next;        public Node(int item, Node next) {            this.item = item;            this.next = next;        }    }       private Node first;    public void addFirst(int a) {        first = new Node(a, first);    } } 1. Write the method add(int item, int position), which takes an item and a position, and...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y;...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y; public MyPoint() { this(0, 0); } public MyPoint(double x, double y) { this.x = x; this.y = y; } // Returns the distance between this MyPoint and other public double distance(MyPoint other) { return Math.sqrt(Math.pow(other.x - x, 2) + Math.pow(other.y - y, 2)); } // Determines if this MyPoint is equivalent to another MyPoint public boolean equals(MyPoint other) { return this.x == other.x &&...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y;...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y; public MyPoint() { this(0, 0); } public MyPoint(double x, double y) { this.x = x; this.y = y; } // Returns the distance between this MyPoint and other public double distance(MyPoint other) { return Math.sqrt(Math.pow(other.x - x, 2) + Math.pow(other.y - y, 2)); } // Determines if this MyPoint is equivalent to another MyPoint public boolean equals(MyPoint other) { return this.x == other.x &&...
1. Consider the following code: public class Widget implements Serializable { private int x; public void...
1. Consider the following code: public class Widget implements Serializable { private int x; public void setX( int d ) { x = d; } public int getX() { return x; } writeObject( Object o ) { o.writeInt(x); } } Which of the following statements is true? I. The Widget class is not serializable because no constructor is defined. II. The Widget class is not serializable because the implementation of writeObject() is not needed. III. The code will not compile...
public class Date { private int dMonth; //variable to store the month private int dDay; //variable...
public class Date { private int dMonth; //variable to store the month private int dDay; //variable to store the day private int dYear; //variable to store the year //Default constructor //Data members dMonth, dDay, and dYear are set to //the default values //Postcondition: dMonth = 1; dDay = 1; dYear = 1900; public Date() { dMonth = 1; dDay = 1; dYear = 1900; } //Constructor to set the date //Data members dMonth, dDay, and dYear are set //according to...
COMPLETE JAVA CODE public class Point2 { private double x; private double y;    /** *...
COMPLETE JAVA CODE public class Point2 { private double x; private double y;    /** * Create a point with coordinates <code>(0, 0)</code>. */ public Point2() { complete JAVA code this.set(0.0, 0.0); COMPLETE CODE }    /** * Create a point with coordinates <code>(newX, newY)</code>. * * @param newX the x-coordinate of the point * @param newY the y-coordinate of the point */ public Point2(double newX, double newY) { complete Java code this.set(newX, newY); }    /** * Create a...
public class P2 { public static int F(int x[], int c) { if (c < 3)...
public class P2 { public static int F(int x[], int c) { if (c < 3) return 0; return x[c - 1] + F(x, c - 1); } public static int G(int a, int b) { b = b - a; a = b + a; return a; } public static void main(String args[]) { int a = 4, b = 1; int x[] = { 3, 1, 4, 1, 5 }; String s = "Problem Number 2"; System.out.println(x[2 +...
class A { public: //constructors // other members private: int a; int b; }; Give declatations...
class A { public: //constructors // other members private: int a; int b; }; Give declatations of operator functions for each of the following ways to overload operator + You must state where the declatation goes, whether within the class in the public or private section or outside the class. The operator + may be overloaded. a) as friend function b) as member function c) as non-friend, non-member function
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT