@@ -11,7 +11,6 @@ public static void main(String[] args) {
1111 System .out .println ("*** ***" );
1212 System .out .println ("******************************************" );
1313 System .out .println ();
14- Ex14 ex14 = new Ex14 ();
1514
1615 /* Question 1 */
1716 System .out .println ("Question 1: " );
@@ -22,79 +21,76 @@ public static void main(String[] args) {
2221 int [] arr5 = {2 , 1 , 5 , 1 , 4 , 1 , 1 , 1 , 2 }; // 7
2322 int [] arr6 = {9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 }; // 0
2423 int [] arr7 = {1 , 5 , 4 , 3 , 2 , 1 , 3 , 3 , 3 , 3 , 2 , 1 , 2 , 6 , 6 , 5 , 4 , 3 , 2 , 1 }; // 28
25- boolean trapWater = (ex14 .waterVolume (arr ) == 0 ) && (ex14 .waterVolume (arr2 ) == 1 ) &&
26- (ex14 .waterVolume (arr3 ) == 0 ) && (ex14 .waterVolume (arr4 ) == 7 ) && (ex14 .waterVolume (arr5 ) == 7 ) &&
27- (ex14 .waterVolume (arr6 ) == 0 ) && (ex14 .waterVolume (arr7 ) == 28 );
28- if (trapWater )
24+ boolean trapWater = (Ex14 .waterVolume (arr ) == 0 ) && (Ex14 .waterVolume (arr2 ) == 1 ) &&
25+ (Ex14 .waterVolume (arr3 ) == 0 ) && (Ex14 .waterVolume (arr4 ) == 7 ) && (Ex14 .waterVolume (arr5 ) == 7 ) &&
26+ (Ex14 .waterVolume (arr6 ) == 0 ) && (Ex14 .waterVolume (arr7 ) == 28 );
27+ if (trapWater )
2928 System .out .println (GOOD );
3029 else
3130 System .out .println (BAD );
3231
3332 /* Question 2 */
3433 System .out .println ("Question 2: " );
3534 int [] temp = checkWTF ();
36- if (temp == null )
35+ if (temp == null )
3736 System .out .println (GOOD );
3837 else {
3938 System .out .println (BAD );
4039 System .out .println ("\t \t \t The Array: " + printArr (temp ));
4140 System .out .println ("\t \t \t Expected: " + what (temp ));
42- System .out .println ("\t \t \t Your stupid method: " + ex14 .what (temp ));
41+ System .out .println ("\t \t \t Your stupid method: " + Ex14 .what (temp ));
4342 }
4443
4544 }
4645
47- public static String printArr (int [] arr ){
46+ public static String printArr (int [] arr ) {
4847 String ar = "" ;
4948 for (int i = 0 ; i < arr .length ; i ++) {
50- ar += arr [i ] + ", " ;
49+ ar += arr [i ];
50+ if (i < arr .length - 1 )
51+ ar += ", " ;
5152 }
5253 return ar ;
5354 }
5455
55- public static int [][] generateArrays (){
56+ public static int [][] generateArrays () {
5657 int [][] mat = new int [10 ][];
5758 Random rnd = new Random ();
5859
5960 for (int i = 0 ; i < mat .length ; i ++) {
6061 mat [i ] = new int [rnd .nextInt (30 )];
6162 for (int j = 0 ; j < mat [i ].length ; j ++) {
62- mat [i ][j ] = rnd . nextInt ( 200 );
63+ mat [i ][j ] = ( int ) (( Math . random () * 200 ) - 100 );
6364 }
6465 }
6566
6667 return mat ;
6768 }
6869
69- public static int [] checkWTF (){
70- Ex14 ex14 = new Ex14 ();
70+ public static int [] checkWTF () {
7171 int [][] mat = generateArrays ();
7272 for (int i = 0 ; i < mat .length ; i ++) {
73- if ( ex14 .what (mat [i ]) != what (mat [i ]))
73+ if ( Ex14 .what (mat [i ]) != what (mat [i ]))
7474 return mat [i ];
7575 }
7676 return null ;
7777 }
7878
79- private static int f (int []a , int low , int high )
80- {
79+ private static int f (int [] a , int low , int high ) {
8180 int res = 0 ;
82- for (int i = low ; i <= high ; i ++)
81+ for (int i = low ; i <= high ; i ++)
8382 res += a [i ];
8483 return res ;
8584 }
86- public static int what ( int [] a )
87- {
85+
86+ public static int what ( int [] a ) {
8887 int temp = 0 ;
89- for (int i =0 ; i <a .length ; i ++)
90- {
91- for (int j =i ; j <a .length ; j ++)
92- {
88+ for (int i = 0 ; i < a .length ; i ++) {
89+ for (int j = i ; j < a .length ; j ++) {
9390 int c = f (a , i , j );
94- if (c %2 == 0 )
95- {
96- if (j -i +1 > temp )
97- temp = j -i +1 ;
91+ if (c % 2 == 0 ) {
92+ if (j - i + 1 > temp )
93+ temp = j - i + 1 ;
9894 }
9995 }
10096 }
0 commit comments