File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /**
2+ * Tests whether x is a single digit integer.
3+ *
4+ * @param x the integer to test
5+ * @return true if x has one digit, false otherwise
6+ */
7+ public static boolean isSingleDigit (int x ) {
8+ if (x > -10 && x < 10 ) {
9+ return true ;
10+ } else {
11+ return false ;
12+ }
13+ }
14+
15+
16+ System .out .println (isSingleDigit (2 ));
17+ boolean bigFlag = !isSingleDigit2 (17 );
18+
Original file line number Diff line number Diff line change 1+ /**
2+ * CodingBat examples from Chapter 8.
3+ */
4+ public class CodingBat {
5+
6+ public static String noX (String str ) {
7+ if (str .length () == 0 ) {
8+ return "" ;
9+ }
10+ char c = str .charAt (0 );
11+ if (c == 'x' ) {
12+ return noX (str .substring (1 ));
13+ } else {
14+ return c + noX (str .substring (1 ));
15+ }
16+ }
17+
18+ public int array11 (int [] nums , int index ) {
19+ if (index >= nums .length ) {
20+ return 0 ;
21+ }
22+ if (nums [index ] == 11 ) {
23+ return 1 + array11 (nums , index + 1 );
24+ } else {
25+ return array11 (nums , index + 1 );
26+ }
27+ }
28+
29+ }
Original file line number Diff line number Diff line change 1- public class Recursive {
1+ /**
2+ * Stack diagram exercise.
3+ */
4+ public class Exercise {
25
36 public static void main (String [] args ) {
47 System .out .println (prod (1 , 4 ));
Original file line number Diff line number Diff line change 11/**
2- * Recursion exercise.
2+ * Palindrome exercise.
33 */
44public class Recurse {
55
Original file line number Diff line number Diff line change @@ -4,17 +4,11 @@ public static void main(String[] args) {
44 System .out .println ("countdown" );
55 countdown (3 );
66
7- System .out .println ("countup" );
8- countup (3 );
9-
10- System .out .println ("newLine" );
11- newLine ();
12-
137 System .out .println ("nLines" );
148 nLines (3 );
159
16- System .out .println ("threeLine " );
17- threeLine ( );
10+ System .out .println ("countup " );
11+ countup ( 3 );
1812
1913 System .out .println ("displayBinary" );
2014 displayBinary (23 );
@@ -34,12 +28,6 @@ public static void newLine() {
3428 System .out .println ();
3529 }
3630
37- public static void threeLine () {
38- newLine ();
39- newLine ();
40- newLine ();
41- }
42-
4331 public static void nLines (int n ) {
4432 if (n > 0 ) {
4533 System .out .println ();
Original file line number Diff line number Diff line change 33 */
44public class Series {
55
6- public static void countup (int n ) {
7- if (n == 0 ) {
8- System .out .println ("Blastoff!" );
9- } else {
10- countup (n - 1 );
11- System .out .println (n );
12- }
13- }
14-
15- /**
16- * Tests whether x is a single digit integer.
17- *
18- * @param x the integer to test
19- * @return true if x has one digit, false otherwise
20- */
216 public static boolean isSingleDigit (int x ) {
22- if (x > -10 && x < 10 ) {
23- return true ;
24- } else {
25- return false ;
26- }
27- }
28-
29- public static boolean isSingleDigit2 (int x ) {
307 return x > -10 && x < 10 ;
318 }
329
@@ -48,12 +25,6 @@ public static int fibonacci(int n) {
4825
4926 public static void main (String [] args ) {
5027
51- countup (3 );
52- System .out .println ("Have a nice day." );
53-
54- System .out .println (isSingleDigit (2 ));
55- boolean bigFlag = !isSingleDigit2 (17 );
56-
5728 int z = 9 ;
5829 if (isSingleDigit (z )) {
5930 System .out .println ("z is small" );
You can’t perform that action at this time.
0 commit comments