-4

My array contains 3 elements which describe 3 different 'singers', every singer has an abstract String method which returns 'lyrics'. Lyrics contain lower and uppercase letters, the singer who has the largest number of uppercase letters is the one who sings the loudest. The method below has to compare these three singers and their lyrics, the one who sings the loudest (has the largest number of uppercase letters) needs to be returned, if anyone knows how to solve this problem, please, help me. Thanks!

public static String najglosniej(Spiewak[] sp) {

        return " ";
}
Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51
1qazxsw2
  • 5
  • 4
  • 1
    We won't do your homework for you. You don't even show any effort to try to solve this yourself. – mata Apr 18 '15 at 13:02

2 Answers2

0

I'll explain you two methods, but won't code it for you. The most basic is using imperative programming and a loop. One could also use a custom function with max.

Using a loop

The main idea of imperatively getting an element with the max count is having a loop in which we calculate the count we are interested in for each element. We also have a storage variable for the currently highest element and the currently highest count. Something like max_count and max_element. These two are coupled (which is, why the imperative method is not the best method to use, but the one I think most beginners use; you easily forget to update either one of the two and have an error).

The max_count holds the maximum count we saw up to the current iteration, the max_element the object which gave us the currently maximum value. If in one iteration, we see a count higher than max_count, we update both max_count and max_element, so that in all upcoming iterations we know the currently highest count and element.

At the end of the loop, max_element will be the result.

Using max

A cleaner way to read your code would be to use the Collections.max function with a custom comparison. Usually, one learns comparing elements with the built-in comparison at first. Like when you sort a list of numbers, the numerical value is being used. When you sort strings, the alphabet is used. But you can also use custom comparators in Java (e.g. for sort or max). This custom comparator then retrieves two elements to compare. Instead of relying on a built-in function (which in Java would be converting the object to string representation (toString()) and comparing alphabetically, I think), you pass it an object with only one method which calculates the loudness for both singers and compares the numbers. See this example, where employees are compared by their salary. It resembles your problem a lot, only that you will need to have a calculation for the number of uppercase letters too.

aufziehvogel
  • 7,167
  • 5
  • 34
  • 56
  • Thank you very much, I didn't mean anyone to code it for me, I just need some hints, I've tried a few methods but weren't successful, that's why I posted an empty method, I know the rules and I don't expect anyone to make the whole code for me, thanks again for your reply, I will surely use them. – 1qazxsw2 Apr 18 '15 at 13:21
  • If you can understand it, go for the `max` method. It's much cleaner in code, but I know it requires a bit of time to understand callbacks and stuff. – aufziehvogel Apr 18 '15 at 13:24
0

Try this (if i really understand you):

String[] test = new String[3];
        test[0] = "hello";
        test[1] = "WoRLD";
        test[2] = "WINNER";
        String result = getMaxCapitalLetters(test);
    }

    public String getMaxCapitalLetters(String[] strings){
        Pattern p = Pattern.compile("([A-Z])");
        String winner = null;
        int maxCount = Integer.MIN_VALUE;
        for(String input : strings){
            Matcher m = p.matcher(input);
            int count = 0;
            while(m.find()) {
                count++;
            }
            if (count > maxCount) {
                maxCount = count;
                winner = input;
            }
        }
        return winner;
    }
Olli Zi
  • 325
  • 2
  • 10
  • Thank you for your reply ! I've done it differently but this might be helpful somewhere in the future :) – 1qazxsw2 Apr 19 '15 at 22:20