Question

USING JAVA GIVEN THE CODE BELOW Choose the players to play each other in the rounds...

USING JAVA GIVEN THE CODE BELOW Choose the players to play each other in the rounds (there would be 3 rounds, quarter-final, semi-final and final) from the eligible players (players that lose a game are eliminated). 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

CODE:

public class PlayerSort {

public static void main(String[] args) {
String[] names = {"Ronaldo", "Messi", "Bale", "Neymar", "Isco", "Hazard", "Mbappe", "Sterling"};
int[] ranks = {1, 2, 5, 3, 8, 4, 6, 7};

// sorting here
String tempName;
int tempRank;
for (int i = 0; i < names.length; i++) {
for (int j = 0; j < names.length-1; j++) {
if (ranks[j] > ranks[j+1]) {
tempName = names[j];
names[j] = names[j+1];
names[j+1] = tempName;

tempRank = ranks[j];
ranks[j] = ranks[j+1];
ranks[j+1] = tempRank;
}
}
}

// print players
System.out.println("Players sorted by ranks are:");
for (int i = 0; i < names.length; i++) {
System.out.println(ranks[i] + ". " + names[i]);
}
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

For this program, we just need to add the randomizing mechanism for the player matches. So, we take two players, one from each end of the array like: 1-8, 2-7, etc and then using a 50-50 chances(can be changed) of each player winning. So,if the outcome of the random() function is less than 0.5, then first player wins and the second one otherwise.

CODE:

public class Main {

public static void main(String[] args) {
String[] names = {"Ronaldo", "Messi", "Bale", "Neymar", "Isco", "Hazard", "Mbappe", "Sterling"};
int[] ranks = {1, 2, 5, 3, 8, 4, 6, 7};

// sorting here

String tempName;
int tempRank;
for (int i = 0; i < names.length; i++) {
for (int j = 0; j < names.length-1; j++) {
if (ranks[j] > ranks[j+1]) {
tempName = names[j];
names[j] = names[j+1];
names[j+1] = tempName;

tempRank = ranks[j];
ranks[j] = ranks[j+1];
ranks[j+1] = tempRank;
}
}
}

// print players
System.out.println("Players sorted by ranks are:");
for (int i = 0; i < names.length; i++) {
System.out.println(ranks[i] + ". " + names[i]);
}

for(int i=0; i<4; i++)
{
System.out.println("Match between "+names[i]+" and "+names[8-i-1]);
  
if (Math.random() < 0.5) //randomizing game output with 50-50 chances
System.out.println("Winner: "+names[i]);
else
System.out.println("Winner: "+names[8-i-1]);
}
}
}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
USING JAVA GIVEN THE CODE BELOW Choose the players to play each other in the rounds...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Listed below is code to play a guessing game. In the game, two players attempt...

    Java Listed below is code to play a guessing game. In the game, two players attempt to guess a number. Your task is to extend the program with objects that represent either a human player or a computer player. boolean checkForWin (int guess, int answer) { System.out.println("You guessed" + guess +"."); if (answer == guess) { System.out.println( "You're right! You win!") ; return true; } else if (answer < guess) System.out.println ("Your guess is too high.") ; else System.out.println ("Your...

  • In java---- The DeckTester.java file, provides a basic set of Deck tests. Add additional code at...

    In java---- The DeckTester.java file, provides a basic set of Deck tests. Add additional code at the bottom of the main method to create a standard deck of 52 cards and test the shuffle method ONLY in the Deck class. After testing the shuffle method, use the Deck toString method to “see” the cards after every shuffle. Deck: import java.util.List; import java.util.ArrayList; /** * The Deck class represents a shuffled deck of cards. * It provides several operations including *...

  • Modify the program that you wrote for the last exercise in a file named Baseball9.java that...

    Modify the program that you wrote for the last exercise in a file named Baseball9.java that uses the Player class stored within an array. The program should read data from the file baseball.txt for input. The Player class should once again be stored in a file named Player.java, however Baseball9.java is the only file that you need to modify for this assignment. Once all of the input data from the file is stored in the array, code and invoke a...

  • How to I change this code to print the highest value(s) in the array? The wanted...

    How to I change this code to print the highest value(s) in the array? The wanted output (you just need to make it print the winner, ignore what comes before) is included below: -------------------------------------------------------------------------------------------- public static void playGame(int players, int cards) { if (players * cards > 52) { System.out.println("Not enough cards for that many players."); return; } boolean[] deck = new boolean[52]; int[] playerScore = new int[players]; for (int i = 0; i < players; i++) { System.out.println("Player "...

  • This assignment simply requests that you take the quicksort algorithm that you have been working with...

    This assignment simply requests that you take the quicksort algorithm that you have been working with and make it generic. You are to demonstrate the effectiveness of your work by defining the main method that passes a list of Integers, Doubles, and Strings and sorts them all. NOTE: this is the code, please tell me which one of the blocks of code does. import java.util.*; public class Driver{ public static <T extends Comparable<T>> void quickSort(T[] data, int a, int b)...

  • This is my code for my game called Reversi, I need to you to make the...

    This is my code for my game called Reversi, I need to you to make the Tester program that will run and complete the game. Below is my code, please add comments and Javadoc. Thank you. public class Cell { // Displays 'B' for the black disk player. public static final char BLACK = 'B'; // Displays 'W' for the white disk player. public static final char WHITE = 'W'; // Displays '*' for the possible moves available. public static...

  • (a)How many times does the code snippet given below display "Hello"? int x = 1; while...

    (a)How many times does the code snippet given below display "Hello"? int x = 1; while (x != 15) {    System.out.println ("Hello");    x++; } (b)What is the output of the following code fragment? int i = 1; int sum = 0; while (i <= 5) {    sum = sum + i;    i++; } System.out.println("The value of sum is " + sum); Quie 2 What is the output of the following snipped code? public class Test {...

  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

  • 1. Given the following code, how many references exist to the Time object at the end...

    1. Given the following code, how many references exist to the Time object at the end of the main method and what are their names? public class Foo { public static void main(String args[]) { Time t = new Time(); Time t1 = t; Time t2 = t; Clock clock = new Clock(t); // AT THIS POINT, HOW MANY REFERENCES TO THE TIME OBJECT ARE THERE } } public class Time { } public class Clock { private Time time;...

  • JAVA Only Help on the sections that say Student provide code. The student Provide code has...

    JAVA Only Help on the sections that say Student provide code. The student Provide code has comments that i put to state what i need help with. import java.util.Scanner; public class TicTacToe {     private final int BOARDSIZE = 3; // size of the board     private enum Status { WIN, DRAW, CONTINUE }; // game states     private char[][] board; // board representation     private boolean firstPlayer; // whether it's player 1's move     private boolean gameOver; // whether...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT