-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccumulation.java
75 lines (61 loc) · 2.07 KB
/
Accumulation.java
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
public class Accumulation {
public static void main(String[] args) {
System.out.println("--- Test sumN ---");
System.out.println(sumN(0)); //expect 0
System.out.println(sumN(1)); //expect 1
System.out.println(sumN(2)); //expect 3
System.out.println(sumN(5)); //expect 15
System.out.println(multN(3)); //expect 6
System.out.println(multN(5)); //expect 120
System.out.println(multN(0)); //expect 1 because you start at 1 and loop doesn't run
System.out.println("--- Test sumMult5 ---");
System.out.println(sumMult5(0)); //expect 0
System.out.println(sumMult5(3)); //expect 0
System.out.println(sumMult5(5)); //expect 5
System.out.println(sumMult5(12)); //expect 15
System.out.println(sumMult5(15)); //expect 30
System.out.println(sumMult5(16)); //expect 30
System.out.println(multiplyString("hey", 3));
System.out.println(multiplyString("h", 5));
System.out.println(multiplyString("hey", 0));
System.out.println(multiplyString(" ", 100));
}
//sum all the integers between 1 and n (included)
public static int sumN(int n) {
int result = 0;
for (int i = 1; i <= n; i++) {
result = result + i; //update result
}
return result;
}
//multiply all the integers between 1 and n (included)
public static int multN(int n) {
int result = 1;
for (int i = 1; i <= n; i++) { //i++ means i = i + 1
result = result * i;
}
return result;
}
//sums all the integer multiples of 5 up to n (included)
public static int sumMult5 (int n) {
int result = 0;
for (int i = 5; i <= n; i += 5) { //i += 5 means i = i + 5
//result = result + i; means same thing as...
result += i; //same as result = result + i
}
return result;
}
//multiply a given string n times
//example: multiplyString("hey", 3) returns "heyheyhey"
public static String multiplyString(String s, int n) {
String result = "";
for (int i = 0; i < n; i++) {
result = result + s;
}
return result;
}
}
//accumulation strategy
//1. initialize a variable that will contain the result
//2. update the result inside a loop
//3. after the loop, collect and return the result