-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise08_25.java
More file actions
61 lines (52 loc) · 1.58 KB
/
Copy pathExercise08_25.java
File metadata and controls
61 lines (52 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.util.Scanner;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author jorda
*/
public class Exercise08_25 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a 3 by 3 matrix row by row: ");
double[][] m = new double[3][3];
for (int col = 0; col < m.length; col++) {
for (int row = 0; row < m[col].length; row++) {
m[col][row] = input.nextDouble();
}
}
if (isMarkovMatrix(m)) {
System.out.println("It is a Markov matrix");
} else {
System.out.println("It is not a Markov matrix");
}
}
public static boolean isMarkovMatrix(double[][] m) {
return isElementsPositive(m) && isEachColumnSum1(m);
}
public static boolean isElementsPositive(double[][] m) {
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < m[i].length; j++) {
if (m[i][j] < 0) {
return false;
}
}
}
return true;
}
public static boolean isEachColumnSum1(double[][] m) {
for (int col = 0; col < m.length; col++) {
double sum = 0;
for (int row = 0; row < m.length; row++) {
sum += m[row][col];
}
if (sum != 1) {
return false;
}
}
return true;
}
}