Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- eret_ArrayList.java
- //Johnathan Margheret
- //Assignment
- public class StringObj
- {
- private int maxim;
- private String macs = new String();
- public StringObj(String str1)
- {
- maxim=5;
- macs=str1;
- }
- public String toString()
- {
- return macs;
- }
- public int getNumVowels()
- {
- int i = macs.length();
- int count=0;
- for(int k =0;k<i;k++)
- {
- 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
- {
- count++; // Counts the number of vowels
- }
- }
- return count;
- }
- public boolean areAlphabetized(StringObj str)
- {
- if(str.macs.toLowerCase().compareTo(this.macs.toLowerCase())>=0)//smooth and efficient way to make sure it is in alphabetical order
- return true;
- else
- return false;
- }
- public boolean isPalindrome()
- {
- String test = "";
- for(int i = this.macs.length();i>0;i--)
- {
- test += this.macs.substring(i-1,i).toLowerCase();//makes a copy of this but puts it in reverse order
- }
- // System.out.println(test + "\n" +copy+ "\n" +this.macs);
- if(this.macs.toLowerCase().equals(test))//checks to see if the reverse = the original
- return true;
- else
- return false;
- }
- public boolean isPalindromeBONUS()
- {
- String copy = "";
- String test = "";
- String cop = "";
- for(int k = 0;k<this.macs.length();k++)
- {
- cop = this.macs.substring(k,k+1).toLowerCase();//makes a copy of this
- }
- for(int i = this.macs.length();i>0;i--)
- {
- test = this.macs.substring(i-1,i).toLowerCase();//test is the individual letter or symbol
- if(test.equals(" ")||test.equals(",")||test.equals(".")||test.equals(";")||test.equals(":")||test.equals("'"))
- {
- //smoothly eliminates all symbols from sentence
- }
- else
- copy+=test.toLowerCase();//makes a copy of string with no spaces or symbols
- // System.out.println(copy);
- }
- String test1 = "";
- for(int i = copy.length();i>0;i--)
- {
- test1 += copy.substring(i-1,i).toLowerCase();//does what isPalindrome() does
- }
- // System.out.println("copy: " +copy+ " \nTest: "+test+"\ntest1: " +test1);
- if(copy.equals(test1))
- return true;
- else
- return false;
- }
- public int wordCount(String str)
- {
- int count = 0; // make sure string being looked for is made lowercase along with everyhting else.
- String maxter ="";
- for(int k = 0;k<str.length();k++)
- maxter+= str.substring(k,k+1).toLowerCase();
- String dup = " ";
- String check = "";
- for(int i = 0; i<this.macs.length();i++)
- {
- if(this.macs.substring(i,i+1).toLowerCase()==".")//replaces period with space
- dup = " ";
- else
- dup+=this.macs.substring(i,i+1).toLowerCase();//copy of this
- // System.out.println("dup: " + dup);
- }
- for(int c = 0; c<dup.length();c++)
- {
- if(dup.substring(c,c+1).equals(" "))
- {
- if(check.equals(maxter))
- count++;
- check = "";
- }
- else
- check += dup.substring(c,c+1);
- }
- // for(int p = 0; p<dup.length()-str.length();p++)
- // {
- //
- // if(maxter.equals(dup.substring(p,p+str.length())))
- // {
- // System.out.println(maxter);
- // if(dup.substring(p+1+maxter.length(),p+2+maxter.length()).equals(" ")&&dup.substring(p-1,p).equals(" "))
- // count++;
- // }
- // }
- return count;
- }
- public int wordCountBONUS(String str)
- {
- int count = 0; // make sure string being looked for is made lowercase along with everyhting else.
- String maxter ="";
- for(int k = 0;k<str.length();k++)
- maxter+= str.substring(k,k+1).toLowerCase();
- String dup = " ";
- for(int i = 0; i<this.macs.length();i++)
- {
- if(this.macs.substring(i,i+1).toLowerCase()==".")
- dup = " ";
- else
- dup+=this.macs.substring(i,i+1).toLowerCase();
- // System.out.println("dup: " + dup);
- }
- for(int p = 0; p<dup.length()-str.length();p++)
- {
- if(maxter.equals(dup.substring(p,p+str.length())))
- {
- count++;
- }
- }
- return count;
- }
- public void displayAnalysisBONUS()
- {
- String copy = "";
- int count = 0;// for number of words
- for(int k = 0;k<this.macs.length();k++)
- {
- copy = this.macs.substring(k,k+1).toLowerCase();//makes a copy of this
- }
- for(int p = 0; p<copy.length();p++)//NUMBER OF WORDS
- {
- if(copy.substring(p,p+1).equals(" "))
- count++;
- }
- System.out.println("\n1.\tThere are "+count+" Words in the object.");
- //////////////////////////////////////////////////
- //Finding the number of different words!!
- int counter = 0;
- String poop= "";
- String check ="";
- for(int c = 0; c<copy.length();c++)
- {
- if(copy.substring(c,c+1).equals(" "))
- {
- if(check.equals(poop))
- {
- }
- else
- counter++;
- for(int d = 0; d<check.length();d++)
- {
- poop+= check.substring(d,d+1);
- }
- check = "";
- }
- else
- check += copy.substring(c,c+1);
- }
- System.out.println("\n2.\tThere are " +counter+ " unique words.");
- /////////////////////////////////////////////////////
- //Finds The Longest Word/Words and the number of letters
- int add = 0;
- String pooper = "";
- String swag = "";
- String longest ="";
- for(int c = 0; c<copy.length();c++)
- {
- if(copy.substring(c,c+1).equals(" "))
- {
- if(swag.length()>pooper.length())
- {
- for(int d = 0; d<swag.length();d++)
- {
- longest+= swag.substring(d,d+1);
- }
- }
- else
- add++;
- for(int d = 0; d<swag.length();d++)
- {
- pooper+= swag.substring(d,d+1);
- }
- swag = "";
- }
- else
- swag += copy.substring(c,c+1);
- }
- System.out.println ("\n3.\tThe longest word is "+longest+" and it is "+longest.length()+"letters long.");
- //////////////////////////////////////////////////////////
- //The Shortest word and it length
- int add1 = 0;
- String pooper1 = "";
- String swag1 = "";
- String shortest ="";
- for(int c = 0; c<copy.length();c++)
- {
- if(copy.substring(c,c+1).equals(" "))
- {
- if(swag.length()>pooper1.length())
- {
- for(int d = 0; d<swag1.length();d++)
- {
- shortest+= swag.substring(d,d+1);
- }
- }
- else
- add1++;
- for(int d = 0; d<swag1.length();d++)
- {
- pooper1+= swag1.substring(d,d+1);
- }
- swag1 = "";
- }
- else
- swag1 += copy.substring(c,c+1);
- System.out.println("\n4.\tThe shortest word is " +shortest+"and it is " +shortest.length()+"letters long.");
- }
- ////////////////////////////////////////////////////////////
- //Alphabetize!
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement