ALERT!
Click here to register with a few steps and explore all our cool stuff we have to offer!

Jump to content



Photo

Need Java Coder For Test Question


  • Please log in to reply
Need Java Coder For Test Question

#1

Berserk
Berserk
    Offline
    285
    Rep
    171
    Likes

    Veteran

Posts: 497
Threads: 75
Joined: Jul 01, 2019
Credits: 0

Four years registered
#1

Need someone to code this for me in java.

 

 

 

A string contains the information of the top scorers in La Liga. The String has the following format:

 

 

"name1, goals_scored1, minutes_per_goal1, assists1, name2, goals_scored2, minutes_per_goal2, assists2,..."

 

 

1.Write a method named avgGoalsScored that takes a String in the above format and returns the average scored goals per player.

 

 

2. Write a method named assists that takes a string in the above format and returns the number of players who scored more than 5 goals.

 

---------------------------------------------------------------------------------------------------------------------

 

I have an hour to get this done. If you can do it message me ASAP. Give me a price for this and we can talk about it.

 

Berserk#7678


  • 0

#2

Illuminati
Illuminati
    Offline
    1059
    Rep
    2224
    Likes

    :bird: Not a bot :bird:

Posts: 1794
Threads: 269
Joined: Jun 12, 2018
Credits: 2

Five years registered
#2

So the easiest way to approach this would be to split the string into an array. You could go with either a 2d array with each name being its own array and containing the information goals scored, minutes per goal, assists, or, assuming if a plyer has no goals scored, etc. they have 0 for goals_scored and 0 for minutes_per_goal, etc. etc. just tally the goals which would be located at index 1+4n. You can find the number of players by dividing the array length by 4. 


  • 1

Posted Image Rules, Guidelines & Policies Posted Image

General Rules - Shoutbox Rules - Marketplace Rules - ToS

Posted Image Helpful Links Posted Image

Awards - Usergroups - Statistics - FAQ - Banlist - DMCA


#3

Brian
Brian
    Offline
    1156
    Rep
    1751
    Likes

    Nulled's (Retired) Janitor

Posts: 2897
Threads: 251
Joined: Mar 12, 2018
Credits: 0

Six years registered
#3

A valid (not good enough though) answer to the first question:

    public static float avgGoalsScored(String s) {
        String[] arr = s.split(",");
        float sum = 0;
        int occ = 0;
        for (int i = 1; i < arr.length; i += 4) {
            sum += Integer.valueOf(arr[i].trim());
            occ++;
        }
        return sum / occ;
    }

You would need to know for sure if there are always the same 4 fields for every player. If one player has more or less than 4, code won't work

 

sum -> the sum of goals

occ -> ocurrences

 

and simply return sum divided by occurences

 

Same thing for assists

    public static int assists(String s) {
        String[] arr = s.split(",");
        int occ = 0;
        for (int i = 3; i < arr.length; i += 4) {
            if (Integer.valueOf(arr[i].trim()) > 5) {
                occ++;
            }
        }
        return occ;
    }

  • 1


 Users browsing this thread: