1. Create a java program with a double array named handicap that will contain th
ID: 3777158 • Letter: 1
Question
1. Create a java program with a double array named handicap that will contain the handicap that will contain the handicap index for each of the six players (6 element array). 2. You must use a method to calculate the handicap index for each player Method: calculateHandicap Argument: an int array containing one players scores (Note: the course rating, and the slope rating have been definded as Class Variables so these two variables may be used in the method) Method Body: Step through the array and calculate the players average score. Do not hardcode 10 in your for loop header. User player.length instead, assuming the int array is called. Player: Now,use this average score to calculate the players average differential. Multiply the average differential by .96 to get the Handicap index. Return Value: double - the Handicap index 3. Using a for loop, output each player number and the players handicap to 1 decimal place (see Sample Output). You must use System.out.printf or DecimalFormat to control for the number of decimal places. Do Not hardcode 6 in your for-loop header - use handicap.length instead. 4. Now,prompt the user for each of the six players new scores for new round of golf (winner takes all!). Use this score to populate a second array named netScore, which will contain the adjusted scores of the players(i.e, the score minus the handicap).Use netScore.length in your for-loop header (see Sample Output). 5. Step through netScore and determine which player has won the game . Again, use netScore.length in your for-loop header. The Lowest score wins! 6. Output both the net score (to 1 decimal place) and the player number of who won the round. That is, the element and index (the index repents the players). the course rating, which is a number generally between 67 and 77 that is used to measure the average "good score" on a course by a scratch golfer (plays to a course handicap of 0). the slope rating, which is a number between 55 and 155that describes the relative difficulty of a course for a bogey golfer (golfer who averages 1 bogey per hole) compared to a scratch golfer. These two numbers are used to calculate a players handicap differntial, which is used to adjust a players score in relation to poar, according to the course and slope and ratings. The slopes rating for a golf course of average diffculty is 113. Handicap Differntial= ((Gross Score-Course Rating)*113)/Slope Rating Handicap Index = (Average of 10 best differentials )*0.96 Net Score = Gross Score - Handicap The program below contains 10 best scores of 6 players, the course rating and, the slope rating. From these scores, determine the players handicaps. Then assuming these six players have just completed a tournment, prompt for thier gross score from the golf round. Determine their net scores and output the winner (note that the lowest score wins in golf). import java.util.*; import java.text.*; public class GolfHandicap { //Class Variables public static double slope = 119; public static double course = 69.3; //Main Method public static void main(String[] args) { int [] player1 = {91,89,90,90,85,87,95,81,91,89}; int [] player2 = {85,83,83,88,80,85,85,90,86,87}; int [] player3 = {79,78,85,76,79,75,74,77,75,78}; int [] player4 = {82,80,82,82,87,79,81,80,81,84}; int [] player5 = {95,98,94,95,97,98,99,97,99,96}; int [] player6 = {74,77,78,73,71,75,75,71,73,80}; } //Function calculateHandicap }
Explanation / Answer
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class GolfHandicap {
//Class Variables
public static double slopeRating = 119;
public static double courseRating = 69.3;
//Main Method
public static void main(String[] args) throws IOException {
int[] player1 = {91, 89, 90, 90, 85, 87, 95, 81, 91, 89};
int[] player2 = {85, 83, 83, 88, 80, 85, 85, 90, 86, 87};
int[] player3 = {79, 78, 85, 76, 79, 75, 74, 77, 75, 78};
int[] player4 = {82, 80, 82, 82, 87, 79, 81, 80, 81, 84};
int[] player5 = {95, 98, 94, 95, 97, 98, 99, 97, 99, 96};
int[] player6 = {74, 77, 78, 73, 71, 75, 75, 71, 73, 80};
double[] handicapIndexes = {GolfHandicap.calculateHandicap(player1),
GolfHandicap.calculateHandicap(player2),
GolfHandicap.calculateHandicap(player3),
GolfHandicap.calculateHandicap(player4),
GolfHandicap.calculateHandicap(player5),
GolfHandicap.calculateHandicap(player6),
};
//Printing Player Number and handicap index
for(int i=0;i<handicapIndexes.length;i++){
System.out.printf("Player Number%d Handicap Index=%.1f ",i+1,handicapIndexes[i]);
}
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//taking new score from user and calcualting net and stroing into array
double netScore[]=new double[6];
for(int i=0;i<handicapIndexes.length;i++){
System.out.println("Please Enter Score For Player "+(i+1)+" seperated by one space");
String[] scoreString =br.readLine().split(" ");
int[] newScore = new int[scoreString.length];
double grossScore = 0;
for(int j=0;j<scoreString.length;j++){
newScore[j]=Integer.parseInt(scoreString[j]);
grossScore+=newScore[i];
}
netScore[i]=grossScore-handicapIndexes[i];
}
double []tmp = netScore;
Arrays.sort(tmp);
for(int i=0;i<netScore.length;i++){
if(tmp[0]==netScore[i]){
System.out.printf("Player Number%d Handicap Index=%.1f ",i+1,netScore[i]);
}
}
}
//Function calculateHandicap
/**
*
* @param playerScore
* @return
*/
public static double calculateHandicap(int[] playerScore) {
int grossScore = 0;
double avg;
for (int i = 0; i < playerScore.length; i++) {
grossScore += playerScore[i];
}
avg = grossScore / playerScore.length;
double handicapDifferential = ((avg - courseRating) * 113) / slopeRating;
double handicapIndex = handicapDifferential * 0.96;
return handicapIndex;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.