Question

In: Computer Science

Create a method called firstLetter that takes a String parameter and integer parameter. It should return...

Create a method called firstLetter that takes a String parameter and integer parameter. It should return -1 if the number of words in the given String is greater than or equal to the integer parameter (it should return -1 and not process the String any further) (4 points). If the String does not have more words than the given integer, the method should return 1 if all the words in the string start with the same letter(8 points) and 0 if all the words do not start with the letter(4 points). (4 points: correct method signature-2 for parameter, 2 for return type) For example, System.out.println(firstLetter("Charlie chews chewy chews.",10)); would print to screen 1 since all words start with the same letter (notice the method is not case sensitive) and the number of words is less than 10.

code in java

Solutions

Expert Solution

Method code

public static int firstLetter(String str, int n)
{
String words[]=str.split(" ");
if(words.length>=n)
return -1;
for(int i=0;i<words.length-1;i++)
if(Character.toUpperCase(words[i].charAt(0))!=Character.toUpperCase(words[i+1].charAt(0)))
return 0;
return 1;
}
  

for testing i created this simple program


public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
System.out.println("firstLetter(\"Charlie chews chewy chews.\",10))="+firstLetter("Charlie chews chewy chews.",10));
System.out.println("\nfirstLetter(\"Charlie chews chewy chews.\",3))="+firstLetter("Charlie chews chewy chews.",3));
System.out.println("\nfirstLetter(\"Charlie chews jay chews.\",10))="+firstLetter("Charlie chews jay chews.",10));
}

public static int firstLetter(String str, int n)
{
String words[]=str.split(" ");
if(words.length>=n)
return -1;
for(int i=0;i<words.length-1;i++)
if(Character.toUpperCase(words[i].charAt(0))!=Character.toUpperCase(words[i+1].charAt(0)))
return 0;
return 1;
}
  
}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.


Related Solutions

Write a static method called "evaluate" that takes a string as a parameter
In Java language  Write a static method called "evaluate" that takes a string as a parameter. The string will contain a postfix expression, consisting only of integer operands and the arithmetic operators +, -, *, and / (representing addition, subtraction, multiplication, and division respectively). All operations should be performed as integer operations. You may assume that the input string contains a properly-formed postfix expression. The method should return the integer that the expression evaluates to. The method MUST use a stack...
Java Write a valid Java method called printReverse that takes a String as a parameter and...
Java Write a valid Java method called printReverse that takes a String as a parameter and prints out the reverse of the String. Your method should not return anything. Make sure you use the appropriate return type.
Write a method that takes four strings as parameter. The first string should be a pokemon...
Write a method that takes four strings as parameter. The first string should be a pokemon name, the second a pokemon type, the third a pokemon name, and the fourth a pokemon type. The method should print out which pokemon has the advantage over the other based on their type. In this case you can use fire, water, and leaf. Example outputs should be like: Example: Pokemon X (Water) has the advantage over Pokemon Y (fire) Pokemon X (Fire) has...
create overloaded functions called lastValue. The first function should take as a parameter a string and...
create overloaded functions called lastValue. The first function should take as a parameter a string and the second function should take as a parameter an int. each of you functions should return an int value. in the case of the function that takes the string as an argument you will return the ascii value of the last character in the string. in the case of the function that takes an int parameter your function will return the last digit in...
Write a method called recursiveDownAndUp() that takes one non-negative integer parameter, recursively starts at one thousand...
Write a method called recursiveDownAndUp() that takes one non-negative integer parameter, recursively starts at one thousand and prints all the integers from one thousand to the parameter (that is, prints 1000, 999, etc. all the way down to the parameter), then recursively starts at the integer parameter and prints all the integers from the parameter up to one thousand (that is, prints the parameter, the parameter + 1, the parameter + 2, etc. all the way up to 1000). Hint:...
Write a method called recursiveDownAndUp() that takes one non-negative integer parameter, recursively starts at one thousand...
Write a method called recursiveDownAndUp() that takes one non-negative integer parameter, recursively starts at one thousand and prints all the integers from one thousand to the parameter (that is, prints 1000, 999, etc. all the way down to the parameter), then recursively starts at the integer parameter and prints all the integers from the parameter up to one thousand (that is, prints the parameter, the parameter + 1, the parameter + 2, etc. all the way up to 1000).
Write a RECURSIVE method that receives a string as a parameter. The method will return true...
Write a RECURSIVE method that receives a string as a parameter. The method will return true if the string received as a parameter is a palindrome and false if it is not. The method must not have any loops! In JAVA
Write a method that takes an integer array as its parameter and sorts the contents of...
Write a method that takes an integer array as its parameter and sorts the contents of the array in ascending order using the Insertion Sort algorithm. Call this method after the original array and other stats have been displayed. Once the array has been sorted by your method, display its contents to the screen in the same manner as the original array was displayed. CODE SO FAR: import java.util.*; public class ArrayInteger { public static void getRandomNumber(int A[],int n){ Random...
} 1. write a function that takes a string as parameter, return true if it’s a...
} 1. write a function that takes a string as parameter, return true if it’s a valid variable name, false otherwise. You can use keyword module’s  iskeyword() to determine is a string is keyword. import keyword keyword.iskeyword(var)   #returns true or false }2. write a function that returns the length of a string (without using len() function) }3. write a function that counts number of vowels in a string }4. write a function that checks if a word is palindrome PYTHON PROGRAMMING
Create a method that takes a HashMap<Integer, Integer> and returns the sum of the keys of...
Create a method that takes a HashMap<Integer, Integer> and returns the sum of the keys of the HashMap.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT