170k views
5 votes
You are writing a program to track the scores for the Masters. The Masters Golf tournament takes place over 4 days Thursday – Sunday. After the first 2 days, a cut-off score is calculated to reduce the size of the field for the weekend rounds that take place on Saturday and Sunday. To "make the cut", players must be either in the group of 50 players closest to the leader’s score (ties counting), or within 10 strokes of the leader's score. You are going to write a program that loads the player’s scores for the first 2 days of play into arrays, adds the scores to get a combined score, searches the combined score array to find out who has the lowest score (the leader), and displays a list of players who are within 10 strokes of the leader’s score. (As an example, if the leader scores a 140 after 2 days of play, then "within 10" means a score less than or equal to 150…anyone over 150 does not make the cut unless they are within the 50 players who are closest to the leader’s score.) We are only determining who is within 10 of the leader’s score.

1 Answer

1 vote

Answer:

The code is given below and output is attached

Step-by-step explanation:

//Java code

import java.util.Scanner;

public class GolfTournament {

public static void main(String[] args)

{

Scanner input = new Scanner(System.in);

System.out.print("Please enter the number of players: ");

int size = input.nextInt();

if(size<=0)

{

System.out.println("goodbye ");

return;

}

String[] names = new String[size];

int[] score1 = new int[size];

int[] score2 = new int[size];

int [] combinedScores = new int[size];

for (int i = 0; i <size ; i++) {

System.out.println(" -------------------------Player "+(i+1)+"------------------------- ");

System.out.print("Please enter the Player Name: ");

names[i] = getName();

System.out.print("Please enter the day 1 score: ");

score1[i] = getScore();

System.out.print("Please enter the day 2 score: ");

score2[i] = getScore();

combinedScores[i] = score1[i]+score2[i];

}

System.out.println("------------------------------------------------Tournament Statistics-----------------------------------------");

System.out.println("PlayerID\t\tDay1 Score\t\t Day2 Score\t\tCombined Score");

for (int i = 0; i <size ; i++) {

System.out.println(String.format("%10s %15s %15s %15s",names[i],score1[i],score2[i],combinedScores[i]));

}

System.out.println("--------------------------------------------------------- Leader ----------------------------------------------------");

System.out.println(names[findMinIndex(combinedScores)]);

System.out.println(" ---------------------------------------Players Within 10 Strokes of Leader --------------------------------- ");

int index = findMinIndex(combinedScores);

int valueWithin10 = combinedScores[index]+10;

for (int i = 0; i < size; i++) {

if(combinedScores[i]>combinedScores[index] && combinedScores[i]<=valueWithin10)

System.out.println(names[i]);

}

System.out.println("--------------------------------------------------See Ya’ At the 19th Hole---------------------------------------");

}

private static int findMinIndex(int[] c)

{

int index =0;

for (int i = 0; i <c.length ; i++) {

if(c[index]>c[i])

{

index = i;

}

}

return index;

}

private static boolean validateRange(int score)

score>95)

return true;

else

return false;

private static String getName()

{

Scanner input = new Scanner(System.in);

return input.nextLine();

}

private static int getScore()

{

Scanner input = new Scanner(System.in);

int score = input.nextInt();

while (validateRange(score))

{

System.err.println("ERROR... Score is not valid...");

System.out.println("Enter score: ");

score = input.nextInt();

}

return score;

}

}

You are writing a program to track the scores for the Masters. The Masters Golf tournament-example-1
User Keisha W
by
5.7k points