Question

In: Computer Science

USING JAVA Consider the following methods: StringBuilder has a method append(). If we run: StringBuilder s...

USING JAVA

Consider the following methods:
StringBuilder has a method append(). If we run:
StringBuilder s = new StringBuilder();
s.append("abc");
The text in the StringBuilder is now "abc"
Character has static methods toUpperCase() and toLowerCase(), which convert characters to upper or lower case. If we run Character x = Character.toUpperCase('c');, x is 'C'.
Character also has a static isAlphabetic() method, which returns true if a character is an alphabetic character, otherwise returns false.
You will also need String's charAt() method, which returns the character at a given index in the String. For example, "Godzilla".charAt(1) returns 'o'.

Write an application as follows:
public static String getNonAlpha() takes a String as parameter, builds a StringBuilder consisting of only the nonalphabetic characters in the String, and returns a String based on the StringBuilder (eg, sb.toString())
public static String getUpper() takes a String, builds a StringBuilder of the upper case versions of all the alphabetic characters in the String, and returns a String based on the StringBuilder.
Write JUnit tests to verify that the above methods are correct

Solutions

Expert Solution

Here is the completed code for this problem including JUnit tests. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// StringBufferMethods.java

import static org.junit.Assert.*;

import org.junit.Test;

public class StringBufferMethods {

      // returns a String containing all non-alphabetic characters in text

      public static String getNonAlpha(String text) {

            // creating a StringBuffer

            StringBuffer buffer = new StringBuffer();

            // looping through each character

            for (int i = 0; i < text.length(); i++) {

                  // if current character is not a letter, appending to buffer

                  if (!Character.isLetter(text.charAt(i))) {

                        buffer.append(text.charAt(i));

                  }

            }

            return buffer.toString();

      }

      public static String getUpper(String text) {

            // creating a StringBuffer

            StringBuffer buffer = new StringBuffer();

            // looping through each character

            for (int i = 0; i < text.length(); i++) {

                  // if current character is a letter, converting to upper case and

                  // appending to buffer

                  if (Character.isLetter(text.charAt(i))) {

                        buffer.append(Character.toUpperCase(text.charAt(i)));

                  }// note: this method ignores all other characters

            }

            return buffer.toString();

      }

      // JUnit test methods to test the above two methods

      @Test

      public void testGetNonAlpha() {

            // testing normal case

            assertEquals(getNonAlpha("abcd#$.ef12XX"), "#$.12");

            // testing a String that has no non-alphabetic characters

            assertEquals(getNonAlpha("abcd"), "");

            // testing a String that has only non-alphabetic characters

            assertEquals(getNonAlpha("..@@@##"), "..@@@##");

            // testing an empty String

            assertEquals(getNonAlpha(""), "");

      }

      @Test

      public void testGetUpper() {

            // testing normal case

            assertEquals(getUpper("ab.12@Dce"), "ABDCE");

            // testing a String containing only lower case characters

            assertEquals(getUpper("xyz"), "XYZ");

            // testing a String containing only numbers

            assertEquals(getUpper("1234"), "");

            // testing a String containing only upper case characters

            assertEquals(getUpper("ABC"), "ABC");

            // testing an empty String

            assertEquals(getUpper(""), "");

      }

}


Related Solutions

created a new StringBuilder sb using the following index: StringBuilder sb = new StringBuilder ("Welcome To...
created a new StringBuilder sb using the following index: StringBuilder sb = new StringBuilder ("Welcome To "); What would be sb after each of the following statements? Explain the methods used and how they change the content sb.append("Java"); sb.insert(11, "HTML and ");
java 1.) a method to append a 2d double array at the right of another 2d...
java 1.) a method to append a 2d double array at the right of another 2d double array, return a new array. E.g. numss1: {{1, 2}, {3, 4, 5}, {6}}, numss2: {{7}, {8, 9}}, return {{1, 2, 7}, {3, 4, 5, 8, 9}, {6}}
Write the following methods in a Java project: a) A Java method to determine and return...
Write the following methods in a Java project: a) A Java method to determine and return the sum of first three numbers, where three numbers are received as parameters. b) A Java method to determine and return the highest of N integers. The number of integers is received as a parameter. The method should prompt the user to enter the N numbers, then it return the highest. c) A Java method to determine and return an appropriate value indicating if...
Java Palindrome Use StringBuilder concept to create a palindrome checker.   Method should be called pCheck. Must...
Java Palindrome Use StringBuilder concept to create a palindrome checker.   Method should be called pCheck. Must use var-arg concept.   Class name for this program is Palindrome.   Must pass this test:: public class PalindromeTest { public static void main(String[] args) { Palindrome palindrome = new Palindrome(); boolean answer = palindrome.pCheck("racecar", "Bell", "elle", "bunny"); System.out.println(“These are palindromes: " + answer); } }
COP 2800, Java Programming Assignment 6-1 Using Java create a program using the “Methods” we covered...
COP 2800, Java Programming Assignment 6-1 Using Java create a program using the “Methods” we covered this week. come up with a “problem scenario” for which you can create a “solution”. Utilize any/all of the examples from the book and class that we discussed. Your program should be interactive and you should give a detailed statement about what the “description of the program – purpose and how to use it”. You should also use a “good bye” message. Remember to...
Implement the following methods in Java: a. A method named MergeFileswith the following signature that gets...
Implement the following methods in Java: a. A method named MergeFileswith the following signature that gets two file names and write the content of the first file (sourceFile) into the beginning of the second file (destinationFile. For example, if sourceFile contains “Hello ” and destinationFile contains “World!”, this method must keep sourceFile as it is, but replace the content of the second file by “Hello World!”.public static voidMergeFiles(String sourceFile, String destinationFile) b. A recursive method with the following signature that...
Consider a stick of unit length, broken into 3 pieces using the following methods: 1. We...
Consider a stick of unit length, broken into 3 pieces using the following methods: 1. We chose randomly and independently two points on the stick using a uniform PDF, and we break stick at these 2 points 2. We break the stick at a random point chosen by using a uniform PDF, and then we break the piece that contains the right end of the stick, at a random point chosen by using a uniform PDF 3. We break the...
Write a recursive method using Java that takes a string s as input and returns a...
Write a recursive method using Java that takes a string s as input and returns a list that contains all the anagrams of the string s. An anagram is a word formed by rearranging the letters of a different word. For instance, the word ‘cat’ is an anagram of ‘act’. Notice that the output list cannot contain duplicates.
Code using JAVA: must include a "Main Method" to run on "intelliJ". Hint: Use a hash...
Code using JAVA: must include a "Main Method" to run on "intelliJ". Hint: Use a hash table. You can use either Java HashTable or Java HashMap. Refer to Java API for the subtle differences between HashTable and HashMap. Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules: Each row must contain the digits 1-9 without repetition. Each column must contain the digits 1-9 without repetition. Each...
Code using JAVA: must include a "Main Method" to run on "intelliJ". Hint: Use recursion "...
Code using JAVA: must include a "Main Method" to run on "intelliJ". Hint: Use recursion " /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public boolean isSymmetric(TreeNode...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT