Java Assignment 4
Java Assignment 4
Java Assignment 4
Output:-
Fig. 2.1
System.out.println("\nSecond Double matrix is : ")
;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(b[i][j] + " ");
}
System.out.println();
}
System.out.println("\nThe addition of two Double m
atrices is : ");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(sum[i][j] + " ");
}
System.out.println();
}
}
void addMatrix(int[][] a, int[][] b) {
int[][] sum = new int[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
sum[i][j] = a[i][j] + b[i][j];
}
}
System.out.println("\nFirst Integer matrix is : ")
;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
System.out.println("\nSecond Integer matrix is : "
);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(b[i][j] + " ");
}
System.out.println();
}
System.out.println("\nThe addition of two Integer
matrices is : ");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(sum[i][j] + " ");
}
System.out.println();
}
}
void addMatrix(float[][] a, float[][] b) {
float[][] sum = new float[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
sum[i][j] = a[i][j] + b[i][j];
}
}
System.out.println("\nFirst Floating type matrix i
s : ");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
System.out.println("\nSecond Floating type matrix
is : ");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(b[i][j] + " ");
}
System.out.println();
}
System.out.println("\nThe addition of the Floatin
g type matrices is : ");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(sum[i][j] + " ");
}
System.out.println();
}
}
}
public class AddMatrix {
public static void main(String[] args) {
// Scanner sc = new Scanner(System.in);
// double[][] x = new double[3][3];
// double[][] y = new double[3][3];
double x[][]={{4.5,3,4},{2.5,7.4,3.1},{5,4.8,5}};
double y[][]={{7,3.1,4.99},{2.1,4.5,3},
{0.11,2,4.22,4.1}};
int a[][]={{4,3,4},{2,7,3},{5,4,5}};
int b[][]={{7,3,4},{2,4,3},{8,2,4}};
float c[][]={{4.1f,3.8f,4},{2.2f,7.11f,3.96f},
{5.5f,4.1f,5.11f}};
float d[][]={{7f,3.5f,0.52f},{2.1f,0.5f,3.21f},
{8.52f,2.21f,4.5f}};
// System.out.println("\nEnter the elements of fir
st 3X3 matrix :");
// for (int i = 0; i < 3; i++) {
// for (int j = 0; j < 3; j++) {
// x[i][j] = sc.nextInt();
// }
// }
// System.out.println("\nEnter the elements of sec
ond 3X3 matrix :");
// for (int i = 0; i < 3; i++) {
// for (int j = 0; j < 3; j++) {
// y[i][j] = sc.nextInt();
// }
// }
Matrix m = new Matrix();
m.addMatrix(x, y);
m.addMatrix(a, b);
m.addMatrix(c, d);
}
}
Output:-
Fig. 2.2
Ques 3 - Write the Output with description of the following :
class A{
static{
System.out.println(“Static Block”);
}
static public void main(String...args){
System.out.println(“This is the main block”);
}
}
Ans 3 - Above given program print “Static Block” first and then print “This is
main block”.
Because “Static Block” has written under the static block and in Java,
static block executes first because it has not required object to invoke
static block. And The static block holds the memory itself and does
not require any variable.
Output:-
Fig. 2.2