* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Midterm 2
Survey
Document related concepts
Linear least squares (mathematics) wikipedia , lookup
System of linear equations wikipedia , lookup
Eigenvalues and eigenvectors wikipedia , lookup
Rotation matrix wikipedia , lookup
Determinant wikipedia , lookup
Jordan normal form wikipedia , lookup
Four-vector wikipedia , lookup
Principal component analysis wikipedia , lookup
Matrix (mathematics) wikipedia , lookup
Singular-value decomposition wikipedia , lookup
Perron–Frobenius theorem wikipedia , lookup
Orthogonal matrix wikipedia , lookup
Non-negative matrix factorization wikipedia , lookup
Cayley–Hamilton theorem wikipedia , lookup
Matrix calculus wikipedia , lookup
Transcript
Name : ..................................................................... Comp 240 Midterm 2 ID # : ..................................................................... May 16, 2005 1/6 State clearly all of your assumptions. Good luck. Q1 (40 Q2 (10) Q3(50) Sum Q1. (40 pt) 1.a. (4 pt) (Check all that apply.) Private fields of a superclass can be accessed in a subclass by calling private methods declared in the superclass. by calling public or protected methods declared in the superclass. directly. 1.b. (4 pt) (Check all that apply.) The term information hiding refers to: public methods. hiding implementation details from clients of a class. accessing static class members. the process of releasing an object for garbage collection. 1.c. (4 pt) Indicate true or false for the following statements: _ _ _ _ Every element in an array has the same type. The array size is fixed after it is declared. The array size is fixed after it is created. The element in the array must be of primitive data type. 1.d. (4 pt) (Check all that apply.) Which statement is true when a superclass uses protected instance variables? A subclass object can assign an invalid value to the superclass’s instance variables, thus leaving the object in an inconsistent state. Subclass methods are more likely to be written so that they depend on the superclass’s data implementation. We may need to modify all the subclasses of the superclass if the superclass implementation changes. 1.e. (4 pt) (Check all that apply.) Every class in Java, except ________, extends an existing class. Integer. Object. String. Class. Name : ..................................................................... Comp 240 Midterm 2 ID # : ..................................................................... May 16, 2005 2/6 1.f. (4 pt) (Check all that apply.) To avoid duplicating code (and possibly errors), use ________, rather than ________. inheritance, the “copy-and-paste” approach. the “copy-and-paste” approach, inheritance. a class that explicitly extends Object, a class that does not extend Object. a class that does not extend Object, a class that explicitly extends Object. 1.g. (4 pt) (Check all that apply.) Which of the following is an example of a functionality that should be “factored out” to a superclass? Both ducks and geese are birds that know how to start flying from the water. All vehicles know how to start and stop. All animals lay eggs, except for mammals. All paints have a color. 1.h. (4 pt) Indicate true or false for the following statements: _ _ _ _ 1.i. A static method must be used to access private static instance variables. A static method has no this reference. A static method can be accessed even when no objects of its class have been instantiated. A static method can call instance methods directly. (4 pt) Constructors: Initialize instance variables. When overloaded, can have identical argument lists. When overloaded, are selected by number and types of parameters. 1.j. (4 pt) Set and Get methods are necessary to access the data stored in a _________ instance field for the client of that class. public. private. protected. Name : ..................................................................... Comp 240 Midterm 2 ID # : ..................................................................... May 16, 2005 3/6 Q2. (10 pt) 2.a. (5 pt) What is the output of the following application? public class Test { public static void main(String[] args) { int number = 0; int numbers[] = new int[1]; m(number, numbers); System.out.println("numbers is " + number + " and numbers[0] is " + numbers[0]); } public static void m(int x, int y[]) { x = 3; y[0] = 3; } } 2.b. (5 pt) What is the output of the following code segment? int array[][] = new int [5][6]; int x[] = {1, 2}; array[0] = x; System.out.println("array[0][1] is " + array[0][1]); System.out.println("array[1][1] is " + array[1][1]); Name : ..................................................................... Comp 240 Midterm 2 ID # : ..................................................................... May 16, 2005 4/6 Q3. Programming (50 pt) Warning: You will NOT receive any partial credit if your code does not compile correctly. Read the whole question before starting to solve the problem. Write a class to perform matrix calculations. You will need to perform matrix addition and multiplication operations that are defined below: Addition: Let A and B be two n-by-m matrices. (i.e., n rows by m columns) Then the addition of A and B is an n-by-m matrix C A B where cij aij bij , 1 i n, 1 j m . Multiplication: Let A be an n-by-m matrix, and B be an m-by-p matrix. Then the multiplication of A and B is an n-by-p matrix m C A B where cij aikbkj , 1 i n, 1 j p . k 1 3.a. Matrix class (8 pt) Create a class named “Matrix.java” which will hold the methods that describe the functionality of the matrix operations. You will need to create at least the following instance variables: An integer variable “row” to hold the number of rows in the matrix. The value for this variable should be assigned whenever matrix is created. An integer variable “col” to hold the number of columns in the matrix. The value for this variable should be assigned whenever matrix is created. An array (possibly multidimensional) of double precision floating point numbers to the elements of the matrix. Comments: (do not fill this space) 3.b. Constructors (8 pt) The matrix can be created in two ways. Only the row and column counts could be given, and the corresponding empty matrix is created. Together with the row and column counts, the list of elements of the matrix could be given in a comma-separated list. Then, the matrix containing this list should be created. You should create two different constructors accordingly. Note here, you should check the validity of row and column values (They should be greater than zero). Similarly, you should also check the length of the list. If the number of elements does not match with the size of the matrix, then do not use the list and leave the elements zero. Comments: (do not fill this space) Name : ..................................................................... Comp 240 Midterm 2 ID # : ..................................................................... May 16, 2005 5/6 3.c. setElements method (8 pt) Create a method that sets the values of the elements in the matrix. A list of elements of the matrix should be given in a comma-separated list. You can also use this method in the constructor. Comments: (do not fill this space) 3.d. get and set methods (4 pt) Create corresponding get and set methods for the instance fields row and col. Comments: (do not fill this space) 3.e. add method (8 pt) Create a method that adds two matrices that are passed as arguments and returns a new matrix containing the addition result. If the sizes do not match for a valid matrix addition, then it returns “null” to indicate an invalid multiplication. Comments: (do not fill this space) 3.f. multiply method (8 pt) Create a method that multiplies two matrices that are passed as arguments and returns a new matrix containing the multiplication result. If the sizes do not match for a valid matrix multiplication, then it returns “null” to indicate an invalid multiplication. Comments: (do not fill this space) 3.g. toString method (6 pt) Create a method that returns the matrix elements as a string in a tabular format. Use only two digits after the decimal point. Comments: (do not fill this space) Name : ..................................................................... Comp 240 Midterm 2 ID # : ..................................................................... May 16, 2005 6/6 3.h. MatrixTest class (0 pt) Create the application class named “MatrixTest.java” that is used to test the Matrix class. The following code will perform such a test. 1 1 2 3 14 , B 2 , A B null, A B A 4 5 6 32 3 6 3 9 C , D , C D , C D null 7 4 11 public class MatrixTest { public static Matrix a Matrix b Matrix c Matrix d void main(String[] args) { = new Matrix( 2, 3, 1, 2, 3, 4, 5, 6 ); = new Matrix( 3, 1, 1, 2, 3 ); = new Matrix( 2, 1, 6, 7 ); = new Matrix( 2, 1 ); System.out.println( "A = \n" + a ); System.out.println( "A + B = \n" + Matrix.add(a, b) ); System.out.println( "A * B = \n" + Matrix.multiply(a, b) ); System.out.println( "C + D = \n" + Matrix.add(c, d) ); System.out.println( "C * D = \n" + Matrix.multiply(c, d) ); d.setElements( 3, 4 ); System.out.println( "C + D = \n" + Matrix.add(c, d) ); System.out.println( "C * D = \n" + Matrix.multiply(c, d) ); } } Sample output: A = 1,00 4,00 A + B = null A * B = 14,00 32,00 C + D = 6,00 7,00 C * D = null C + D = 9,00 11,00 C * D = null 2,00 5,00 3,00 6,00