Java methods: solutions for in-class exercises.


public class Methods {
	public static void main(String[] args) {

		printNumbers(1, 10, 2);
		printNumbers(-10, 0, 1);

		String str = "aardvarks don't eat bananas";

		System.out.println("The string \"" + str + "\" has " + countA(str)
				+ " letters a");
		
		String s = interleave("abcd", '*');
		String t = replace("a cat is cute",'c','*');
		System.out.println(s);
		System.out.println(t);
	}

	/**
	 * Parameters: three integers: start, bound, and skip. The method prints
	 * numbers starting at start, no larger than bound, incrementing by skip.
	 * The numbers are printed on a single line, then a new line is printed.
	 **/
	public static void printNumbers(int start, int bound, int skip) {
		for (int i = start; i <= bound; i = i + skip) {
			System.out.print(i + " ");
		}
		System.out.println();
	}

	/**
	 * Parameters: String s. The method takes a String and returns the number of
	 * lower-case letters 'a' in the string.
	 **/
	public static int countA(String s) {
		int count = 0;
		for (int j = 0; j < s.length(); j++) {
			if (s.charAt(j) == 'a') {
				count++;
			}
		}
		return count;
		
	}

	public static String interleave(String m, char n) {
		String p = "";
		for (int j=0; j<m.length(); j++) {
			p = p + m.charAt(j) + n;
		} 
		return p;
	}
	
	public static String replace(String str, char a, char b){
		String str2 = "";
		for(int i=0; i<str.length(); i++){
			if(str.charAt(i) == a){
				str2 = str2 + b;
			}else{
				str2 = str2 + str.charAt(i);
			}
		}
		return str2;
	}
}

Exercises

Write a method interleave that takes a character and a string and returns a string that has the character interleaved after each character in the given string. For instance, interleave("abc", '*') returns "a*b*c*".

Write a method replace that takes a string and two characters and returns a string in which every occurrence of the first character is replaced by the second one. For instance, replace("a cat is cute", 'c', '*') results in "a *at is *ute"

Write a method hasCharacter that takes a string and a character and returns true if the character occurs in the string and false otherwise.

Write a method printEveryOther that takes a string and prints its every other character, starting with the first one.


CSci 2101 course web site.