Question

Write a method will accept an array of Strings. This method should return the string that...

Write a method will accept an array of Strings. This method should return the string that comes after all the other strings in lexicographical order.
(Assume all the Strings only contain lower case letters. In Example: No number, no Capitals and no Symbols)

lastString({“hola”, “word”, “night”, “boom”}) → “word”
lastString({“java”, “is”, “so”, “much”, “fun”}) → “so”
lastString({“i”, “love”, “java”}) → “love”

//precondition: words.length >0
public static String lastString(String[] words)
{

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1


public class Main
{
//precondition: words.length >0
public static String lastString(String[] words)
{
//RETURNS THE LAST STRING IN LEXICOGRAPHIC ORDER
String last = words[0];
for(int i=0;i<words.length;i++){
if(last.compareTo(words[i])<0)//updating if current word comes after last word
last=words[i];
}
return last;
}
   public static void main(String[] args) {
   //testing above method
       System.out.println(lastString(new String[]{"hola", "word", "night", "boom"}));
       System.out.println(lastString(new String[]{"java", "is", "so", "much", "fun"}));
       System.out.println(lastString(new String[]{"i","love","java"}));
   }
}

output:

word
so
love


//PLS give a thumbs up if you find this helpful, it helps me alot, thanks.


//if you have any doubts, ask me in the comments

Add a comment
Know the answer?
Add Answer to:
Write a method will accept an array of Strings. This method should return the string that...
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
  • 1) Write a method that will return the length of smallest string in an array of...

    1) Write a method that will return the length of smallest string in an array of strings. smallestLength ({“hola”, “word”, “night”, “boom”}) → 4 smallestLength ({“java”, “is”, “so”, “much”, “fun”}) → 2 smallestLength ({“i”, “love”, “java”}) → 1 //precondition: strs.length>0 public static int smallestLength(String[] strs) { }

  • This method takes an array of Strings as a parameter and has no return value. The...

    This method takes an array of Strings as a parameter and has no return value. The purpose of this method is to count all the characters in an array of strings. Count the number of characters in every string in the array and assign to numberChars. Count the number of lowercase letters in every string in the array and assign to numberLower. Count the number of uppercase letters in every string in the array and assign to numberUpper. Count characters...

  • Why is my java method returning "-1" even if the string is in the array? The...

    Why is my java method returning "-1" even if the string is in the array? The method is:        public static int findWord(String wordToFind {        boolean found=false;            int i=0;            while (i<words.length&& !found)                if (words[i]==wordToFind)                    found=true;                else i++;                if (found)                    return i;                else return -1;   ...

  • Write a function that will accept an array of string. Return the index of the element...

    Write a function that will accept an array of string. Return the index of the element that has the most vowels. For this exercise, vowels are a,e,i,o, and u. If none of the strings contain a vowel, return -1. If two or more words contain the same largest amount of vowels, either index of such words is acceptable. (using C++)

  • **JAVA** Write a binary search method for a String array that might contain nulls. Your method...

    **JAVA** Write a binary search method for a String array that might contain nulls. Your method should not crash or throw a runtime exception. See the driver program for examples. The method header is: public static int binarySearchWithNulls(String[] words, String target) must be recursive.

  • /**    Given a String and an array of two Strings,    return a three String...

    /**    Given a String and an array of two Strings,    return a three String array containing the strings in alphabetical order.    Note: Capital letters count    sort3Strings("wallace", {"washington", "irving"}) -> {"irving", "wallace", "washington"}    sort3Strings("wallace", {"washington", "Irving"}) -> {"Irving", "wallace", "washington"}    sort3Strings("Washington", {"irving", wallace"}) -> {"Washington", "irving", "wallace"}    sort3Strings("washington", {"washington", "Washington"}) -> {"Washington", "washington", "washington"} **/ public static String[] sort3Strings(String stringValue, String[] stringArray) {    //your code here    return new String[1]; }//end sort3Strings   ...

  • write a program which include a class containing an array of words (strings).The program will search...

    write a program which include a class containing an array of words (strings).The program will search the array for a specific word. if it finds the word:it will return a true value.if the array does not contain the word. it will return a false value. enhancements: make the array off words dynamic, so that the use is prompter to enter the list of words. make the word searcher for dynamic, so that a different word can be searched for each...

  • You will write three static methods to manipulate an input String in different ways using various...

    You will write three static methods to manipulate an input String in different ways using various String methods. You need to provide the code for each of the three static methods in class StringPlay (requirements for each listed below). You will also change the control statement in the test harness to allow for variations in the sentinel value. You need to modify the loop control condition in Lab09.java so that user inputs of ‘finish’, “FINISH”, “FiniSH”, “fINISH”, etc. will end...

  • In java please write a method List toList(String[] array) that converts its argument array of strings...

    In java please write a method List toList(String[] array) that converts its argument array of strings to a list of strings and then returns it.

  • package week_3; /** Write a method called countUppercase that takes a String array argument. You can...

    package week_3; /** Write a method called countUppercase that takes a String array argument. You can assume that every element in the array is a one-letter String, for example String[] test = { "a", "B", "c", "D", "e"}; This method will count the number of uppercase letters from the set A through Z in the array, and return that number. So for the example array above, your method will return 2. You will need to use some Java library methods....

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