Lab 2 Instruction (3+1 marks)
Lab 2 Instruction (3+1 marks)
Question 1 (2 marks)
A 2x2 square matrix is a matrix with two rows and two columns. For example, M1, M2
and M3 are 2x2 matrices.
M 1=
[13 24 ] M =[3311 4422] M =[ 0.5
2 3
0.9 −1 ]
−0.3 .
1. Determinant
2. Matrix addition
A=
[ ac bd ] B=[ eg hf ],
the result of adding these two matrices is the following matrix
[ a+c+ ge b+ f
d+h ]
.
1
THE HANG SENG UNIVERSITY OF HONG KONG
DEPARTMENT OF COMPUTER SCIENCE
COM1102 Lab 2
3. Matrix multiplication
Again, given the following matrices
A=
[ ] [ ]
a b
c d
B=
e f ,
g h
[ ae+ bg
ce +dg
af +bh
cf + dh].
Complete class Matrix (file name: Matrix.java) for representing 2x2 matrices, with
the following attributes and methods.
1. Attributes
Define the required attributes for holding the elements of a matrix (i.e., the four
numbers). You are free to decide how the numbers are to be stored.
2. Constructor
Add a constructor method with four double arguments that represent the
four initial elements of the matrix. Use the arguments to initialize the attribute
values. The two values for the first row are followed by those for the second
row.
For example, the following code creates an object that corresponds to the
matrix M 1= [ ]
1 2
3 4
:
3. Add a toString() method in the class. It takes no arguments and returns a String
representation of the matrix.
System.out.println(M1.toString());
2
THE HANG SENG UNIVERSITY OF HONG KONG
DEPARTMENT OF COMPUTER SCIENCE
COM1102 Lab 2
3 4
3
THE HANG SENG UNIVERSITY OF HONG KONG
DEPARTMENT OF COMPUTER SCIENCE
COM1102 Lab 2
System.out.println(M1);
4. Add an add() method in the class. It takes one argument which is another
Matrix, and returns the addition result of the two matrices.
or in Java code
Matrix M1 = new Matrix(1,2,3,4); and
[
12 24
36 48
. ]
5. Add a mult() method in the class. It takes one argument, which is another
Matrix, and returns the result of the first object multiplied by its argument.
[ 1∗0.5+2∗0.9
3∗0.5+4∗0.9
1∗−0.3+ 2∗−1
3∗−0.3+ 4∗−1 ]
¿
[2.3
5.1
−2.3
−4.9
.]
4
THE HANG SENG UNIVERSITY OF HONG KONG
DEPARTMENT OF COMPUTER SCIENCE
COM1102 Lab 2
6. Add a determinant () method in the class. It takes no argument, and returns the
determinant of the Matrix.
7. After you finish, run the testing code in the main method. You may also add
your own test cases at the end.
5
THE HANG SENG UNIVERSITY OF HONG KONG
DEPARTMENT OF COMPUTER SCIENCE
COM1102 Lab 2
Question 2 (1 mark)
Modify your StudentGrade program from last week to make it more object-oriented!
Both attributes can be public for the basic part of this question.
2. In your Student class, add a getGrade( ) method which returns the grade of the
student based on its score attribute.
Score Grade
Higher than 90 A
From 70 to 89 B
From 50 to 69 C
Lower than 50 F
The return type should be a char. For example, your method may look like:
3. In your StudentGrade2 class, create a new array for holding up to five Student
objects.
4. Display the message “Please enter the names and scores of the five students”.
5. Accept the name and score of each student one by one, separated by space or
newline. The scores are integer, and the names are alphabetical.
6. For each student record entered, create a new Student object to store the
student data (name and score), and store all five Student objects in your array.
6
THE HANG SENG UNIVERSITY OF HONG KONG
DEPARTMENT OF COMPUTER SCIENCE
COM1102 Lab 2
7. After all data has been entered, print out a grade report as follows:
Hints: use a for loop to retrieve the five Student objects from the array one by
one. For each object, use the getGrade() method of each Student object to
compute the grade, then print out the details of that student.
7
THE HANG SENG UNIVERSITY OF HONG KONG
DEPARTMENT OF COMPUTER SCIENCE
COM1102 Lab 2
File: Student.java
File: StudentGrade2.java