Advertisement
JohnathanM4

StringObj

Nov 1st, 2015
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.47 KB | None | 0 0
  1. eret_ArrayList.java
  2. //Johnathan Margheret
  3. //Assignment
  4.  
  5. public class StringObj
  6. {
  7.  
  8. private int maxim;
  9. private String macs = new String();
  10. public StringObj(String str1)
  11. {
  12. maxim=5;
  13. macs=str1;
  14. }
  15. public String toString()
  16. {
  17. return macs;
  18. }
  19.  
  20. public int getNumVowels()
  21. {
  22.  
  23. int i = macs.length();
  24. int count=0;
  25. for(int k =0;k<i;k++)
  26. {
  27. if(macs.charAt(k)== 'a'||macs.charAt(k)=='e'||macs.charAt(k)=='i'||macs.charAt(k)=='o'||macs.charAt(k)=='u')//checks to see if it a vowel
  28. {
  29. count++; // Counts the number of vowels
  30. }
  31.  
  32. }
  33. return count;
  34. }
  35. public boolean areAlphabetized(StringObj str)
  36. {
  37. if(str.macs.toLowerCase().compareTo(this.macs.toLowerCase())>=0)//smooth and efficient way to make sure it is in alphabetical order
  38. return true;
  39. else
  40. return false;
  41.  
  42. }
  43. public boolean isPalindrome()
  44. {
  45.  
  46.  
  47. String test = "";
  48. for(int i = this.macs.length();i>0;i--)
  49. {
  50. test += this.macs.substring(i-1,i).toLowerCase();//makes a copy of this but puts it in reverse order
  51. }
  52. // System.out.println(test + "\n" +copy+ "\n" +this.macs);
  53. if(this.macs.toLowerCase().equals(test))//checks to see if the reverse = the original
  54. return true;
  55. else
  56. return false;
  57. }
  58. public boolean isPalindromeBONUS()
  59. {
  60. String copy = "";
  61. String test = "";
  62. String cop = "";
  63. for(int k = 0;k<this.macs.length();k++)
  64. {
  65. cop = this.macs.substring(k,k+1).toLowerCase();//makes a copy of this
  66. }
  67. for(int i = this.macs.length();i>0;i--)
  68. {
  69. test = this.macs.substring(i-1,i).toLowerCase();//test is the individual letter or symbol
  70. if(test.equals(" ")||test.equals(",")||test.equals(".")||test.equals(";")||test.equals(":")||test.equals("'"))
  71. {
  72. //smoothly eliminates all symbols from sentence
  73. }
  74. else
  75. copy+=test.toLowerCase();//makes a copy of string with no spaces or symbols
  76. // System.out.println(copy);
  77.  
  78. }
  79. String test1 = "";
  80. for(int i = copy.length();i>0;i--)
  81. {
  82. test1 += copy.substring(i-1,i).toLowerCase();//does what isPalindrome() does
  83. }
  84. // System.out.println("copy: " +copy+ " \nTest: "+test+"\ntest1: " +test1);
  85. if(copy.equals(test1))
  86. return true;
  87. else
  88. return false;
  89. }
  90. public int wordCount(String str)
  91. {
  92. int count = 0; // make sure string being looked for is made lowercase along with everyhting else.
  93. String maxter ="";
  94. for(int k = 0;k<str.length();k++)
  95. maxter+= str.substring(k,k+1).toLowerCase();
  96. String dup = " ";
  97. String check = "";
  98. for(int i = 0; i<this.macs.length();i++)
  99. {
  100. if(this.macs.substring(i,i+1).toLowerCase()==".")//replaces period with space
  101. dup = " ";
  102. else
  103. dup+=this.macs.substring(i,i+1).toLowerCase();//copy of this
  104. // System.out.println("dup: " + dup);
  105. }
  106. for(int c = 0; c<dup.length();c++)
  107. {
  108. if(dup.substring(c,c+1).equals(" "))
  109. {
  110. if(check.equals(maxter))
  111. count++;
  112. check = "";
  113. }
  114. else
  115. check += dup.substring(c,c+1);
  116. }
  117. // for(int p = 0; p<dup.length()-str.length();p++)
  118. // {
  119. //
  120. // if(maxter.equals(dup.substring(p,p+str.length())))
  121. // {
  122. // System.out.println(maxter);
  123. // if(dup.substring(p+1+maxter.length(),p+2+maxter.length()).equals(" ")&&dup.substring(p-1,p).equals(" "))
  124. // count++;
  125. // }
  126. // }
  127. return count;
  128. }
  129. public int wordCountBONUS(String str)
  130. {
  131. int count = 0; // make sure string being looked for is made lowercase along with everyhting else.
  132. String maxter ="";
  133. for(int k = 0;k<str.length();k++)
  134. maxter+= str.substring(k,k+1).toLowerCase();
  135. String dup = " ";
  136. for(int i = 0; i<this.macs.length();i++)
  137. {
  138. if(this.macs.substring(i,i+1).toLowerCase()==".")
  139. dup = " ";
  140. else
  141. dup+=this.macs.substring(i,i+1).toLowerCase();
  142. // System.out.println("dup: " + dup);
  143. }
  144. for(int p = 0; p<dup.length()-str.length();p++)
  145. {
  146.  
  147. if(maxter.equals(dup.substring(p,p+str.length())))
  148. {
  149. count++;
  150. }
  151. }
  152. return count;
  153. }
  154. public void displayAnalysisBONUS()
  155. {
  156. String copy = "";
  157. int count = 0;// for number of words
  158. for(int k = 0;k<this.macs.length();k++)
  159. {
  160. copy = this.macs.substring(k,k+1).toLowerCase();//makes a copy of this
  161. }
  162. for(int p = 0; p<copy.length();p++)//NUMBER OF WORDS
  163. {
  164. if(copy.substring(p,p+1).equals(" "))
  165. count++;
  166.  
  167. }
  168. System.out.println("\n1.\tThere are "+count+" Words in the object.");
  169. //////////////////////////////////////////////////
  170. //Finding the number of different words!!
  171. int counter = 0;
  172. String poop= "";
  173. String check ="";
  174. for(int c = 0; c<copy.length();c++)
  175. {
  176. if(copy.substring(c,c+1).equals(" "))
  177. {
  178. if(check.equals(poop))
  179. {
  180. }
  181. else
  182. counter++;
  183. for(int d = 0; d<check.length();d++)
  184. {
  185. poop+= check.substring(d,d+1);
  186. }
  187.  
  188. check = "";
  189. }
  190. else
  191. check += copy.substring(c,c+1);
  192. }
  193. System.out.println("\n2.\tThere are " +counter+ " unique words.");
  194. /////////////////////////////////////////////////////
  195. //Finds The Longest Word/Words and the number of letters
  196. int add = 0;
  197. String pooper = "";
  198. String swag = "";
  199. String longest ="";
  200. for(int c = 0; c<copy.length();c++)
  201. {
  202. if(copy.substring(c,c+1).equals(" "))
  203. {
  204. if(swag.length()>pooper.length())
  205. {
  206. for(int d = 0; d<swag.length();d++)
  207. {
  208. longest+= swag.substring(d,d+1);
  209. }
  210. }
  211. else
  212. add++;
  213. for(int d = 0; d<swag.length();d++)
  214. {
  215. pooper+= swag.substring(d,d+1);
  216. }
  217.  
  218. swag = "";
  219. }
  220. else
  221. swag += copy.substring(c,c+1);
  222.  
  223. }
  224. System.out.println ("\n3.\tThe longest word is "+longest+" and it is "+longest.length()+"letters long.");
  225. //////////////////////////////////////////////////////////
  226. //The Shortest word and it length
  227. int add1 = 0;
  228. String pooper1 = "";
  229. String swag1 = "";
  230. String shortest ="";
  231. for(int c = 0; c<copy.length();c++)
  232. {
  233. if(copy.substring(c,c+1).equals(" "))
  234. {
  235. if(swag.length()>pooper1.length())
  236. {
  237. for(int d = 0; d<swag1.length();d++)
  238. {
  239. shortest+= swag.substring(d,d+1);
  240. }
  241. }
  242. else
  243. add1++;
  244. for(int d = 0; d<swag1.length();d++)
  245. {
  246. pooper1+= swag1.substring(d,d+1);
  247. }
  248.  
  249. swag1 = "";
  250. }
  251. else
  252. swag1 += copy.substring(c,c+1);
  253. System.out.println("\n4.\tThe shortest word is " +shortest+"and it is " +shortest.length()+"letters long.");
  254. }
  255. ////////////////////////////////////////////////////////////
  256. //Alphabetize!
  257.  
  258. }
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement