Java Code Snippet
Java Code Snippet
U4AIE23104
import java.util.*;
System.out.println("1. Protector");
System.out.println("2. Attacker");
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);
class Human {
String name;
this.name = name;
void run() {
void walk() {
void swim() {
void climb() {
// Subclass Protectors
void hide() {
void shoot() {
// Subclass Attackers
void crawl() {
void sneak() {
// Interaction class
class Interaction {
Antonio Roger P. CB.SC.U4AIE23104
//
if (hasAdvancedWeapon) {
attacker.hide();
attacker.run();
} else {
attacker.sneak();
protector.shoot();
// Example of interaction
interaction.interaction(protector, attacker);
}
Antonio Roger P. CB.SC.U4AIE23104
Question-2:
class Matrix {
this.rows = rows;
this.columns = columns;
this.matrix[i][j] = data[i][j];
result[j][i] = this.matrix[i][j];
return 0;
// Method to return the sum of the elements on the main diagonal of the matrix
int sum = 0;
sum += matrix[i][i];
return sum;
return false;
// Example usage
System.out.println("Sum Matrix:");
sumMatrix.print();
// Transpose matrix
System.out.println("Transposed Matrix:");
transposedMatrix.print();
For 2 by 2 matrice
class Matrix {
this.rows = rows;
this.columns = columns;
this.matrix[i][j] = data[i][j];
result[j][i] = this.matrix[i][j];
if (size != 2) {
// Method to return the sum of the elements on the main diagonal of the matrix
Antonio Roger P. CB.SC.U4AIE23104
return determinant() == 0;
// Example usage
}
Antonio Roger P. CB.SC.U4AIE23104