Prog04.java - *My name is Elisha Steiner and the due date...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
/*My name is Elisha Steiner and the due date is 4/11/16 The purpose of this is to create 25 different methods plus on bonus method that perform different tasks. A separate program was created to test each method with their own main. */ import java.util.*; public class Prog04 { // 1. int addChars(String) public static int addChars( String s ) { int x = 0; for (int i = 0; i<s.length();i++){ x= x + s.charAt(i); } return x; } // 2. int addSomeChars(String, String) public static int addSomeChars(String s, String kb) { int y = 0; for (int i = 0; i<s.length();i++){ boolean x = true; char p = s.charAt(i); for (int j = 0; j < kb.length(); j++){ char o = kb.charAt(j); if ( p == o ){x = false;} } if (x) { y += s.charAt(i);} } return y; } // 3. accepts a string and returns whether its a palindrome public static boolean isPalindrome (String s) { int n = s.length(); for (int i = 0; i < (n / 2) + 1; ++i) { if (s.charAt(i) != s.charAt(n - i - 1)) { return false; } } return true; } // 4. accepts a string and returns the string in reverse public static String reverse(String s){ String x = ""; for (int i = s.length() - 1; i >= 0; i--){ x = x + s.charAt(i); } return x; } // 5. takes a string and returns a jumbled string with those letters public static String jumble(String s) { String mix = ""; String again = ""; int x = 0; int y = 0; if (s.length() > 3){ for (int i = 1; i <= s.length()-1; i++){ char l = s.charAt(i); again = again + l; } Random rand = new Random(); int random1 = rand.nextInt(again.length())+1; int random2 = rand.nextInt(again.length())+1;
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
while (random1 == random2){ random2 = rand.nextInt(again.length())+1; } if (random1>random2) { x = random1; y = random2; } else { x = random2; y = random1; } for (int i = 0; i<y; i++){ char l = s.charAt(i); mix = mix + l; } mix = mix + s.charAt(x); for (int i = y+1; i<x; i++){ char l = s.charAt(i); mix = mix + l; } mix = mix + s.charAt(y); for (int i = x+1; i<s.length()-1; i++){ char l = s.charAt(i); mix = mix + l; } mix = mix + s.charAt(s.length()-1); } else {mix = s;} return mix; } // 6. gets a string and swaps the adjacent pairs of letters around public static String swapPairs (String s) { String mix = ""; for ( int i = 0; i<s.length()-1; i+=2 ){ char x = s.charAt(i); char y = s.charAt(i+1); mix = mix + y + x; } if (s.length()%2==1){mix = mix + s.charAt(s.length()-1); } return mix; } // 7. accepts a string and returns the number of vowels
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern