Financial tsunami java program

image

this is the program that i am attempting to write.

and it says that bank 1 is unsafe which i don’t quite get.

because bank 1’s total assets are

125=its own
85=it lent to bank3
40=it lent to bank2

i wonder if author did a mistake?

Has there been a definition of what “under a certain limit” is?

Just solved this:

import java.util.Arrays;  
  
public class Lr {  
    public static void main(String[] args) {  
        double[][] assets = {  
                {25, 100.5, 0, 0, 320.5},  
                {0, 125, 40, 85, 0},  
                {125, 0, 175, 75, 0},  
                {125, 0, 0, 75, 0},  
                {0, 0, 125, 0, 181}  
        };  
        // calculate assets  
        double[] totalAsset = new double[5];  
        boolean[] bankSafe = new boolean[5];// we have only 5 banks  
        boolean[] saveBankSafe = new boolean[bankSafe.length];  
        // initialize the entire array as true;  
        for (int i = 0; i < bankSafe.length; i++) {  
            bankSafe[i] = true;  
        }  
        while (!Arrays.equals(bankSafe, saveBankSafe)) {  
            // backup this array  
            for (int i = 0; i < bankSafe.length; i++) {  
                saveBankSafe[i] = bankSafe[i];  
            }  
            totalAsset = calculateAsset(assets);  
            for (int i = 0; i < totalAsset.length; i++) {  
                if (!isSafe(totalAsset[i])) {  
                    bankSafe[i] = false;  
                }  
            }  
            // re calculate assets of banks who lent to Bank 3 the unsafe bank  
            // the banks who lent to bank3 are represented as [B0-B1's index][B3 index] B3 index will be calculated belows        /*        j is an array that contains only unsafe banks. The unsafe banks is the index in the bankSafe array where arr[index]>0         */            int[] j = new int[5];  
            for (int i = 0; i < bankSafe.length; i++) {  
                j[i] = !bankSafe[i] ? i : -1;  
            }  
            for (int i = 0; i < assets.length; i++) {  
                for (int k = 0; k < j.length; k++) { // Loop over j array  
                    if (j[k] >= 0 && assets[i][j[k]] > 0) {  
                        // Do something with assets[i][j[k]]  
                        assets[i][j[k]] = 0;  
                    }  
                }  
            }  
        }  
        for (int i = 0; i < bankSafe.length; i++) {  
            int unsafe = bankSafe[i] ? -1 : i;  
            if (unsafe == i)  
                System.out.println("Bank " + i + " is unsafe ");  
        }  
    }  
  
    public static boolean isSafe(double totalAssetPerBank) {  
        if (totalAssetPerBank > 201) {  
            return true;  
        }  
        return false;  
    }  
  
    public static double[] calculateAsset(double[][] assets) {  
        double[] totalAsset = new double[5];  
        for (int i = 0; i < assets.length; i++) {  
            for (int j = 0; j < assets[i].length; j++) {  
                if (assets[i][j] > 0) {  
                    totalAsset[i] += assets[i][j];  
                }  
            }  
        }  
        return totalAsset;  
    }  
}
1 Like

That’s awesome! :d_smile:

1 Like