0% found this document useful (0 votes)
8 views

Lab 2 Instruction (3+1 marks)

The document outlines the requirements for Lab 2 of COM1102 at The Hang Seng University of Hong Kong, focusing on implementing matrix operations in Java and modifying a student grading program. It details the creation of a Matrix class with methods for determinant calculation, addition, multiplication, and string representation, as well as a Student class to manage student data and grades. The assignment emphasizes object-oriented programming principles and includes a bonus for implementing data-hiding techniques.

Uploaded by

Lee Vico
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Lab 2 Instruction (3+1 marks)

The document outlines the requirements for Lab 2 of COM1102 at The Hang Seng University of Hong Kong, focusing on implementing matrix operations in Java and modifying a student grading program. It details the creation of a Matrix class with methods for determinant calculation, addition, multiplication, and string representation, as well as a Student class to manage student data and grades. The assignment emphasizes object-oriented programming principles and includes a bonus for implementing data-hiding techniques.

Uploaded by

Lee Vico
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

THE HANG SENG UNIVERSITY OF HONG KONG

DEPARTMENT OF COMPUTER SCIENCE


COM1102 Lab 2

Due: Week 4, before class


Instruction:
- Submit your source codes to Moodle (all .java files only)

Question 1 (2 marks)

Main class: Matrix Package: lab2

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 .

There are a few matrix operations that we need to implement:

1. Determinant

Given a matrix A= [ ac bd ] ,the determinant is a number that is given by


(a*d - b*c).

2. Matrix addition

We can add two matrices by adding up the corresponding elements.

For example, given the matrices

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

multiplying A by B (note their orders) gives the following matrix

[ ae+ bg
ce +dg
af +bh
cf + dh].

What to do for this question

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
:

Matrix M1 = new Matrix(1,2,3,4);

3. Add a toString() method in the class. It takes no arguments and returns a String
representation of the matrix.

For example, given the above Matrix M1, the statement

System.out.println(M1.toString());

would print out


1 2

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

Note that if we pass an object to the Java method System.out.println


method as one of the arguments, println would actually help you to call the
object's toString method automatically if it is available. So, the following statement
should work as well.

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.

For example, given that M 1= [13 24 ] M =[3311 4422]


2

or in Java code
Matrix M1 = new Matrix(1,2,3,4); and

Matrix M2 = new Matrix(11,22,33,44);

the expression M1.add(M2)will return a Matrix object that is equivalent to

[
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.

For example, given that M 1= [13 24 ] M =[ 0.5


3
0.9
−0.3
−1 ]
or in Java code
Matrix M1 = new Matrix(1,2,3,4); and

Matrix M3 = new Matrix(0.5,-0.3,0.9,-1);

the expression M1.mult(M3)will return a Matrix object that is equivalent to

[ 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.

For example, given that M 1= [13 24 ]


or in Java code

Matrix M1 = new Matrix(1,2,3,4);

calling M1.determinant() shall return -2, which is the determinant of


Matrix M1 (i.e., 1*4 – 2*3).

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)

Main class: StudentGrade2 Package: lab2

Modify your StudentGrade program from last week to make it more object-oriented!

1. Create a new class (Student) with the following attributes


 name (String): the name of a student
 score (int): the score of a student

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:

Then, create a new main class program (StudentGrade2) to do the following:

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:

***** Grade Report *****


Name1, Score1, Grade1
Name2, Score2, Grade2
Name3, Score3, Grade3
Name4, Score4, Grade4
Name5, Score5, Grade5
***** End of Report *****

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.

8. The following is a sample run of the program

 Please enter the names and scores of the five students


Alan 60
Betty 70
Cindy 50
Donald 30
Elaine 90

 ***** Grade Report *****


 Alan, 60, C
 Betty, 70, B
 Cindy, 50, C
 Donald, 30, F
 Elaine, 90, A
 ***** End of Report *****

7
THE HANG SENG UNIVERSITY OF HONG KONG
DEPARTMENT OF COMPUTER SCIENCE
COM1102 Lab 2

Question 2 – Bonus Part (Extra 1 mark)

Enhance your Question 2 answer by applying data-hiding in the Student class.


- All attributes should be private
- Provide setters and getters method
- Your program must work to get the bonus marks

Suggested Program Design for Question 2

File: Student.java

File: StudentGrade2.java

You might also like