Antonio Roger P. CB.SC.
U4AIE23104
JAVA PRACTICE ASSIGNEMENT
Question-1:
import java.util.*;
public class Game {
public static void main(String[] args) {
Protectors protector = new Protectors();
Attackers attacker = new Attackers();
System.out.println("Pick Your Role");
System.out.println("1. Protector");
System.out.println("2. Attacker");
Scanner role=new Scanner(System.in);
int a=role.nextInt();
if (a==1) {
System.out.println("Protector actions:");
protector.walk();
protector.hide();
protector.defend();
System.out.println();
else if(a==2) {
System.out.println("Attacker actions:");
attacker.run();
attacker.shoot();
attacker.crawl();
System.out.println();
Antonio Roger P. CB.SC.U4AIE23104
// Interaction
attacker.observe(protector);
// Base class Human
class Human {
String name;
public Human(String name) {
this.name = name;
void run() {
System.out.println(name + " is running.");
void walk() {
System.out.println(name + " is walking.");
void swim() {
System.out.println(name + " is swimming.");
void climb() {
System.out.println(name + " is climbing.");
Antonio Roger P. CB.SC.U4AIE23104
// Subclass Protectors
class Protectors extends Human {
void hide() {
System.out.println(name + " cleverly hides in the shadows.");
void shoot() {
System.out.println(name + " lines up a precise shot and fires!");
// Subclass Attackers
class Attackers extends Human {
void crawl() {
System.out.println(name + " moves silently on all fours.");
void sneak() {
System.out.println(name + " advances cautiously, trying to remain unseen.");
// Interaction class
class Interaction {
Antonio Roger P. CB.SC.U4AIE23104
void interaction(Protectors protector, Attackers attacker) {
//
boolean hasAdvancedWeapon = true;
if (hasAdvancedWeapon) {
// Attacker tries to hide or run
attacker.hide();
attacker.run();
} else {
// Some other interaction logic
// For example, attacker approaches or attacks protector
attacker.sneak();
protector.shoot();
public class Main {
public static void main(String[] args) {
Protectors protector = new Protectors("Guardian");
Attackers attacker = new Attackers("Intruder");
Interaction interaction = new Interaction();
// Example of interaction
interaction.interaction(protector, attacker);
}
Antonio Roger P. CB.SC.U4AIE23104
Question-2:
class Matrix {
protected int[][] matrix;
protected int rows;
protected int columns;
// Constructor to initialize the matrix with specific dimensions and values
public Matrix(int rows, int columns, int[][] data) {
this.rows = rows;
this.columns = columns;
this.matrix = new int[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
this.matrix[i][j] = data[i][j];
// Method to add two matrices of the same dimensions, element-wise
public Matrix add(Matrix otherMatrix) {
if (this.rows != otherMatrix.rows || this.columns != otherMatrix.columns) {
throw new IllegalArgumentException("Matrix dimensions must match for addition.");
int[][] result = new int[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
result[i][j] = this.matrix[i][j] + otherMatrix.matrix[i][j];
Antonio Roger P. CB.SC.U4AIE23104
return new Matrix(rows, columns, result);
// Method to transpose the matrix
public Matrix transpose() {
int[][] result = new int[columns][rows];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
result[j][i] = this.matrix[i][j];
return new Matrix(columns, rows, result);
// Subclass SquareMatrix inherits from Matrix
class SquareMatrix extends Matrix {
// Constructor to initialize a square matrix
public SquareMatrix(int size, int[][] data) {
super(size, size, data);
// Method to calculate determinant of the square matrix
public int determinant() {
// Implementation of determinant calculation
Antonio Roger P. CB.SC.U4AIE23104
// Assuming it's calculated here and returning 0 for demonstration
return 0;
// Method to return the sum of the elements on the main diagonal of the matrix
public int diagonalSum() {
int sum = 0;
for (int i = 0; i < rows; i++) {
sum += matrix[i][i];
return sum;
// Method to check if the matrix is singular or not
public boolean isSingular() {
// Implementation of checking singularity
// Assuming it's implemented here and returning false for demonstration
return false;
public class Main {
public static void main(String[] args) {
// Example usage
int[][] data1 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int[][] data2 = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};
Antonio Roger P. CB.SC.U4AIE23104
Matrix matrix1 = new Matrix(3, 3, data1);
Matrix matrix2 = new Matrix(3, 3, data2);
// Add two matrices
Matrix sumMatrix = matrix1.add(matrix2);
System.out.println("Sum Matrix:");
sumMatrix.print();
// Transpose matrix
Matrix transposedMatrix = matrix1.transpose();
System.out.println("Transposed Matrix:");
transposedMatrix.print();
// Square matrix example
SquareMatrix squareMatrix = new SquareMatrix(3, data1);
System.out.println("Determinant: " + squareMatrix.determinant());
System.out.println("Diagonal Sum: " + squareMatrix.diagonalSum());
System.out.println("Is Singular: " + squareMatrix.isSingular());
For 2 by 2 matrice
class Matrix {
protected int[][] matrix;
protected int rows;
protected int columns;
Antonio Roger P. CB.SC.U4AIE23104
// Constructor to initialize the matrix with specific dimensions and values
public Matrix(int rows, int columns, int[][] data) {
this.rows = rows;
this.columns = columns;
this.matrix = new int[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
this.matrix[i][j] = data[i][j];
// Method to add two matrices of the same dimensions, element-wise
public Matrix add(Matrix otherMatrix) {
if (this.rows != otherMatrix.rows || this.columns != otherMatrix.columns) {
throw new IllegalArgumentException("Matrix dimensions must match for addition.");
int[][] result = new int[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
result[i][j] = this.matrix[i][j] + otherMatrix.matrix[i][j];
return new Matrix(rows, columns, result);
// Method to transpose the matrix
Antonio Roger P. CB.SC.U4AIE23104
public Matrix transpose() {
int[][] result = new int[columns][rows];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
result[j][i] = this.matrix[i][j];
return new Matrix(columns, rows, result);
// Subclass SquareMatrix inherits from Matrix
class SquareMatrix extends Matrix {
// Constructor to initialize a square matrix
public SquareMatrix(int size, int[][] data) {
super(size, size, data);
if (size != 2) {
throw new IllegalArgumentException("SquareMatrix must be 2x2.");
// Method to calculate determinant of the square matrix
public int determinant() {
return (matrix[0][0] * matrix[1][1]) - (matrix[0][1] * matrix[1][0]);
// Method to return the sum of the elements on the main diagonal of the matrix
Antonio Roger P. CB.SC.U4AIE23104
public int diagonalSum() {
return matrix[0][0] + matrix[1][1];
// Method to check if the matrix is singular or not
public boolean isSingular() {
return determinant() == 0;
public class Main {
public static void main(String[] args) {
// Example usage
int[][] data1 = {{1, 2}, {3, 4}};
int[][] data2 = {{4, 3}, {2, 1}};
// Square matrix example
SquareMatrix squareMatrix1 = new SquareMatrix(2, data1);
System.out.println("Determinant: " + squareMatrix1.determinant());
System.out.println("Diagonal Sum: " + squareMatrix1.diagonalSum());
System.out.println("Is Singular: " + squareMatrix1.isSingular());
SquareMatrix squareMatrix2 = new SquareMatrix(2, data2);
System.out.println("Determinant: " + squareMatrix2.determinant());
System.out.println("Diagonal Sum: " + squareMatrix2.diagonalSum());
System.out.println("Is Singular: " + squareMatrix2.isSingular());
}
Antonio Roger P. CB.SC.U4AIE23104