COE 2SH4程序 写作、 辅导java语言编程

” COE 2SH4程序 写作、 辅导java语言编程McMaster UniversityDept. Electrical and Comp. Engineering COE 2SH4 – Fall 2020LAB 4 – Java ClassesAssessment: 7% of the total course mark.1 General Instructions Your programs should be written in a good programming style, including instructivecomments and well-formatted, well-indented code. Use self-explanatory names for variablesas much as possible. ( 5% of the mark) You have to make sure you pass all the tests. Please note that passing the tests doesnot grant you Automatically the full mark, we run other hidden test cases that are notshared with you to Further assess your code. You are required to make sure your work iscorrectly implemented to the best of your knowledge. One task that can help you withthat is to add further tests to stress the corner cases of each question. For each method, you are required to add at least one additional test to the matrexTestAll.javatest class.2 Submission DeadlineThe deadline for lab5 is Nov 20th. Please note that this is a programmed deadline in theenvironment, so make sure you submit in time since you will not be able to submit after thatdictated deadline.3 Environment Setup3.1 Add Java JDK to System PathPlease follow the steps in the document named Installing Java JDK.3.2 Download and Run Eclipse IDE for JavaPlease go to httpss://www.eclipse.org/downloads/packages/ and download Eclipse IDEfor Java Developers the version that corresponds to your operating system.3.3 Importing the starter code of the labYou should follow exactly the same process you have been following in past labs to importthe starter code from the following invitation link and to create the project: httpss://classroom.github.com/a/N16Z3W641McMaster UniversityDept. Electrical and Comp. Engineering COE 2SH4 – Fall 2020Importing JUNIT into your project. We will be using the JUNIT to create and runtest cases. JUNIT comes already shipped with Eclipse for Java. So, you do not need to installanything. However, you should perform the following step to include the JUNIT library intothe project to be able to run the test cases:Open one of the Project files by double clicking it, then: selectFile-New-JUnit Test CaseThis will create another class for testing. You need to delete this file since we alreadyhave our own test cases in the UpperTriangularMatrix.java class. However, this step alsowill include the JUNIT (probably JUnit 5) to the project, which is what we need.You can read about the Java JUnit here: httpss://courses.cs.washington.edu/courses/cse143/11wi/eclipse-tutorial/junit.shtml4 Lab QuestionsWrite two Java classes: Matrix, which represents matrices with integer elements, and UpperTriangularMatrix to represent upper triangular matrices with integer elementsstored efficiently.The accompanying files Matrix.java and UpperTriangularMatrix in the provided startercode contains an incomplete declaration of the methods of both classes. You need to completethe declarations of incomplete methods or constructors according to the specifications givenbelow.Finally, a class Named UpperTriangularMatrix.java includes all the test cases. Youshould, as usual, write at least one additional test case for every method.4.1 Matrix Class Class Matrix has only the following instance fields, which have to be private:- an integer to store the number of rows- an integer to store the number of columns.- a two dimensional array of integers to store the matrix elements. Class Matrix Contains at least the following constructors:- public Matrix(int row, int col) – constructs a row-by-col matrix with allelements equal to 0; if row 0, the number of rows of the matrix is set to 3;likewise, if col 0 the number of columns of the matrix is set to 3.- public Matrix(int[][] table) – constructs a matrix out of the two dimensionalarray table, with the same number of rows, columns, and the same element ineach position as array table. Class Matrix contains at least the following methods:2McMaster UniversityDept. Electrical and Comp. Engineering COE 2SH4 – Fall 20201) public int getElement(int i, int j) throws IndexOutOfBoundsException -returns the element on row i and column j of this matrix; it throws an exceptionif any of indexes i and j is not in the required range (rows and columns indexingstarts with 0); the detail message of the exception should read: Invalid indexes.2) public boolean setElement(int x, int i, int j) – if i and j are valid indexesof this matrix, then the element on row i and column j of this matrixis assigned the value x and true is returned; otherwise false is returned and nochange in the matrix is performed.3) public Matrix copy() – returns a deep copy of this Matrix. Note: A deepcopy does not Share any piece of memory with the original. Thus, any changeperformed on the copy will not affect the original.4) public void addTo(Matrix m) throws ArithmeticException – adds Matrix mto this Matrix (note: this Matrix WILL BE CHANGED) ; it throws an exceptionif the matrix addition is not defined (i.e, if the matrices do not have the samedimensions); the detail message of the exception should read: Invalid operation.5) public Matrix subMatrix(int i, int j) throws ArithmeticException – returnsa new Matrix object, which represents a submatrix of this Matrix, formedout of rows 0 through i and columns 0 through j. The method should first checkif values i and j are Within the required range, and throw an exception if any ofthem is not. The Exception detail message should read: Submatrix not defined.Note: The new object should be constructed in such a way that changes in thenew matrix do not affect this Matrix.6) public boolean isUpperTr() – returns true if this Matrix is upper triangular,and false otherwise. A matrix is said to be upper triangular if all elements belowthe main diagonal are 0. Note that the main diagonal contains the elements situatedat positions Where the row index equals the column index. In the followingexamples the main diagonal contains elements 1,9,3.Example of a 3-by-3 upper triangular matrix:1 4 10 9 00 0 3Example of a 3-by-4 upper triangular matrix:1 5 1 40 9 6 60 0 3 8Example of a 4-by-3 upper triangular matrix:1 4 20 9 60 0 30 0 07) public static Matrix sum(Matrix[] matArray) throws ArithmeticExceptionreturnsa new matrix representing the sum of all matrices in matArray. The methodthrows an exception if the matrices do not have the same dimensions. This methodMUST USE method addTo() to perform the addition of two matrices.3McMaster UniversityDept. Electrical and Comp. Engineering COE 2SH4 – Fall 20208) public String toString() – returns a string representing the matrix, with eachrow on a Separate line, and the elements in a row being separated by 1 blank space.For instance like this:1 2 34 5 67 8 94.2 UpperTriangularMatrix Class An n-by-n matrix a is said to be upper triangular if all elements below the main diagonalare 0. Such a matrix can be represented efficiently by using only a one dimensional arrayof size n(n+1)/2, which stores the matrix elements row by row, skipping the zeros belowthe diagonal, i.e., in the following order: a(0,0), a(0,1), a(0,2), , a(0,n-1),a(1,1), a(1,2), , a(1,n-1), a(2,2), a(2,3), , a(2,n-1) a(n-1,n-1).The Java class UpperTriangularMatrix has to model square upper triangular matricesof integers, stored in efficient format as described above. Class UpperTriangularMatrixshould have two private instance variables: an integer to represent the matrix size (i.e.the number of rows n), and a one dimensional array to store the matrix elements inefficient format. Class UpperTriangularMatrix contains at least the following constructors:- public UpperTriangularMatrix(int n) – if n 0, changes n to 1; initializes theUpperTriangularMatrix object to represent the all-zero n-by-n matrix.- public UpperTriangularMatrix(Matrix upTriM) throws IllegalArgumentException- initializes the UpperTriangularMatrix object to represent the upper triangularmatrix upTriM. Note that upTriM is an object of the class Matrix that you have towrite for this assignment. The method throws an exception if upTriM is not uppertriangular. The exception detail message should read: Not an upper triangularmatrix To check if the upper triangular condition is satisfied you MUST USE themethod isUpperTr() of class Matrix. Class UpperTriangularMatrix contains at least the following instance methods:- public int getDim() – returns the number of rows of this matrix.- public int getElement(int i, int j) throws IndexOutOfBoundsException -returns the matrix element on row i and column j if i and j are valid indices ofthis matrix (indexing starts at 0); otherwise an IndexOutOfBoundsException isthrown, with message Invalid index.- public void setElement(int x, int i, int j) throws IndexOutOfBoundsException,IllegalArgumentException – if i and j are valid indexes of the matrix, then theelement on row i and column j of the matrix is assigned the value x; however, ifindexes i and j Correspond to a position in the lower part of the matrix and x is not0 then an IllegalArgumentException has to be thrown with message Incorrectargument; finally, if indexes i and j do not represent a valid position in the matrixthen an IndexOutOfBoundsException is thrown, with message Invalid index.4McMaster UniversityDept. Electrical and Comp. Engineering COE 2SH4 – Fall 2020- public Matrix fullMatrix() – returns a Matrix object corresponding to thisUpperTriangularMatrix. Note that the Matrix object will store the full matrixincluding all the zeros from the lower part.- public String toString() – returns a string representing this UpperTriangularMatrixobject. The representation should show all elements of the full matrix with eachrow on a separate line.- public int getDet() – returns the determinant of the matrix, which equals theproduct of the elements on the main diagonal.- public double[] effSolve(double[] b) throws IllegalArgumentException -This method solves the matrix equation Ax=b, where A is this UpperTriangularMatrix,if the determinant of A is non-zero. Otherwise it throws an exception, with messageThe determinant of the matrix is 0. The method returns array x. The methodhas to be efficient, which means that it has to use an efficient way to solve theequation and implement it without wasting time or memory resources, in otherwords, without allocating arrays (except for x) or invoking other methods. Partialmarks will be awarded For correct, but less efficient solutions. Note that themethod should also check if the dimension of b is appropriate and if not throwan exception, with the message: The dimension of the matrix is not defined foroperation.INSTRUCTIONS: You May implement public methods in class Matrix to return the number of rows andthe number of columns.如有需要,请加QQ:99515681 或邮箱:99515681@qq.com

添加老师微信回复‘’官网 辅导‘’获取专业老师帮助,或点击联系老师1对1在线指导