-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWEEK 1-Word Play
82 lines (62 loc) · 2.07 KB
/
WEEK 1-Word Play
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
1. # Write a method isVowel that has one Char parameter named ch.
# This method returns true if ch is a vowel (one of 'a', 'e', 'i', 'o', or 'u' or the uppercase versions) and false otherwise.
# You should write a tester method to see if this method works correctly.
public class WordPlay {
public boolean isVowel(char ch){
ch=Character.toLowerCase(ch);
if (ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
return true;
else
return false;
}
public void testisVowel(){
char ch='o';
boolean result=isVowel(ch);
System.out.println(result);
}}
2.
# Write a method replaceVowels that has two parameters, a String named phrase and a Char named ch.
# This method should return a String that is the string phrase with all the vowels (uppercase or lowercase) replaced by ch.
# Be sure to call the method isVowel that you wrote and also test this method.
public String replaceVowels(String phrase, char ch){
String a="";
for (int i=0; i<phrase.length(); i++){
if (isVowel(phrase.charAt(i))==true)
a=a+""+ch;
else
a=a+phrase.charAt(i);
}
return a;
}
public void testReplaceVowels(){
String a="sehytkziuewq";
char ch='*';
String b=replaceVowels(a,ch);
System.out.println(b);
}
3.
# Write a method emphasize with two parameters, a String named phrase and a character named ch.
# This method should return a String that is the string phrase but with the Char ch (upper- or lowercase) replaced by
# ‘*’ if it is in an odd number location in the string (first character has index 0, third character has index 2, etc.),
# or ‘+’ if it is in an even number location in the string (second character has index 1, fourth character has index 3, etc.).
public String emphasize(String phrase, char ch){
phrase=phrase.toLowerCase();
String a="";
for(int i=0;i<phrase.length(); i++){
if (phrase.charAt(i)!=ch)
a=a+phrase.charAt(i);
else if(phrase.charAt(i)==ch){
if(i%2==0)
a=a+'*';
if(i%2!=0)
a=a+'+';
}
}
return a;
}
public void testEmphasize(){
String a="sehytkziuewq";
char ch='e';
String b=emphasize(a,ch);
System.out.println(b);
}