OOP’s in JAVA Batch - B Time :- 1hr
Task:
Design a class that represents 2×2 matrices and supports evaluation and combination through constructor/method
overloading and object passing. (5 Marks)
Requirements:
• Class Matrix2x2
◦ Fields: double[][] values (size 2×2), final int matrixId, static int count.
◦ Constructors:
1. (double a, double b, double c, double d) → fill 2×2 matrix.
2. Copy constructor (deep copy).
◦ Methods:
1. determinant() → return determinant.
2. trace() → return sum of diagonal elements.
3. multiply(Matrix2x2 m) → return new Matrix2x2 as multiplication result.
4. add(Matrix2x2 m) → return new Matrix2x2 as addition result.
5. Overload scale(int k) and scale(double k) → multiply all elements by scalar.
6. toString() → pretty print matrix.
◦ Static: getCount() → total matrices created.
Expected Usage:
Matrix2x2 m1 = new Matrix2x2(1, 2, 3, 4);
Matrix2x2 m2 = new Matrix2x2(2, 0, 1, 2);
Matrix2x2 m3 = [Link](m2);
[Link]("M3 = " + m3);
[Link]("Determinant of M3: " + [Link]());
Expected Output:
Matrix M1 (ID 1): [1.00 2.00] [3.00 4.00]
Matrix M2 (ID 2): [2.00 0.00] [1.00 2.00]
Matrix M3 = M1 * M2 (ID 3): [4.00 4.00] [10.00 8.00]
Determinant of M3: -4.0
Total Matrices Created: 3
OOP’s in JAVA Batch - B Time :- 1hr
Task:
Design a system to represent and operate on complex numbers using constructor overloading, method overloading,
and object passing. (5 marks)
Requirements
• Class Complex
◦ Fields:
▪ double real, double imag
▪ final int complexId → unique id per object
▪ static int count → total complex numbers created
◦ Constructors:
▪ (double r) → create purely real number (r + 0i).
▪ (double r, double i) → create complex number r + i·i.
▪ Copy constructor (deep copy).
◦ Methods:
▪ add(Complex c) → returns new Complex (sum).
▪ add(double k) → overload → add constant to real part.
▪ multiply(Complex c) → returns new Complex (product).
▪ multiply(double k) → overload → scalar multiplication.
▪ conjugate() → return conjugate as new Complex.
▪ magnitude() → return magnitude √(real² + imag²).
▪ toString() → format "a + bi" (handle negative properly).
◦ Static:
▪ getCount() → return total number of complex objects created.
Sample Usage
Complex c1 = new Complex(2, 3); // 2 + 3i
Complex c2 = new Complex(1, -4); // 1 - 4i
Complex c3 = [Link](c2); // add
Complex c4 = [Link](c2); // multiply
Complex c5 = new Complex(c1); // copy
OOP’s in JAVA Batch - B Time :- 1hr
[Link]("c1: " + c1);
[Link]("c2: " + c2);
[Link]("c3 (sum): " + c3);
[Link]("c4 (product): " + c4);
[Link]("Conjugate of c1: " + [Link]());
[Link]("Magnitude of c1: " + [Link]());
[Link]("Total Complex Numbers: " + [Link]());
Expected Output
c1: 2.0 + 3.0i
c2: 1.0 - 4.0i
c3 (sum): 3.0 - 1.0i
c4 (product): 14.0 - 5.0i
Conjugate of c1: 2.0 - 3.0i
Magnitude of c1: 3.605551275463989
Total Complex Numbers: 5