Question

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 consonants. Otherwise it returns false. Ignore all other characters that are not letters. Add the isVowel method from Lab6 to your class. You can use isLetter from the Character class.
  • noDuplicates - which returns a string where each character in the phrase appears exactly once. Consider upper and lower case letters as different letters. Note that spaces and punctuation are also characters. This is a good place to use String's contains method. Hint: Remember if a boolean expression dup is true then !dup is false. Do not use a do-nothing if clause. There are a lot of weird solutions on the Internet. Do not use anything we have not covered. If the string is Mississippi, the return value will be Misp. Hint: it is easier to use substring here than charAt.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

class StringsAndThings {
   private String str;

   public StringsAndThings(String aStr) {
       super();
       str = aStr;
   }
   public int countNonLetters() {
       int count=0;
       for(int i=0;i<str.length();i++)
               if(Character.isLetter(str.charAt(i)))
                   count++;
       return str.length()-count;
   }
   public boolean moreVowels() {
       int count=0;
       String s="AEIOUaeiou";
       for(int i=0;i<str.length();i++)
           if(s.contains(str.charAt(i)+""))
               count++;
       int c1=str.length()-count;
       return count>=c1;
   }
   public String noDuplicates() {
       String res="";
      
       for(int i=0;i<str.length();i++)
           if(!res.contains(str.charAt(i)+""))
                   res=res+str.charAt(i);
      
       return res;
   }
}
public class TestStringsAndThings {
       public static void main(String[] args) {
           StringsAndThings sa= new StringsAndThings("Udaay");
           System.out.println(sa.countNonLetters());
           System.out.println(sa.moreVowels());
           System.out.println(sa.noDuplicates());
       }
}

Add a comment
Know the answer?
Add Answer to:
Write a class StringsAndThings. The class has one instance variable of type String and a constructor...
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
  • Write the Java code for the class WordCruncher. Include the following members:

    **IN JAVAAssignment 10.1 [95 points]The WordCruncher classWrite the Java code for the class WordCruncher. Include the following members:A default constructor that sets the instance variable 'word' to the string "default".A parameterized constructor that accepts one String object as a parameter and stores it in the instance variable. The String must consist only of letters: no whitespace, digits, or punctuation. If the String parameter does not consist only of letters, set the instance variable to "default" instead. (This restriction will make...

  • 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()...

  • containsSubSequence takes two Strings as input and returns a boolean: Returns true if the first input...

    containsSubSequence takes two Strings as input and returns a boolean: Returns true if the first input string contains all the characters of the second input string, in order, but not necessarily consecutively. > HW2.containsSubSequence("abracadabra", "abcd") true > HW2.containsSubSequence("abracadabra", "abdc") false you must not use either break or continue in your code. 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 I write something...

  • Write a class named GasTank containing: An instance variable named amount of type double, initialized to...

    Write a class named GasTank containing: An instance variable named amount of type double, initialized to 0. A method named addGas that accepts a parameter of type double. The value of the amount instance variable is increased by the value of the parameter. A method named useGas that accepts a parameter of type double. The value of the amount instance variable is decreased by the value of the parameter. However, if the value of amount is decreased below 0, amount...

  • Complete the class LoopyText. The constructor has been completed and provided to you in Codecheck. public...

    Complete the class LoopyText. The constructor has been completed and provided to you in Codecheck. public LoopyText(String theText) Constructs an object of LoopyText and stores theText inside the object. The parameter theText is a string with zero or more words separated by single spaces. public String getEverySecondCharacter() Returns a string consisting of every other character, starting with the character at index 0. If the string is the empty string return the empty String. public int upperCaseCount() Returns the number of...

  • Task 01: (a)Write a class Fruit with: private instance variables name, and pricePerKilogram Appropriate constructor Appropriate...

    Task 01: (a)Write a class Fruit with: private instance variables name, and pricePerKilogram Appropriate constructor Appropriate accessor methods Appropriate mutator methods toString method equals method (b) Write a FruitDriver class that: Initializes an array of Fruit objects, fruitArray, with 10 objects with names: banana, apple, mango, orange,pineapple, pear, grapes, tangerine, watermelon, sweetmelon and appropriate prices per kilogram. Uses an appropriate loop to display all objects with pricePerKilogram > 5.00 Saudi Riyals, if any. Calls a linearSearch method:                        public static...

  • Write a class named Book containing: Two instance variables named title and author of type String....

    Write a class named Book containing: Two instance variables named title and author of type String. A constructor that accepts two String parameters. The value of the first is used to initialize the value of title and the value of the second is used to initialize author. A method named toString that accepts no parameters. toString returns a String consisting of the value of title, followed by a newline character, followed by the value of author.

  • Write a method in java named isValidEmail that takes a string as input parameter, and returns...

    Write a method in java named isValidEmail that takes a string as input parameter, and returns true if that string represents a valid email address, or false otherwise. An email address is considered valid if it follows this format “user123@domain.ext”, where:  user123 represents a sequence of word characters (i.e., letters, digits, or underscore) whose length is between 1 and 10 (inclusive), but the first character must be a letter  domain represents a sequence of alphanumeric characters (i.e., letters...

  • 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...

  • Java Write a class named CheckingAccount containing: An instance variable named balance of type double, initialized...

    Java Write a class named CheckingAccount containing: An instance variable named balance of type double, initialized to 0. A method named deposit that accepts a parameter of type double. The value of the balance instance variable is increased by the value of the parameter. A method named withdraw that accepts a parameter of type double. The value of the balance instance variable is decreased by the value of the parameter. A method named getBalance that accepts no parameters, returns the...

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