/* ********************* * * GolferScore - represents one player's score on an 18 hole golf course. * * Author: Shawn Brenneman and YOUR NAME * Date: 2019-Apr 6 * ***********************/ public class GolferScore { // instance data: private final int NUM_HOLES = 18; private int[] par; private int[] score; private String golfer; private String courseName; // a contstructor which takes a golfer's name, a course name, and an array // containing the par for each hole (integers) public GolferScore(String golfer, String course, int[] parForEachHole){ this.golfer = golfer; courseName = course; par = parForEachHole; score = new int[NUM_HOLES]; } // ADD THIS METHOD ------------------------------------------------------- // Overload the constructor to take an array of scores as well as // golfer's name, course name, and pars array // getters public String getGolfer() { return golfer; } public String getCourseName () { return courseName; } public int[] getPar() { return par; } public int[] getScore() { return score; } // ADD THIS METHOD ------------------------------------------------------- // compareTo - compares totalCourseScore for two players // setScore - takes a holeNumber and a golfer's score for that hole, // and sets it in the score array public void setScore(int holeNum, int strokes) { if (holeNum >0 && holeNum <= NUM_HOLES) { score[holeNum-1] = strokes; } } private int totalFrontNine() { int sum=0; for (int i=0; i<9; i++) { sum += score[i]; } return sum; } private int parFrontNine() { int parSum = 0; for(int i=0; i<9; i++) { parSum += par[i]; } return parSum; } private int totalBackNine() { int sum=0; for (int i=9; i 0) { strokeSum += score[i]; } } return strokeSum; } // parSumOfHolesPlayed - returns the total of par for holes that have been played public int parSumOfHolesPlayed() { int parSum=0; for(int i=0; i 0) { parSum += par[i]; } } return parSum; } // isOverPar - answers if the total score of holes played so far is over par public boolean isOverPar() { return (scoreSum() > parSumOfHolesPlayed()); } // isUnderPar - answers if the total score of holes played so far is under par public boolean isUnderPar() { return (scoreSum() < parSumOfHolesPlayed()); } // isUnderPar - answers if the total score of holes played so far is the same as par public boolean isPar() { return (scoreSum() == parSumOfHolesPlayed()); } // isOverPar - answers if the score for the given hole number is over par // NOTE: this is an overloaded method. it takes a parameter, unlike the // previous isOverPar method. Also, this method checks to see if the // given hole number is valid, otherwise, the method returns false. // This note applies to the next two methods as well. public boolean isOverPar(int holeNum) { boolean result = false; if (holeNum > 0 && holeNum <= 18) { result = (score[holeNum-1] > par[holeNum-1]); } return result; } // isUnderPar - answers if the score for the given hole number is under par // NOTE: if a score is zero, it means the hole has not been played yet. // In that case, this method should return false. public boolean isUnderPar(int holeNum) { boolean result = false; if (holeNum > 0 && holeNum <= 18 && score[holeNum-1] != 0) { result = (score[holeNum-1] < par[holeNum-1]); } return result; } // isPar - answers if the score for the given hole number is par public boolean isPar(int holeNum) { boolean result = false; if (holeNum > 0 && holeNum <= 18) { result = (score[holeNum-1] == par[holeNum-1]); } return result; } // numHolesUnderPar - returns the number of holes played that are under par // NOTE: if a score is zero, that means the hole has not yet been played // and should not be counted as under par. public int numHolesUnderPar() { int cnt=0; for(int i=1; i<=NUM_HOLES; i++) { if (isUnderPar(i)){ cnt++; } } return cnt; } // numHolesOverPar - returns the number of holes played that are over par public int numHolesOverPar() { int cnt=0; for(int i=1; i<=NUM_HOLES; i++) { if (isOverPar(i)){ cnt++; } } return cnt; } // numHolesPar - returns the number of holes played that are par public int numHolesPar() { int cnt=0; for(int i=1; i<=NUM_HOLES; i++) { if (isPar(i)){ cnt++; } } return cnt; } // numBirdies - returns the number of holes played that are birdies // NOTE: a birdie is one under par. public int numBirdies() { int cnt=0; for(int i=0; i totalCourseScore()) { int diff = parSumOfHolesPlayed() - totalCourseScore(); str += " " + diff + " under Par"; } else { int diff = totalCourseScore() - parSumOfHolesPlayed(); str += " " + diff + " over Par"; } str += "\n"; return str; } }