File tree Expand file tree Collapse file tree 6 files changed +33
-46
lines changed
src/com/codefortomorrow/advanced Expand file tree Collapse file tree 6 files changed +33
-46
lines changed Original file line number Diff line number Diff line change @@ -9,10 +9,7 @@ public static void main(String[] args) {
9
9
10
10
int a = 72 , b = 20 ;
11
11
while (a != b ) {
12
- if (a > b )
13
- a -= b ;
14
- else if (b >a )
15
- b -= a ;
12
+ if (a > b ) a -= b ; else if (b > a ) b -= a ;
16
13
}
17
14
System .out .println ("GCD:" + a );
18
15
@@ -23,20 +20,17 @@ else if (b>a)
23
20
// Recursive function
24
21
public static int power (int base , int exp ) {
25
22
// Base case: exp = 0, so the result is 1.
26
- if (exp == 0 )
27
- return 1 ;
23
+ if (exp == 0 ) return 1 ;
28
24
29
25
// Multiply once by the previous power.
30
- return power (base , exp - 1 )* base ;
26
+ return power (base , exp - 1 ) * base ;
31
27
}
32
28
33
29
public static int gcd (int a , int b ) {
34
30
// Base case
35
- if (a == b )
36
- return a ;
37
- else if (a > b )
38
- return gcd (a -b , b );
39
- else
40
- return gcd (a , b -a );
31
+ if (a == b ) return a ; else if (a > b ) return gcd (
32
+ a - b ,
33
+ b
34
+ ); else return gcd (a , b - a );
41
35
}
42
36
}
Original file line number Diff line number Diff line change @@ -10,14 +10,13 @@ public class NegativeSum {
10
10
public static void main (String [] args ) {
11
11
System .out .println ("Iterative:" );
12
12
int a = 50 , b = 40 ;
13
- while (a + b > 0 ) {
14
- if (Math .pow (a , 2 ) + Math .pow (b , 2 ) > 10000 ) {
15
- a *= 8 ;
16
- b *= -4 ;
17
- }
18
- else {
19
- a *= 3 ;
20
- b /= 2 ;
13
+ while (a + b > 0 ) {
14
+ if (Math .pow (a , 2 ) + Math .pow (b , 2 ) > 10000 ) {
15
+ a *= 8 ;
16
+ b *= -4 ;
17
+ } else {
18
+ a *= 3 ;
19
+ b /= 2 ;
21
20
}
22
21
}
23
22
System .out .println ("a + b: " + (a + b ));
@@ -27,7 +26,6 @@ public static void main(String[] args) {
27
26
}
28
27
29
28
public static int negSum (int a , int b ) {
30
-
31
29
// Insert code here and change return statement.
32
30
return 0 ;
33
31
}
Original file line number Diff line number Diff line change @@ -10,14 +10,13 @@ public class NegativeSum {
10
10
public static void main (String [] args ) {
11
11
System .out .println ("Iterative:" );
12
12
int a = 50 , b = 40 ;
13
- while (a + b > 0 ) {
14
- if (Math .pow (a , 2 ) + Math .pow (b , 2 ) > 10000 ) {
15
- a *= 8 ;
16
- b *= -4 ;
17
- }
18
- else {
19
- a *= 3 ;
20
- b /= 2 ;
13
+ while (a + b > 0 ) {
14
+ if (Math .pow (a , 2 ) + Math .pow (b , 2 ) > 10000 ) {
15
+ a *= 8 ;
16
+ b *= -4 ;
17
+ } else {
18
+ a *= 3 ;
19
+ b /= 2 ;
21
20
}
22
21
}
23
22
System .out .println ("a + b: " + (a + b ));
@@ -27,11 +26,10 @@ public static void main(String[] args) {
27
26
}
28
27
29
28
public static int negSum (int a , int b ) {
30
- if (a + b <= 0 )
31
- return a + b ;
32
- if (Math .pow (a , 2 ) + Math .pow (b , 2 ) > 10000 )
33
- return negSum (a * 8 , b * -4 );
34
- else
35
- return negSum (a * 3 , b / 2 );
29
+ if (a + b <= 0 ) return a + b ;
30
+ if (Math .pow (a , 2 ) + Math .pow (b , 2 ) > 10000 ) return negSum (
31
+ a * 8 ,
32
+ b * -4
33
+ ); else return negSum (a * 3 , b / 2 );
36
34
}
37
35
}
Original file line number Diff line number Diff line change @@ -5,17 +5,15 @@ public class Throw {
5
5
public static void main (String [] args ) {
6
6
try {
7
7
System .out .println (divide (7 , 2 ));
8
- } catch (ArithmeticException e ) {
8
+ } catch (ArithmeticException e ) {
9
9
System .out .println ("There was an arithmetic exception." );
10
10
}
11
11
}
12
12
13
13
public static int divide (int a , int b ) {
14
14
int result = a / b ;
15
- if (result != (double )a / (double )b )
16
- throw new ArithmeticException ();
15
+ if (result != (double ) a / (double ) b ) throw new ArithmeticException ();
17
16
System .out .println ("Got to end of divide()" );
18
17
return a / b ;
19
18
}
20
-
21
19
}
Original file line number Diff line number Diff line change 1
1
package com .codefortomorrow .advanced .chapter14 .examples ;
2
+
2
3
import java .io .*;
3
4
4
5
public class Throws {
@@ -7,13 +8,12 @@ public static void main(String[] args) {
7
8
try {
8
9
File file = null ;
9
10
access (file );
10
- } catch (IOException e ) {
11
+ } catch (IOException e ) {
11
12
System .out .println ("There was an IOException." );
12
13
}
13
14
}
14
15
15
16
public static void access (File file ) throws IOException {
16
- if (file == null )
17
- throw new IOException ();
17
+ if (file == null ) throw new IOException ();
18
18
}
19
19
}
Original file line number Diff line number Diff line change @@ -5,15 +5,14 @@ public class UserExceptions {
5
5
public static void main (String [] args ) {
6
6
try {
7
7
call (null );
8
- } catch (MyException e ) {
8
+ } catch (MyException e ) {
9
9
System .out .println ("Caught MyException" );
10
10
}
11
11
12
12
throw new ArithmeticException ("divide by zero error" );
13
13
}
14
14
15
15
public static void call (String s ) throws MyException {
16
- if (s == null )
17
- throw new MyException ();
16
+ if (s == null ) throw new MyException ();
18
17
}
19
18
}
You can’t perform that action at this time.
0 commit comments