Java Program Using Arrays Need help urgent, please put comments * * Your task is
ID: 639014 • Letter: J
Question
Java Program Using Arrays Need help urgent, please put comments
*
* Your task is to complete the program HighCard. The program should carry out these steps:
Define an ArrayList object, list, that contains Integer objects
Add the numbers from 0 to 51 to list
Use the Collections.shuffle method to create permutations
Each number represents a single playing card.
Play one game of high card with 4 players
The game consists of 13 rounds
Each round
1 card is dealt to each player
The player with the higher card is the winner
Print the winner and the card each player has
Remove the cards from the deck
Shuffle the deck
At the end of the game
Print each player and the numbers of games won
Print the player who won the most games
*/
Explanation / Answer
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class HighCard
{
public static void main(String[] args)
{
int[] wins=new int[4];
wins[0]=0;wins[1]=0;
wins[2]=0;wins[3]=0;
//Define an ArrayList object, list, that contains Integer objects
List<Integer> numbers=new ArrayList<Integer>();
//Add the numbers from 0 to 51 to list
for(int i=0;i<52;i++)
numbers.add(i);
//Use the Collections.shuffle method to create permutations
Collections.shuffle(numbers);
for(int i=0;i<13;i++)
{
System.out.println("round"+(i+1));
int prev=-1;
int winner=1;
for(int j=0;j<4;j++)
{
System.out.println("player "+(j+1)+" card-"+numbers.get(0));
if(prev<numbers.get(0))
{
winner=j+1;
prev=numbers.get(0);
}
numbers.remove(numbers.get(0));
}
System.out.println("winner is player-"+winner);
wins[winner-1]++;
//shuffle after each round
Collections.shuffle(numbers);
}
System.out.println("---------Result----------");
System.out.println("wins of player1-"+wins[0]);
System.out.println("wins of player2-"+wins[1]);
System.out.println("wins of player3-"+wins[2]);
System.out.println("wins of player4-"+wins[3]);
//find player with highest wins
int temp=0,large=0;
for(int i=0;i<4;i++)
{
if(wins[i]>large)
{
temp=i;
large=wins[i];
}
}
System.out.println("player with most wins is player-"+(temp+1)+" won "+wins[temp]);
}
}
output:
round1
player 1 card-31
player 2 card-27
player 3 card-30
player 4 card-41
winner is player-4
round2
player 1 card-44
player 2 card-34
player 3 card-7
player 4 card-37
winner is player-1
round3
player 1 card-35
player 2 card-12
player 3 card-29
player 4 card-50
winner is player-4
round4
player 1 card-9
player 2 card-4
player 3 card-14
player 4 card-51
winner is player-4
round5
player 1 card-23
player 2 card-40
player 3 card-18
player 4 card-49
winner is player-4
round6
player 1 card-17
player 2 card-15
player 3 card-0
player 4 card-3
winner is player-1
round7
player 1 card-5
player 2 card-36
player 3 card-43
player 4 card-11
winner is player-3
round8
player 1 card-32
player 2 card-38
player 3 card-39
player 4 card-33
winner is player-3
round9
player 1 card-6
player 2 card-13
player 3 card-19
player 4 card-8
winner is player-3
round10
player 1 card-28
player 2 card-26
player 3 card-46
player 4 card-48
winner is player-4
round11
player 1 card-10
player 2 card-45
player 3 card-1
player 4 card-16
winner is player-2
round12
player 1 card-47
player 2 card-42
player 3 card-22
player 4 card-2
winner is player-1
round13
player 1 card-24
player 2 card-20
player 3 card-21
player 4 card-25
winner is player-4
---------Result----------
wins of player1-3
wins of player2-1
wins of player3-3
wins of player4-6
player with most wins is player-4 won 6
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.