Skip to content

Commit 022ec37

Browse files
committed
Merge branch 'neha-practice' of https://github.com/code4tomorrow/java into neha-practice
2 parents 8d4141f + 0225037 commit 022ec37

File tree

6 files changed

+33
-46
lines changed

6 files changed

+33
-46
lines changed

src/com/codefortomorrow/advanced/chapter13/examples/IterToRecursive.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ public static void main(String[] args) {
99

1010
int a = 72, b = 20;
1111
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;
1613
}
1714
System.out.println("GCD:" + a);
1815

@@ -23,20 +20,17 @@ else if (b>a)
2320
// Recursive function
2421
public static int power(int base, int exp) {
2522
// Base case: exp = 0, so the result is 1.
26-
if(exp == 0)
27-
return 1;
23+
if (exp == 0) return 1;
2824

2925
// Multiply once by the previous power.
30-
return power(base, exp-1)*base;
26+
return power(base, exp - 1) * base;
3127
}
3228

3329
public static int gcd(int a, int b) {
3430
// 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);
4135
}
4236
}

src/com/codefortomorrow/advanced/chapter13/practice/NegativeSum.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@ public class NegativeSum {
1010
public static void main(String[] args) {
1111
System.out.println("Iterative:");
1212
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;
2120
}
2221
}
2322
System.out.println("a + b: " + (a + b));
@@ -27,7 +26,6 @@ public static void main(String[] args) {
2726
}
2827

2928
public static int negSum(int a, int b) {
30-
3129
// Insert code here and change return statement.
3230
return 0;
3331
}

src/com/codefortomorrow/advanced/chapter13/solutions/NegativeSum.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@ public class NegativeSum {
1010
public static void main(String[] args) {
1111
System.out.println("Iterative:");
1212
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;
2120
}
2221
}
2322
System.out.println("a + b: " + (a + b));
@@ -27,11 +26,10 @@ public static void main(String[] args) {
2726
}
2827

2928
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);
3634
}
3735
}

src/com/codefortomorrow/advanced/chapter14/examples/Throw.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,15 @@ public class Throw {
55
public static void main(String[] args) {
66
try {
77
System.out.println(divide(7, 2));
8-
} catch(ArithmeticException e) {
8+
} catch (ArithmeticException e) {
99
System.out.println("There was an arithmetic exception.");
1010
}
1111
}
1212

1313
public static int divide(int a, int b) {
1414
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();
1716
System.out.println("Got to end of divide()");
1817
return a / b;
1918
}
20-
2119
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package com.codefortomorrow.advanced.chapter14.examples;
2+
23
import java.io.*;
34

45
public class Throws {
@@ -7,13 +8,12 @@ public static void main(String[] args) {
78
try {
89
File file = null;
910
access(file);
10-
} catch(IOException e) {
11+
} catch (IOException e) {
1112
System.out.println("There was an IOException.");
1213
}
1314
}
1415

1516
public static void access(File file) throws IOException {
16-
if(file == null)
17-
throw new IOException();
17+
if (file == null) throw new IOException();
1818
}
1919
}

src/com/codefortomorrow/advanced/chapter14/examples/UserExceptions.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@ public class UserExceptions {
55
public static void main(String[] args) {
66
try {
77
call(null);
8-
} catch(MyException e) {
8+
} catch (MyException e) {
99
System.out.println("Caught MyException");
1010
}
1111

1212
throw new ArithmeticException("divide by zero error");
1313
}
1414

1515
public static void call(String s) throws MyException {
16-
if(s == null)
17-
throw new MyException();
16+
if (s == null) throw new MyException();
1817
}
1918
}

0 commit comments

Comments
 (0)