Advertisement
Guest User

B

a guest
Oct 24th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. /**
  2. * Write a description of class StringsAndThings here.
  3. * @author Taarush
  4. */
  5. public class StringsAndThings{
  6. private String word;
  7. /**
  8. * Constructor for objects of class StringsAndThings
  9. * @param str is the String input
  10. */
  11. public StringsAndThings(String str){
  12. word = str;
  13. }
  14. /**
  15. * Counts how many of the characters in the string are not letters
  16. * @return the number of characters in the string are not letters
  17. */
  18. public int countNonLetters(){
  19. int counter = 0;
  20. for (int i = 0; i < word.length(); i++){
  21. if (Character.isLetter(word.charAt(i))){
  22. counter = counter + 1;
  23. }
  24. }
  25. int nonLetters =word.length() - counter;
  26. return nonLetters;
  27. }
  28.  
  29. /**
  30. Tests whether a letter is a vowel
  31. @param letter a string of length 1
  32. @return true if letter is a vowel
  33. */
  34. public boolean isVowel(String let){
  35. String stringToCheck = "aeiouyAEIOUY";
  36. return (stringToCheck.contains(let));
  37. }
  38.  
  39. /**
  40. * Finds whether a word has more vowels or consonants
  41. * @return returns if the string has more vowels
  42. */
  43. public boolean moreVowels(){
  44. int k = 0;
  45. int vowels = 0,consonants = 0;
  46. boolean test = true;
  47. String testLetter = word.substring(k, k + 1);
  48. while(k < word.length()){
  49. if(isVowel(testLetter)){
  50. vowels = vowels +1;
  51. }
  52. else
  53. {
  54. consonants = consonants + 1;
  55. }
  56. k = k+1;
  57. {
  58. if(consonants > vowels){
  59. test = false;
  60. }
  61. }
  62. }
  63. return test;
  64. }
  65.  
  66. /**
  67. * Returns a string where each character in the phrase occurs once
  68. * @return the string mentioned
  69. */
  70. public String noDuplicates(){
  71. String str = "";
  72. for (int i = 0; i < word.length(); i++){
  73. if (str.contains(word.substring(i,i+1)) == false)
  74. {
  75. str = str + word.charAt(i);
  76. }
  77. }
  78. return str;
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement