clanecollege

Group Assignment Solution

May 11th, 2012
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.93 KB | None | 0 0
  1. public class GroupProject
  2.  
  3. {
  4.  
  5.     public static char[] stringToChar(String word)
  6.  
  7.     {
  8.  
  9.         char[] allLetters = word.toCharArray();
  10.         return allLetters;
  11.  
  12.     }
  13.  
  14.     public static int countLettersInString(String word)
  15.  
  16.     {
  17.  
  18.         word = word.replaceAll(" ", "");
  19.  
  20.         int counter = word.length();
  21.         return counter;
  22.  
  23.     }
  24.  
  25.     public static String weHateTheLetterE(String word)
  26.  
  27.     {
  28.  
  29.         word = word.replaceAll("e", "");
  30.  
  31.         word = word.replaceAll("E", "");
  32.  
  33.         return word;
  34.  
  35.     }
  36.  
  37.     public static String weLoveTheLetterS(String word)
  38.  
  39.     {
  40.  
  41.  
  42.         word = word.replaceAll(" ", "S");
  43.  
  44.         return word;
  45.  
  46.     }
  47.  
  48.     public static int pIndexAdder(String word)
  49.  
  50.     {
  51.  
  52.         int total = 0;
  53.  
  54.         char[] letters = word.toCharArray();
  55.  
  56.         for (int i = 0; i < word.length(); i++)
  57.  
  58.         {
  59.  
  60.             if (letters[i] == 'p' || letters[i] == 'P')
  61.  
  62.             {
  63.  
  64.                 total = total + i;
  65.  
  66.             }
  67.  
  68.         }
  69.  
  70.         return total;
  71.  
  72.     }
  73.  
  74.     public static void main(String[] args)
  75.  
  76.     {
  77.  
  78.         // pass "I love java programming" to the stringToChar method and print//
  79.         // the returned array// Define String Variable
  80.  
  81.         // Q1
  82.         System.out.print("Q1: ");
  83.         String testToChar = "I love java programming";
  84.  
  85.         char[] stringArray = stringToChar(testToChar);
  86.        
  87.         for (int i = 0; i < stringArray.length; i++)
  88.  
  89.         {
  90.  
  91.             System.out.println(stringArray[i]);
  92.  
  93.         }
  94.  
  95.         // Q2
  96.  
  97.         String testToLetter = "I love java programming";
  98.         System.out.println("Q2: " + countLettersInString(testToLetter));
  99.  
  100.         // Q3
  101.  
  102.         String TheLetterE = "If only the letter E didn't exist - I'd be ecstatic";
  103.  
  104.         System.out.println("Q3: " + weHateTheLetterE(TheLetterE));
  105.  
  106.         // Q4
  107.  
  108.         String LoveS = "I'd love it if every sentence that contained the letter s where the spaces are supposed to be";
  109.         System.out.println("Q4: " + weLoveTheLetterS(LoveS));
  110.  
  111.         // Q5
  112.  
  113.         String CountP = "P is Pretty Well Pindexed Pin This P Strong String";
  114.         System.out.println("Q5: Amount of P's in String: "+pIndexAdder(CountP));
  115.  
  116.     }
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment