Java: Write a class StringsAndThings. It has a constructor that takes a String p
ID: 3594734 • Letter: J
Question
Java:
Write a class StringsAndThings. It has a constructor that takes a String parameter.
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 punctuation and digits. 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 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.
No starter file. Provide Javadoc for the class, the constructor, and all methods.
The following file is used to check your work:
StringsAndThingsTester.java
Explanation / Answer
StringsAndThingsTester.java
public class StringsAndThingsTester
{
public static void main(String[] args)
{
StringsAndThings thing = new StringsAndThings
("The Fox jumped over the lazy dog.");
System.out.println("More vowels: " + thing.moreVowels());
System.out.println("Expected: false");
thing = new StringsAndThings("More vowels: " + "Welcome, too");
System.out.println(thing.moreVowels());
System.out.println("Expected: false");
thing = new StringsAndThings("It is due");
System.out.println("More vowels: " + thing.moreVowels());
System.out.println("Expected: true");
thing = new StringsAndThings("Mississippi");
System.out.println("No duplicates: " + thing.noDuplicates());
System.out.println("Expected: Misp");
thing = new StringsAndThings("The big, quick, brown toad!");
System.out.println("No duplicates: " + thing.noDuplicates());
System.out.println("Expected: The big,quckrowntad!");
thing = new StringsAndThings("100 + 200 is 300. stop");
System.out.println("No duplicates: " + thing.noDuplicates());
System.out.println("Expected: 10 +2is3.top");
}
}
StringsAndThings.java
public class StringsAndThings {
private String s;
public StringsAndThings(String s) {
this.s=s ;
}
public boolean moreVowels() {
int vCount =0, cCount= 0;
for(int i=0;i<s.length();i++) {
char ch = s.charAt(i);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='U'||ch=='O') {
vCount++;
} else if(Character.isLetter(ch)) {
cCount++;
}
}
return vCount > cCount;
}
public String noDuplicates() {
String str = "";
for(int i=0;i<s.length();i++) {
if(!str.contains(s.charAt(i)+"")) {
str+=s.charAt(i);
}
}
return str;
}
}
Output:
More vowels: false
Expected: false
false
Expected: false
More vowels: true
Expected: true
No duplicates: Misp
Expected: Misp
No duplicates: The big,quckrowntad!
Expected: The big,quckrowntad!
No duplicates: 10 +2is3.top
Expected: 10 +2is3.top
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.