Question

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 to LowerCase(), 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.
  • main() asks the user for input (using a Scanner) and prints the results of running the input String through each of these methods.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

public class SBTester {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter sentense");
       String str = sc.nextLine();
       String nonAlpha = getNonAlpha(str);
       String upper = getUpper(str);
       System.out.println("Non Alpha : " + nonAlpha);
       System.out.println("Uppercase : " + upper);
   }

   private static String getUpper(String str) {
       StringBuffer sb = new StringBuffer();
       // looping through the string and appending to string buffer if it is uppercase
       for (int i = 0; i < str.length(); i++) {
           if (Character.isUpperCase(str.charAt(i)))
               sb.append(str.charAt(i));
       }
       return sb.toString();
   }

   private static String getNonAlpha(String str) {
       StringBuffer sb = new StringBuffer();
       // looping through the string and appending to string buffer if it is non alphabetic
       for (int i = 0; i < str.length(); i++) {
           if (!Character.isAlphabetic(str.charAt(i)))
               sb.append(str.charAt(i));
       }
       return sb.toString();
   }
}

Add a comment
Know the answer?
Add Answer to:
USING JAVA Consider the following methods: StringBuilder has a method append(). If we run: StringBuilder s...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java Only can use class String length charAt class StringBuilder length charAt append toString class...

    In Java Only can use class String length charAt class StringBuilder length charAt append toString class Character any method Create the following methods nthWord takes an int and a String as input and returns a String: The input int represents a number n that is assumed to be positive, and the output string contains every nth word of the input string, starting with the first word, separated by a single space. {\em For this method, a word is defined to...

  • Programming project in Java: You are allowed to use the following methods from the Java API:...

    Programming project in Java: You are allowed to use the following methods from the Java API: class String length charAt class StringBuilder length charAt append toString class Character any method Create a class called HW2 that contains the following methods: 1. isAlphabeticalOrder takes a String as input and returns a boolean: The method returns true if all the letters of the input string are in alphabetical order, regardless of case. The method returns false otherwise. Do not use arrays to...

  • In Java All methods listed below must be public and static. If your code is using...

    In Java All methods listed below must be public and static. If your code is using a loop to modify or create a string, you need to use the StringBuilder class from the API. Keep the loops simple but also efficient. Remember that you want each loop to do only one "task" while also avoiding unnecessary traversals of the data. No additional methods are needed. However, you may write additional private helper methods, but you still need to have efficient...

  • language is java Restrictions: You are not allowed to use anything from the String, StringBuilder, or...

    language is java Restrictions: You are not allowed to use anything from the String, StringBuilder, or Wrapper classes. In general, you may not use anything from any other Java classes, unless otherwise specified. You are not allowed to use String literals in your code ("this is a string literal"). You are not allowed to use String objects in your code. The methods must be implemented by manipulating the data field array, The CSString Class: NOTE: Pay very careful attention to...

  • Java StringNode Case Study: Rewrite the following methods in the StringNode class shown below. Leave all...

    Java StringNode Case Study: Rewrite the following methods in the StringNode class shown below. Leave all others intact and follow similar guidelines. The methods that need to be changed are in the code below. - Rewrite the indexOf() method. Remove the existing recursive implementation of the method, and replace it with one that uses iteration instead. - Rewrite the isPrefix() method so that it uses iteration. Remove the existing recursive implementation of the method, and replace it with one that...

  • [Java] Create a class called FileExercises that contains the following method Method encrypt that does not...

    [Java] Create a class called FileExercises that contains the following method Method encrypt that does not return anything and takes a shift, input filename and output filename as arguments. It should read the input file as a text file and encrypt the content using a Caesar cypher with the shift provided. The resulting text should be placed in the output file specified. (If the output file already exists, replace the existing file with a new file that contains the required...

  • I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...

    I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...

  • JAVA: Run length encoding is a simple form of data compression. It replaces long sequences of...

    JAVA: Run length encoding is a simple form of data compression. It replaces long sequences of a repeated value with one occurrence of the value and a count of how many times to repeat it. This works reasonably well when there are lots of long repeats such as in black and white images. To avoid having to represent non-repeated runs with a count of 1 and the value, a special value is often used to indicate a run and everything...

  • ​​​​​​public static int countCharacter(String str, char c) { // This recursive method takes a String and...

    ​​​​​​public static int countCharacter(String str, char c) { // This recursive method takes a String and a char as parameters and // returns the number of times the char appears in the String. You may // use the function charAt(int i) described below to test if a single // character of the input String matches the input char. // For example, countCharacter(“bobbie”, ‘b’) would return back 3, while // countCharacter(“xyzzy”, ‘y’) would return back 2. // Must be a RECURSIVE...

  • Write a class StringsAndThings. The class has one instance variable of type String and a constructor...

    Write a class StringsAndThings. The class has one instance variable of type String and a constructor with a parameter to initialize the instance variable. The parameter could contain any characters, including letters, digits, spaces, special characters (+, -, $, @,...), and punctuation marks. Write methods: countNonLetters - that will count how many of the characters in the string are not letters. You can use Character's isLetter method. moreVowels - which returns true if the String parameter has more vowels than...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT