Question: Java, I am having trouble executing this source code. please help // The class LongestStringMain is used to determine the longest string in the array
Java, I am having trouble executing this source code. please help
// The class LongestStringMain is used to determine the longest string in the array
// In the main() function, create the object
// In the compare() method
// Determine the length of two strigs and subtract one string from another string
// and return the result.
// Initialize the array list element values
// Call the maximum() method to determine the longest string in the array list.
// Display the longest string in the array list.
// In the maximum() method
// Declare and initialize the variable.
// The for loop compares the two string to find the longest string in the array list.
// if yes, call the compare() method to determine the longest string and
// check whether the longest string is greater thann 0.
// if yes, asssign test tring into "max" variable
// Return the longest string
/* This program demonstrates to find the longest string in the array list */
import java.util.ArrayList;
import java.util.Comparator;
// The class LongestStringMain is used to determine the longest string in the array
public class LongestStringMain{
// The main() function creates a object for ArrayList class and Comparator interface,
// initializes the values for that array element, and determines the longest string
// in the array list by calling maximum() method.
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String[] args){
// create an object "aObj" for ArrayList class
ArrayList aObj = new ArrayList();
// create an object "cObj" for Comparator interface
Comparator cObj = new Comparator(){
// the compare() method accepts "s1" and "s2" as input parameter to determine
// the length of two string and subtract one string from another string and return the result
@SuppressWarnings("unused")
public int compare(String s1, String s2){
// subtract the string from another string and return the result
return s1.length() - s2.length();
}
@Override
public int compare(Object o1, Object o2) {
// TODO Auto-generated method stub
return 0;
}
};
// initialize the array list element values
// assign the string into array list element
aObj.add("kkkk");
aObj.add("aa");
aObj.add("bbbbbb");
aObj.add("ccc");
// call the maximum() method to determine the longest string in the array list
// call the method to find the longest strig in the array element
String max = maximum(aObj, cObj);
// display the longest string in the array element
System.out.printf("The longest string is : " + max);
}
// the maximum() method accepts "a" and "c" as inpout parameters to determine
// the longest string in the array list. method definition
@SuppressWarnings({ "rawtypes", "unchecked" })
public static String maximum(ArrayList a, Comparator c){
// declare the string variable and get the array values as "0"
String max = (String) a.get(0);
// the for loop compares the two string to find the longest string in the array list
// loop compare the string "test" from "a"
for(String test:a){
// call the compare() method to determine the longest strig and check whether
// the longest string is greater than 0. if yes, assign test string into "max" variable
// verify the longest string greater than 0
if(c.compare(test, max) > 0)
// assign test into "max" variable
max = test;
}
//return the longest string in the array
return max;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
