Question

In: Computer Science

IN JAVA create a program that has 8 players and ranks them randomly and contains 3...

IN JAVA create a program that has 8 players and ranks them randomly and contains 3 rounds. For each round the highest ranked player plays the lowest ranked one i.e., in quarter-final round, player ranked 1 plays player ranked 8, player ranked 2 plays player ranked 7 and so on. Report the winner of each match, simulating using random values of your choice USING JAVA COLLECTIONS IS NOT ALLOWED

Solutions

Expert Solution

The detailed JAVA program is as given. I have randomly given the names of the players. You can change the name and ranking as per your choice.

*********************************************************************************************************************************************

import java.util.Random;

public class Tournament {

   public static void main(String[] args) {
       // TODO Auto-generated method stub
       String[] players = new String[]{"Abc","Bcd","Cde","Def","Efg","Fgh","Ghi","Hij"}; //String array to hold the name of the players
       int ranking[] = new int[] {2,3,1,5,6,7,8,4}; // Integer Array to hold the ranking of the player. You can change the ranking according to your choice
       //Displaying the Players as is given in the array
       System.out.println("Ranking of the players are as follows :");
       for(int i =0;i<8;i++)
           System.out.println(players[i]+" is ranked "+ ranking[i]);
      
       //Sorting the ranking array and players array as per ranking using selection sort
       for (int i = 0; i < 7; i++)
{
// Find the minimum element in unsorted array
int min_idx = i;
for (int j = i+1; j < 8; j++)
if (ranking[j] < ranking[min_idx])
min_idx = j;
  
// Swap the found minimum element with the first
// element
int temp = ranking[min_idx];
ranking[min_idx] = ranking[i];
ranking[i] = temp;
String temp1=players[min_idx];
players[min_idx] = players[i];
players[i] = temp1;
}
      
       System.out.println("Ranking in sorted order :");
       for(int i =0;i<8;i++)
           System.out.println(players[i]+" is ranked "+ ranking[i]);
      
       //Code for Quarter-Final
       String qtrfinal[][]=new String[4][2]; // Array to hold the competitors of the Quarter - Final
       for(int i=0,j=7;i<4 ;i++,j--)
       {
       qtrfinal[i][0]=players[i];
       qtrfinal[i][1]=players[j];
       }
       //Displaying the order of matches
       System.out.println("Order of Matches(Quarter Final) :");
       for(int i=0,j=7;i<4 ;i++,j--)
       {
       System.out.println("["+(i+1)+"] "+qtrfinal[i][0]+" Vs ["+(j+1)+"]"+qtrfinal[i][1]);
       }
      
       Random ran = new Random(); //Random fuction to generate random number for winner
      
       System.out.println("Result of Matches(Quarter Final) :");
       int semi[] = new int[4];// array to hold the index of winners in quarter final
       for(int i=0,j=7;i<4 ;i++,j--)
       {
           System.out.println("["+(i+1)+"] "+qtrfinal[i][0]+" Vs ["+(j+1)+"]"+qtrfinal[i][1]);
           int nxt = ran.nextInt(2); //Finding the winner
           int winindex = findfunction(players,qtrfinal[i][nxt]); //Calling the find function to get the index of the winner in ranking array.
           System.out.println("["+(ranking[winindex])+"] "+qtrfinal[i][nxt]+" wins");
           semi[i]=winindex;// Assigning the index of the winner qualified for semifinal
       //qtrfinal[i][0]=players[i];
       //qtrfinal[i][1]=players[j];
       }
   /*Code for semi final Starts here*/
      
       String semifinal[][]=new String[2][2];// Array to hold the names of semifinalists
   //Sorting the array using selection sort for players qualified in semi final
       for (int i = 0; i < 4; i++)
{
// Find the minimum element in unsorted array
int min_idx = i;
for (int j = i+1; j < 4; j++)
if (semi[j] < semi[min_idx])
min_idx = j;
  
// Swap the found minimum element with the first
// element
int temp = semi[min_idx];
semi[min_idx] = semi[i];
semi[i] = temp;
}
      
       for(int i=0,j=3;i<2 ;i++,j--)
       {
       semifinal[i][0]=players[semi[i]];
       semifinal[i][1]=players[semi[j]];
       }
       System.out.println("Order of Matches(Semi Final) : ");
      
       for(int i=0;i<2;i++)
       {
       int findex = findfunction(players,semifinal[i][0]);
       int secindex = findfunction(players,semifinal[i][1]);
       System.out.println("["+(ranking[findex])+"] "+semifinal[i][0]+" Vs "+(ranking[secindex])+"] "+semifinal[i][1]);
       }
       System.out.println("Result of Matches(Semi Final) :");
      
       int final1[] = new int[2];// Array to hold the players qualified for final
       for(int i=0;i<2;i++)
       {
       int findex = findfunction(players,semifinal[i][0]);
       int secindex = findfunction(players,semifinal[i][1]);
       System.out.println("["+(ranking[findex])+"] "+semifinal[i][0]+" Vs "+(ranking[secindex])+"] "+semifinal[i][1]);
       int nxt = ran.nextInt(2); // Finding the winners in Semi final
       int winindex = findfunction(players,semifinal[i][nxt]);
       System.out.println("["+(ranking[winindex])+"] "+semifinal[i][nxt]+" wins");
       final1 [i]=winindex;
      
       }
      
       /*Coding for findign final starts Here*/
       String fnl[][]=new String[1][2]; //Array to hold the playeres
       fnl[0][0]=players[final1[0]];
           fnl[0][1]=players[final1[1]];
          
           System.out.println("Order of Matches(Final) : ");
       int idx1 = findfunction(players,fnl[0][0]);
           int idex2 = findfunction(players,fnl[0][1]);
           System.out.println("["+(ranking[idx1])+"] "+fnl[0][0]+" Vs "+(ranking[idex2])+"] "+fnl[0][1]);
          
           System.out.println("Result of Final : ");
           idx1 = findfunction(players,fnl[0][0]);
           idex2 = findfunction(players,fnl[0][1]);
               System.out.println("["+(ranking[idx1])+"] "+fnl[0][0]+" Vs "+(ranking[idex2])+"] "+fnl[0][1]);
           int nxt = ran.nextInt(2);
           int temp1 =0 ;
           if(nxt == 0)
               temp1 = 1;
           int winindex = findfunction(players,fnl[0][nxt]);
           System.out.println("["+(ranking[winindex])+"] "+fnl[0][nxt]+" wins the tournament");
           int idex = findfunction(players,fnl[0][temp1]);
           System.out.println("["+(ranking[idex])+"] "+fnl[0][temp1]+" is Runner-UP");
      
   }
   //Function to find the ranking of a given player. It takes the players array and player name as input and provides the
   // index of the ranking array for that perticular player.
   public static int findfunction (String []a, String player)
   {
       int i=0;
       for(;i<a.length;i++)
       {
           if(a[i].equals(player))
               break;//str1.equals(str2);)
       }
       return(i);
   }

}
********************************************************************************************************************************************

The output of the program is as given below for a particular run. For every run the Output will change i.e. it may not be the same.

Ranking of the players are as follows :
Abc is ranked 2
Bcd is ranked 3
Cde is ranked 1
Def is ranked 5
Efg is ranked 6
Fgh is ranked 7
Ghi is ranked 8
Hij is ranked 4
Ranking in sorted order :
Cde is ranked 1
Abc is ranked 2
Bcd is ranked 3
Hij is ranked 4
Def is ranked 5
Efg is ranked 6
Fgh is ranked 7
Ghi is ranked 8
Order of Matches(Quarter Final) :
[1] Cde Vs [8]Ghi
[2] Abc Vs [7]Fgh
[3] Bcd Vs [6]Efg
[4] Hij Vs [5]Def
Result of Matches(Quarter Final) :
[1] Cde Vs [8]Ghi
[8] Ghi wins
[2] Abc Vs [7]Fgh
[2] Abc wins
[3] Bcd Vs [6]Efg
[3] Bcd wins
[4] Hij Vs [5]Def
[4] Hij wins
Order of Matches(Semi Final) :
[2] Abc Vs 8] Ghi
[3] Bcd Vs 4] Hij
Result of Matches(Semi Final) :
[2] Abc Vs 8] Ghi
[8] Ghi wins
[3] Bcd Vs 4] Hij
[3] Bcd wins
Order of Matches(Final) :
[8] Ghi Vs 3] Bcd
Result of Final :
[8] Ghi Vs 3] Bcd
[3] Bcd wins the tournament
[8] Ghi is Runner-UP


Related Solutions

Create a java program. War is a card game for two players. A standard deck of...
Create a java program. War is a card game for two players. A standard deck of 52 cards is dealt so that both players have 26 cards. During each round of play (or "battle"), both players play a card from the top of their hand face up. The player who plays the card of the higher rank wins both cards and places them at the bottom of his stack of cards. If both cards played are of the same rank,...
Write a Java program to randomly create an array of 50 double values. Prompt the user...
Write a Java program to randomly create an array of 50 double values. Prompt the user to enter an index and prints the corresponding array value. Include exception handling that prevents the program from terminating if an out of range index is entered by the user. (HINT: The exception thrown will be ArrayIndexOutOfBounds) *Please do it in eclipse and this is java language*
Create a program using Java. Create two different 3D arrays with random numbers. (lets name them...
Create a program using Java. Create two different 3D arrays with random numbers. (lets name them array1 and array 2) Add the two 3Darrays together and then get the average. Save the average on a separate 3D array.(lets name it array3) Then add array1 and array3 then get the average Save the average on a separate 3Darray(lets name it array 4) Then add array2 and array3 then get the average Save the average on a separate 3Darray (lets name it...
3) Create a Java program that uses NO methods, but use scanner: Write a program where...
3) Create a Java program that uses NO methods, but use scanner: Write a program where you will enter the flying distance from one continent to another, you will take the plane in one country, then you will enter miles per gallon and price of gallon and in the end it will calculate how much gas was spend for that distance in miles. Steps: 1) Prompt user to enter the name of country that you are 2) Declare variable to...
JAVA PROGRAM, Create the game "Rock, Scissor, Paper": Make the AI pick randomly. You need to...
JAVA PROGRAM, Create the game "Rock, Scissor, Paper": Make the AI pick randomly. You need to look up Random numbers in Java. First ask the user what language they want to play in. Choice 1 is English, but choice 2 can be whatever real language you want. Your first loop is making sure they enter good input. If there is a tie you don't give them the option to play again. They have to. Outer loop runs until they say...
Write a java program that contains 3 overloaded static methods for calculating area of a circle,...
Write a java program that contains 3 overloaded static methods for calculating area of a circle, area of a cylinder and volume of a cylinder. Also create an output method which uses JOptionPaneto display instance field(s) and the result of the computing. Then code a driver class which will run and test calling each of these overloaded methods with hard-coded data and display the data and the result of the calculation by calling output method. Thanks!!
Create a java program that: - Has a class that defines an exception -Have that exception...
Create a java program that: - Has a class that defines an exception -Have that exception throw(n) in one method, and be caught and handled in another one. -Has a program that always continues even if incorrect data is entered by the user -has a minimum of 2 classes in it
Create a java program that has a code file with main() in it and another code...
Create a java program that has a code file with main() in it and another code file with a separate class. You will be creating objects of the class in the running program, just as the chapter example creates objects of the Account class. Your system handles employee records and processes payroll for them. Create a class called Employee that holds the following information: first name, last name, monthly salary, and sales bonus. The class should have all the gets...
8) Create the following program using Java. Circle calculation using methods Create scanner declare double variable...
8) Create the following program using Java. Circle calculation using methods Create scanner declare double variable radius = -999 declare character choice create do while loop inside of do loop write: System.out.println(); System.out.println("*** CIRCLE CALCULATIONS ***"); System.out.println(); System.out.println("1. Enter the radius of the circle"); System.out.println("2. Display the area of the circle"); System.out.println("3. Display the circumference of the circle"); System.out.println("4. Quit"); System.out.println(); System.out.println("Enter a number from 1 - 4"); System.out.println(); Declare choice character and relate to scanner declare switch (choice) case...
Identify errors and correct them of the following Java program : 1. import java.utility.Random; 2. 3....
Identify errors and correct them of the following Java program : 1. import java.utility.Random; 2. 3. public class Sequen 4. 5. private int stand 6. private int calc; 7. 8. public Sequen(int numStand) 9. { 10. stand = numStand; 11. skip; 12. } 13. 14. public void skip() 15. { 16. Random sit = new Random(); 17. calc = sit.nextInt(stand) + 1; 18. } 19. 20. public getStand() 21. { 22. return stand; 23. } 24. 25. int getCalc() 26....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT