THIS IS FOR JAVA Write a JAVA program that will calculate the winner of a dive b
ID: 3890706 • Letter: T
Question
THIS IS FOR JAVA
Write a JAVA program that will calculate the winner of a dive between two divers. Each diver will have a list of 7 scores, with each score on a scale from 1-10. The dive will have a difficulty level, with a range between 1-3. The comprehensive score for a diver is determined as follows:
Discard the highest two scores and the lowest two scores.
Add the remainder of the scores.
multiply this by the difficulty of the dive. This is the overall score
Your program will create an array of 7 random numbers (between 1-10) for each diver. Your program will also create a random number for the difficulty. The code will then use these values to calculate the overall score for each diver. Then, it should display all 7 original diver scores (all on the same line) as well as the dive difficulty level and a Leader-board (this means the diver in the lead should be at the top. See the sample output below:
Explanation / Answer
import java.util.*;
import java.util.Arrays;
import java.util.Collections;
//Class DiveDifficultyLevel definition
public class DiveDifficultyLevel
{
//main method definition
public static void main(String[] args)
{
//Random class object created to generate random number
Random rd = new Random();
//Integer array for diver1 created
Integer[] diver1 = new Integer[7];
//Integer array for diver2 created
Integer[] diver2 = new Integer[7];
//To store difficulty level
Integer difficultyLevel;
//To store diver1 and diver2 final values and sum initialized to zero
Integer diver1Score, diver2Score, sum = 0;
//Loops 7 times
for(int counter = 0; counter < 7; counter++)
{
//Creates a random number between 1 and 10 for diver1
diver1[counter] = rd.nextInt(10) + 1;
//Creates a random number between 1 and 10 for diver2
diver2[counter] = rd.nextInt(10) + 1;
}//End of for loop
//Creates a random number between 1 and 3 for difficulty level
difficultyLevel = rd.nextInt(3) + 1;
//Sorts diver1 in ascending order
Arrays.sort(diver1);
//Sorts diver2 in ascending order
Arrays.sort(diver2);
//Loops 7 times and displays diver1 scores
System.out.print("Diver 1 Scores: ");
for(int counter = 0; counter < 7; counter++)
System.out.print(diver1[counter] + ", ");
//Loops 7 times and displays diver2 scores
System.out.print(" Diver 2 Scores: ");
for(int counter = 0; counter < 7; counter++)
System.out.print(diver2[counter] + ", ");
//Displays dive difficulty level
System.out.println(" Dive Difficulty: " + difficultyLevel );
//Loops 7 times and calculates sum of all elements discard the highest two scores and the lowest two scores for diver1
for(int counter = 2; counter < 5; counter++)
sum += diver1[counter];
//Multiply diver1 sum with difficulty level
diver1Score = sum * difficultyLevel;
//Initializes sum to zero for diver2
sum = 0;
//Loops 7 times and calculates sum of all elements discard the highest two scores and the lowest two scores for diver2
for(int counter = 2; counter < 5; counter++)
sum += diver2[counter];
//Multiply diver1 sum with difficulty level
diver2Score = sum * difficultyLevel;
System.out.println("Leader Board");
System.out.println("--------------");
//Checks if diver1 final score is greater than diver2 final store
//then display the highest score first then lowest
if(diver1Score > diver2Score)
System.out.println("Diver 1: " + diver1Score + " Diver 2: " + diver2Score);
//Otherwise
else
System.out.println(" Diver 2: " + diver2Score + " Diver 1: " + diver1Score);
}//End of main method
}//End of class
Sample Run 1:
Diver 1 Scores: 2, 3, 4, 5, 7, 8, 9,
Diver 2 Scores: 1, 2, 4, 6, 7, 9, 10,
Dive Difficulty: 2
Leader Board
--------------
Diver 2: 34
Diver 1: 32
Sample Run 2:
Diver 1 Scores: 5, 6, 6, 7, 7, 7, 8,
Diver 2 Scores: 1, 1, 2, 2, 3, 4, 5,
Dive Difficulty: 1
Leader Board
--------------
Diver 1: 20
Diver 2: 7
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.