Write a class State. The constructor takes a String parameter and is provided fo
ID: 3843254 • Letter: W
Question
Write a class State. The constructor takes a String parameter and is provided for you. You need to copy the starter code from Codecheck You provide the methods: public int numberOfCharacters() gets the number of characters in the State name (Think length) public boolean moreVowels() returns true if the Word has more vowels than other characters. Otherwise it returns false. The vowels are "aeiouAEIOU". You can get the number of other characters by subtracting the number of vowels from the total number of characters - the length) public boolean isWestCoast() returns true if the state name is "California", "Oregon", or "Washington". Otherwise it returns false.
THE CODE SHOULD BE IN JAVA.
Use the following file:
StateTester.java
Explanation / Answer
For finding the length of String you would use string.length() function , where string is the string you enterd. For counting vowels you have to iterate through the String and for each character (you can get character at position i using string.charAt(i) ), check if its 'a' or 'e' or 'i' or 'o' or 'u'. Keep a counter, initialize it with 0, and each time you encounter a vowel inrease counter by 1. For ignoring cases while comparing use .equalsIgnoreCase() .
Below is the java code for class State
class State{
String query;
State(String query){
this.query=query;
}
int numberOfCharacters(){
return query.length();
}
Boolean moreVowels(){
int vowelCounter=0;
for(int i=0;i<query.length();i++){
String q = Character.toString(query.charAt(i));
if(q.equalsIgnoreCase("a")||q.equalsIgnoreCase("e")||q.equalsIgnoreCase("i")||q.equalsIgnoreCase("o")||q.equalsIgnoreCase("u"))
vowelCounter++;
}
int total = this.numberOfCharacters();
if(total-vowelCounter<vowelCounter)
return true;
else
return false;
}
Boolean isWestCoast(){
if(query.equalsIgnoreCase("California")||query.equalsIgnoreCase("Oregon")||query.equalsIgnoreCase("Washington"))
return true;
else
return false;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.