Java methods: solutions for in-class exercises.

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.


public class Methods {
	public static void main(String[] args) {
		System.out.println(interleave('v', "dog"));
		System.out.println(interleave('v', ""));
		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");
		System.out.println(replace('c', '*', "A cat is cute"));
		System.out.println(hasCharacter('y', "heyooo"));
		System.out.println(hasCharacterLoop('y', "heyooo"));
		System.out.println(hasCharacterLoop('y', ""));
		printEveryOther("hahaha");
	}

	/**
	 * 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;
	}
	
	/**
	Parameters: char, String.
	The method 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*".
	**/
	public static String interleave( char x , String word){
		String newS = "";
		for (int i = 0; i < word.length(); i++){
			newS = newS + word.charAt(i) + x;
			
		}
		//System.out.println(newS);
		return newS;
	}
	
	/**
	Parameters: String, char, char.
	The method 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"
	**/
	public static String replace(char x, char y, String str) {
		String newstring = "";
		for(int r = 0; r < str.length(); r++){
			if(str.charAt(r) == x){
				newstring = newstring + y;
			}
			else {
				newstring = newstring + str.charAt(r);
			}
		}
		return newstring; 
	}
	
	/**
	Parameters: string, char
	The methods takes a string and a character and returns true if the 
	character occurs in the string and false otherwise.
	**/
	
	public static boolean hasCharacter(char x, String str){
		return(str.indexOf(x) != -1);
	}
	
	public static boolean hasCharacterLoop(char x, String str){
		boolean b = false;
		for (int i = 0; i < str.length(); i++) {
			if(str.charAt(i) == x) {
				//return true;
				b = true;
			} 
		}
		//return false;
		return b;
	}
	
	/**
	Parameters: a string
	The method takes a string and prints its every other character, 
	starting with the first one.
	**/
	
	public static void printEveryOther(String str){
		for (int i = 0; i < str.length(); i +=2){
			System.out.print(str.charAt(i));
		}
		System.out.println();
	}
}

CSci 2101 course web site.