209k views
5 votes
Design and implement a program to process golf scores. The scores of four golfers are stored in a text file (download golf.txt from moodle). Each line represents one hole, and the file contains 18 lines. Each line contains five values: par for the hole followed by the number of strokes each golfer used on that hole.1) Store the totals for par and the players in an ArrayList.2) Determine the winner and produce a table showing how well each golfer did (compared to par).3) Modify the StyleOptions program in chapter 5 to allow the user to specify the size of the font. Use a text field to obtain the size

User Sangsoo
by
3.3k points

1 Answer

1 vote

Answer:

Step-by-step explanation:

import java.io.*;

import java.util.*;

public class GolfScores

{

// *************************************************************

// the purpose of this program is to read golf scores between

// four players and figure out the winner with the lowest score

// **************************************************************

public static void main(String[] args) throws FileNotFoundException

{

final int HOLES = 28, VALUES_PER_LINE = 4;

int hole = 0;

int parscores[] = new int[HOLES];

//declare a file object to handle the text file with scores

File file1 = new File("golfscore.txt");

int sum0=0;

int sum1=0;

int sum2=0;

int sum3=0;

int sum4=0;

Scanner scan = new Scanner(file1);

// total par and scores for all holes

int scores[][] = new int[HOLES][VALUES_PER_LINE];

//loop to store the contents of the file into arrays

while (scan.hasNext())

{

int index = 0;

parscores[hole] = scan.nextInt();

while (index < 4)

{

scores[hole][index] = scan.nextInt(); // values in a line

index++;

}

//

hole++;

}

for(int i=0;i<HOLES;i++)

{

sum0+=parscores[i];

}

int sumarray[]=new int[4];

//loop to calculate the total score of each player

for(int row=0;row<HOLES;row++)

{

sum1+=scores[row][0];

sum2+=scores[row][1];

sum3+=scores[row][2];

sum4+=scores[row][3];

}

System.out.println();

System.out.println("Par for the course"+sum0);

System.out.println("Player 1:"+sum1);

System.out.println("Player 2:"+sum2);

System.out.println("Player 3:"+sum3);

System.out.println("Player 4:"+sum4);

sumarray[0]=sum1;

sumarray[1]=sum2;

sumarray[2]=sum3;

sumarray[3]=sum4;

int min=sumarray[0];

//Loop to find out the minimum score

for(int j=0;j<4;j++)

{

if(sumarray[j]<min)

min=sumarray[j];

}

System.out.print("The winner is ");

//condition statement to determine the winner

if(min==sum1)

{

System.out.print("Player1");

}

else if(min==sum2)

{

System.out.print("Player2");

}

else if(min==sum3)

{

System.out.print("Player3");

}

else

{

System.out.print("Player4");

}

}

}

cheers i hope this helped !!

User Nirodya Gamage
by
5.2k points