-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeekendAssignment.java
More file actions
239 lines (219 loc) · 9.52 KB
/
WeekendAssignment.java
File metadata and controls
239 lines (219 loc) · 9.52 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/**
* class: WeekendAssignment
* @author: Hugo Padilla
* @date: March 27, 2023
* @version: 1.0
* @course: ITEC 2140 - 05 Spring 2023
* Description: In this Java class, I will be incorporating multiple methods into one overall Class, WeekendAssignment.
* In addition to this, I will be testing to ensure that all of my methods work coherently within the Class.
*/
public class WeekendAssignment{
// I will start with the main method for organization, so my test cases are clearly visible, and the used methods will follow below.
// I make my expected result post with the actual to console for easier reference.
// I also make the inputs actual variables, so that way there are multiple test cases if you would wish.
public static void main (String[] args){
WeekendAssignment wa = new WeekendAssignment();
// test reverseDoubleChar
String input1 = "hello";
String actual1 = wa.reverseDoubleChar(input1);
System.out.println("reverseDoubleChar(\"" + input1 + "\"): " + actual1 + " // Expected result: olleehh");
String input2 = "test";
String actual2 = wa.reverseDoubleChar(input2);
System.out.println("reverseDoubleChar(\"" + input2 + "\"): " + actual2 + " // Expected result: tsettss");
// test sumDigits
int input3 = 1234;
int actual3 = wa.sumDigits(input3);
System.out.println("sumDigits(" + input3 + "): " + actual3 + " // Expected result: 10");
int input4 = 0;
int actual4 = wa.sumDigits(input4);
System.out.println("sumDigits(" + input4 + "): " + actual4 + " // Expected result: 0");
// test birthdayName
String input5 = "Hugo";
String actual5 = wa.birthdayName(input5);
System.out.println("birthdayName(\"" + input5 + "\"): " + actual5 + " // Expected result: Happy Birthday Hugo!");
String input6 = "Dr. Park";
String actual6 = wa.birthdayName(input6);
System.out.println("birthdayName(\"" + input6 + "\"): " + actual6 + " // Expected result: Happy Birthday Dr. Park!");
// test missingFront
String input7 = "Hello";
String actual7 = wa.missingFront(input7);
System.out.println("missingFront(\"" + input7 + "\"): " + actual7 + " // Expected result: lo");
String input8 = "test";
String actual8 = wa.missingFront(input8);
System.out.println("missingFront(\"" + input8 + "\"): " + actual8 + " // Expected result: t");
// test swapEnds
String input9 = "Hello";
String actual9 = wa.swapEnds(input9);
System.out.println("swapEnds(\"" + input9 + "\"): " + actual9 + " // Expected result: oellH");
String input10 = "t";
String actual10 = wa.swapEnds(input10);
System.out.println("swapEnds(\"" + input10 + "\"): " + actual10 + " // Expected result: t");
// test everyOther
String input11 = "Hello";
String actual11 = wa.everyOther(input11);
System.out.println("everyOther(\"" + input11 + "\"): " + actual11 + " // Expected result: Hlo");
String input12 = "test";
String actual12 = wa.everyOther(input12);
System.out.println("everyOther(\"" + input12 + "\"): " + actual12 + " // Expected result: ts");
// test nonStart
String input13a = "Hello";
String input13b = "There";
String actual13 = wa.nonStart(input13a, input13b);
System.out.println("nonStart(\"" + input13a + "\", \"" + input13b + "\"): " + actual13 + " // Expected result: ellohere");
// test fibonacci
int input14 = 5;
int actual14 = wa.fibonacci(input14);
System.out.println("fibonacci(" + input14 + "): " + actual14 + " // Expected result: 5");
int input15 = 10;
int actual15 = wa.fibonacci(input15);
System.out.println("fibonacci(" + input15 + "): " + actual15 + " // Expected result: 55");
// test luckySum
int input16 = 1;
int input17 = 2;
int input18 = 3;
int actual16 = wa.luckySum(input16, input17, input18);
System.out.println("luckySum(" + input16 + ", " + input17 + ", " + input18 + "): " + actual16 + " // Expected result: 6");
int input19 = 1;
int input20 = 13;
int input21 = 3;
int actual17 = wa.luckySum(input19, input20, input21);
System.out.println("luckySum(" + input19 + ", " + input20 + ", " + input21 + "): " + actual17 + " // Expected result: 1");
// test hasPalindrome
String input22 = "racecar";
boolean actual18 = wa.hasPalindrome(input22);
System.out.println("hasPalindrome(\"" + input22 + "\"): " + actual18 + " // Expected result: true");
String input23 = "hello";
boolean actual19 = wa.hasPalindrome(input23);
System.out.println("hasPalindrome(\"" + input23 + "\"): " + actual19 + " // Expected result: false");
// test powerOfTwo
int input24 = 8;
boolean actual20 = wa.powerOfTwo(input24);
System.out.println("powerOfTwo(" + input24 + "): " + actual20 + " // Expected result: true");
int input25 = 7;
boolean actual21 = wa.powerOfTwo(input25);
System.out.println("powerOfTwo(" + input25 + "): " + actual21 + " // Expected result: false");
}
// I will be following the order provided in the Assignment details.
// reverseDoubleChar - Mirrors a word, as well as, having twice the amount of normal letters.
public String reverseDoubleChar(String word){
String result = ""; // Using another string variable for output.
for(int i = word.length()-1; i >= 0; i--)
{
result += word.charAt(i) + "" + word.charAt(i);
}
return result;
}
// sumDigits - Takes the numerical sum of each individual digit of an integer.
public int sumDigits(int n) {
if (n == 0) {
return 0;
} else {
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
}
// birthdayName - Takes an individuals name and adds it after "Happy Birthday"
public String birthdayName(String name){
return ("Happy Birthday " + name + "!");
}
// missingFront - Returns an input string without the inital 3 letters.
public String missingFront(String str){
return str.substring(3,str.length());
}
// swapEnds - Swaps the first and last letter of a string.
public String swapEnds(String str) {
if (str.length() <= 1) {
return str;
} else {
char first = str.charAt(0);
char last = str.charAt(str.length() - 1);
String middle = str.substring(1, str.length() - 1);
return last + middle + first;
}
}
// everyOther - Takes a string and only outputs 'every other' letter.
public String everyOther(String str) {
String result = ""; // Declaring a new string for the output to go.
for (int i = 0; i < str.length(); i += 2) {
result += str.charAt(i);
}
return result;
}
// nonStart - Combines two strings without their inital character.
public String nonStart(String a, String b) {
if (a.length() < 2 || b.length() < 2) {
return ""; // If there aren't enough characters, return nothing.
} else {
return a.substring(1) + b.substring(1);
}
}
// fibonacci - Returns the nth number of the fibonacci series.
public int fibonacci(int n) {
if (n < 0) {
throw new IllegalArgumentException("Cannot be a negative number."); // Failure Result
} else if (n == 0) {
return 0;
} else if (n == 1) {
return 1;
} else {
int a = 0;
int b = 1;
for (int i = 2; i <= n; i++) {
int c = a + b;
a = b;
b = c;
}
return b;
}
}
// luckySum - combines three integers, but stops if one contains '13'
public int luckySum(int a, int b, int c) {
if (a == 13) {
return 0;
} else if (b == 13) {
return a;
} else if (c == 13) {
return a + b;
} else {
return a + b + c;
}
}
// hasPalindrome - Checks if the following string is a palindrome.(Mirrored statement is same as original)
public boolean hasPalindrome(String str) {
str = str.toLowerCase().replaceAll("[^a-zA-Z0-9]", "");
for (int i = 0; i < str.length(); i++) {
// Odd character palindromes.
int left = i - 1;
int right = i + 1;
while (left >= 0 && right < str.length() && str.charAt(left) == str.charAt(right)) {
return true;
}
// Even character palindromes.
left = i;
right = i + 1;
while (left >= 0 && right < str.length() && str.charAt(left) == str.charAt(right)) {
return true;
}
}
return false;
}
// powerOfTwo - If the square root of the number is even, return true.
public boolean powerOfTwo(int n) {
if (n == 0) {
return false;
} else {
do {
if (n == 1) {
return true;
} else if (n % 2 != 0) {
return false;
}
n /= 2;
} while (true);
}
}
}