- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursion.java
More file actions
Latest commit
120 lines (112 loc) · 2.84 KB
/
Copy pathRecursion.java
File metadata and controls
120 lines (112 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* Chapter 8.3
*
* Connected to this class is a Test2 Class in the
* HowSubstringsWork folder!
*/
importjava.util.Scanner;
//import java.util.*;
publicclassRecursion
{
publicstaticvoidmain(String[] args)
{
System.out.println("Pick a number to factorial!");
Scannerin = newScanner(System.in);
intuserNum = in.nextInt();
System.out.println(factorial(userNum) + "\n");
System.out.println("Type in any number to see it in Binary!");
intuserBin = in.nextInt();
binary(userBin);
System.out.println();
in.nextLine();
System.out.println("Please provide a String!");
StringuserStr = in.nextLine();
StringuserLow = userStr.toLowerCase();
System.out.println(noX(userLow));
}
//"stub"
/*public static int factorial(int n)
{
return 0;
}*/
publicstaticintfactorial(intn)
{
if(n == 0)
return1;
intsum = n*factorial(n-1);
returnsum;
//return n*factorial(n-1);
}
publicstaticvoidbinary(intnum)
{
//(1)WRONG includes the base case BUT prints normal
//Because the f(n) is done before everything but printed after!
//(1)int numToBinary = num % 2;
if(num != 0)
{
//(2)WRONG prints correctly just backwards because
//it prints before the recursion
//(2)System.out.print(num%2);
binary(num/2);
System.out.print(num%2);
}
//(1)System.out.print(numToBinary);
//return numToBinary;
/*
int numToBinary = 0;
int mod = 0;
//bad thinking because dividing will bring any
//number to 1 or 2 NOT 0 or 1 and that last 1 or 0
//division is important for the one's place!!!
if(num == 0)
return 0;
else if(num == 1)
return 1;
else
{
mod = num % 2;
System.out.println(mod);
numToBinary = binary(num/2);
System.out.println(mod + "\n\n");
System.out.println(mod);
}
return numToBinary;*/
}
publicstaticStringnoX(Stringsent)
{
if(sent.length() == 0)
return"";
//Why can't I use this in the return and why can it be used
//for loop without being initialized 1st??????
StringnoX;
StringabNoX = "";
for(inti = 0; i < sent.length(); i++)
{
if(sent.charAt(i) == 0 && sent.charAt(i) == 'x')
sent.substring(i+1);
if(sent.charAt(i) == 'x')
{
//System.out.println(sent);
StringnotX = sent.substring(i+1);
//System.out.println(notX);
noX = sent.substring(0, i) + notX;
//Why does this print out the answer and not our return?
System.out.println(noX);
abNoX = noX(noX);
}
}
//System.out.println(noX); ?
returnabNoX;
}
//How many times the No. 11 appears in an array
publicstaticintarray11(int[] nums, intindex)
{
if(index >= nums.length)
return0;
intrecurse = array11(nums, index+1);
if(nums[index] == 11)
return (recurse + 1);
else
returnrecurse;
}
}