Answer to Question #101116 in Java | JSP | JSF for Anupam Choudhury

Question #101116
Write a program in Java 2 input the names of n cricketers and the number of one day matches they have played till date into two separate one dimension arrays sort the array size in alphabetically order of names using selection sort then display the names and find the number of players who have played more than 200 matches
1
Expert's answer
2020-01-08T15:18:29-0500
/*******************************************************************************/
/**                       A question about cricket players.                    */
/**                     Sorting players' names alphabetically.                 */
/**        Conclusion of the number of players who played more than 200 games. */
/*******************************************************************************/
package com.company;
import java.util.Scanner;

public class Main {

    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        int num = 0;                            //number of players

        System.out.println("Please enter the number of players.");
        while (num <= 0 )
        {
            System.out.println("Enter an integer greater than zero. ");
            if(scan.hasNextInt())               //enter an integer greater than zero.
            {
                num = scan.nextInt();
            }
            else
            {
                scan.nextLine();
            }

        }
        scan.nextLine();

        String[] cricketers = new String[num];
        int[]  numGame = new int[num];

        for(int i=0; i<num; i++)
        {
            System.out.println("Enter player name with number " + String.valueOf(i+1) + " : ");
            cricketers[i] = scan.nextLine();
            numGame[i] = -1;
            System.out.println("Enter the number of games he played. ");
            while (numGame[i] < 0 )         //The number of games cannot be a negative number.
            {
                if(scan.hasNextInt())
                {
                    numGame[i] = scan.nextInt();
                }
                else
                {
                    scan.nextLine();
                }
            }
            scan.nextLine();
        }

        String[] s = sort(cricketers);

        for(int i=0; i<num; i++)
        {
            System.out.println(s[i]);
        }

        System.out.println("\n" + more200(numGame) + " players have played more than 200 matches.");

        scan.close();
    }


    /**selection sort*/
    public static String[] sort(String[] str)
    {
        int i, j;
        int len = str.length;
        String s;
        for(i=0; i<(len-1); i++)
        {
            for(j=i+1; j<(len); j++)
            {
                if(str[i].compareTo(str[j]) > 0)
                {
                    s = str[i];
                    str[i] = str[j];
                    str[j] = s;
                }
            }
        }
        return str;
    }

    /**returns the number of players who played more than 200 games*/
    public static int more200(int[] game)
    {
        int match = 0;
        for(int i=0; i<game.length; i++)
        {
            if(game[i]>200)
            {
                match++;
            }
        }
        return match;
    }

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS