-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathLoopFun.java
More file actions
78 lines (69 loc) · 2.5 KB
/
LoopFun.java
File metadata and controls
78 lines (69 loc) · 2.5 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
import java.util.ArrayList;
public class LoopFun
{
/**
* Given a number, return the factorial of that number.
* For example, given 5, the factorial is 5 x 4 x 3 x 2 x 1 which should return 120.
* @param number
* @return the factorial of the number
*/
public Integer factorial(Integer number){
int fact = 1;
for (int i = 1; i <= number; i++) {fact = fact * i;}
return fact;
}
/**
* Given a phrase, get the acronym of that phrase. Acronym is the combination of
* the first character of each word in upper case.
* For example, given "Ruby on Rails", this method will return "ROR"
* @param phrase
* @return Upper case string of the first letter of each word
*/
public String acronym(String phrase) {
String[] words = phrase.split("\\W+");
String output = "";
for (int i = 0; i <words.length; i++) {
// System.out.println(words[i]); // tests if words are being split
output += words[i].charAt(0);
}
return output.toUpperCase();
/*
char c = phrase.charAt(0);
String output = phrase;
String OutPut = null;
for (int i = 1; i < phrase.length(); i++) {
if (Character.isWhitespace(phrase.charAt(i))) {
output = output + phrase.charAt(i);
output += phrase.charAt(i+1);
}
OutPut = output.replaceAll("\\s+","").toUpperCase();
}
return OutPut;
*/
}
/**
* To prevent anyone from reading our messages, we can encrypt it so it will only be readable by its
* intended audience. This method encrypt the message by shifting the letter by 3 characters. If the character is
* at the end of the alphabet, it will wraps around.
* For example:
* 'a' => 'd'
* 'w' => 'z'
* 'x' => 'a'
* 'y' => 'b'
* @param word
* @return the encrypted string by shifting each character by three character
*/
public String encrypt(String word) {
char[] pass = word.toCharArray();
int shiftVal = 3;
for (int i = 0; i < word.length(); i++) {
if (pass[i] >= 120) {
pass[i] -= 23;
} else {
pass[i] += shiftVal;
}
}
String password = new String(pass);
return password;
}
}