We are going to develop a program that will handle a Golfer and his scores. The
ID: 655283 • Letter: W
Question
We are going to develop a program that will handle a Golfer and his scores. The program will be comprised of three classes: a Golfer class that will manage all the golfer's scores, a Score class and a Tester class to drive the other classes. To accomplish this task the program will use a partially filled array to store the golfers scores.
The Golfer Class
1. This is a class designed to represent a golfer including his name, home course, id number and an Array of Score Objects. The golfers scores consist of several pieces of information include the course, the par score for the course, the courses rating and course slope, date score was shot and the score. Each field is explained in detail below. Remember part of being a strong programmer is being flexible enough to learn about new domains, you don't have to be a golfer or a doctor or a banker to write application in those domains.
2. Instance Variables.
name - A String representing the golfer's name
homeCourse - A String representing the golf course where the player's handicap is keep.
idNum - A unique integer that identifies each golfer.
scores an Array - stores Score object that represent the golfer's scores. Set the size of the array to 10 but use numOfScores instance variable(below) to track how many Score object are actually in the array. ((NOTE: MUST USE AN ARRAY, CANNOT USE ANY OTHER TYPE OF JAVA COLLECTION OBJECT)
numOfScores an int representing the number of Score Objects in the Array used in the partially filled array algorithm
3. Static Variable - int nextIDNum - starts at 1000 and is used to generate the next Golfer's IDNum
4. Methods
Constructor - sets name and homeCourse from parameters and uses the static variable nextIDNum to retrieve the next available ID number. Creates Array.
Constructor - default constructor, sets all instance field to a default value. Creates Array.
Access and mutator methods as identified in the UML Class Diagram below) NOTE Mutator method for IDNum should use the static variable nextIDNum. Mutator method should be used to set all instance fields
addScore - create a Score object from the parameters that represent the course, score, course rating, course slope, and date. Adds the newly created Score object to the Array of Scores.
deleteScore - delete a score from the Array based on score date, Assume only one score per day. Returns true if score found and deleted, false if not found.
getScore - returns a score object based on the score date. If not found returns null;
findScore - private method given a parameter representing the score's date, returns the Array index of a score. (Use in deleteScore and getScore). Return constant NOTFOUND if not found, NOTFOUND is set to -1;
toString - returns a nicely formated string of a Golfer's information and all their scores. Use the Score toString method to assist. It should looks something like
John Smith ID number: 1234 Home Course: Bay Hill CC
Score Date Course Course Rating Course Slope
75 6/3/12 Bay Hill CC 69.5 123
77 7/23/12 AC Read 70.4 128
Golfer
-name:String
-homeCourse:String
-idNum:int
-scores:Score[ ]
+ Golfer()
+ Golfer(String,String, int)
+ getName():String
+ getHomeCourse():String
+ getIDNum():int
+ setName(String)
+ setHomeCourse(String)
+ setIDNum()
+ addScore(String,int, double, int, String)
+ deleteScore(String):boolean
+ getScore(String):Score
+ findScore(String):int
+ toString():String
The Score Class
Definitions for non-golfers ( for more definitions go to usga.org)
1. course - the specific place where golf is played, has a name such as Augusta National or St. Andrews or A,C. Read.
score- The number of strokes taken during a round(18 holes) of golf.
course rating - indicates the evaluation of the playing difficulty of a course for a scratch golfer (zero handicap) under normal course and weather conditions. It is expressed as strokes taken to one decimal place, and is based on yardage and other obstacles to the extent that they affect the scoring ability of a scratch golfer.is the starting place for the hole to be played.
course slope - indicates the measurement of the relative difficulty of a course for players who are not scratch golfers. The lowest Slope Rating is 55 and the highest is 155. A golf course of standard playing difficulty has a Slope Rating of 113.
Instance Variables
courseName - a String representing the name of the course.
score - an int representing a 18 hole score, it must be between 40 and 200 (inclusive)
date - a String representing the date in format mm/dd/yy
courseRating - a double representing the course rating, it must be between 60 and 80. (inclusive)
courseSlope - an int representing the course slope, it must be between 55 and 155 (inclusive).
NOTE: Your code should ensure all instance field are valid as described above. If not write an error message to the standard output and continues. Set field to 9999
Methods
1. Constructor - sets all instance fields from parameters
Default Constructor - sets all instance fields to a default value.
3. Access and mutator methods for all instance variables. Mutator method should be used to set all instance fields
toSting - returns a nicely formatted String that contains the score and its instance fields that looks something like:
75 6/3/12 Bay Hill CC 69.5 123
Score
- course:String
- score:int
- courseRating:double
- courseSlope:int
+ Score()
+ Score(String,int,double,int)
+ getCourse():String
+ getScore():int
+ getCourseRating():double
+ getSlope():int
+ toString():String
The GolferTester Class
This class should consists of a main method that tests all the methods in each class, either directly by calling the method from the Tester or indirectly by having another method call a method.
The Tester should include valid and invalid data that fully tests the application. No user input , create enough Golfer object to fully test your program.
Ensure you display the contents of the Golfer's array list prior to exiting.
Golfer
-name:String
-homeCourse:String
-idNum:int
-scores:Score[ ]
+ Golfer()
+ Golfer(String,String, int)
+ getName():String
+ getHomeCourse():String
+ getIDNum():int
+ setName(String)
+ setHomeCourse(String)
+ setIDNum()
+ addScore(String,int, double, int, String)
+ deleteScore(String):boolean
+ getScore(String):Score
+ findScore(String):int
+ toString():String
Explanation / Answer
//Golfer.java
import javax.lang.model.type.NullType;
public class Golfer {
private static final int NOTFOUND = -1;
private String name;
private String homeCourse;
private int idNum;
private Score[] scores;
private int numOfScores;
private static int nextIdNum = 1000;
Golfer()
{
scores = new Score[10];
numOfScores = 0;
}
Golfer(String n,String hC)
{
scores = new Score[10];
numOfScores = 0;
name = n;
homeCourse = hC;
idNum = (nextIdNum++);
}
public String getName()
{
return name;
}
public String getHomeCourse()
{
return homeCourse;
}
public int getIDNum()
{
return idNum;
}
public void setName(String n)
{
name = n;
}
public void setHomeCourse(String hC)
{
homeCourse = hC;
}
public void setIDNum()
{
idNum = nextIdNum++;
}
public void addScore(String c,int s, double cR, int cS, String d)
{
scores[numOfScores++] = new Score(c, d, s, cR, cS);
}
public boolean deleteScore(String d)
{
int f = findScore(d);
if(f != -1)
{
numOfScores = numOfScores - 1;
for(int i=f; i<numOfScores; i++)
{
scores[i] = scores[i+1];
}
return true;
}
else
return false;
}
public Score getScore(String d)
{
int f = findScore(d);
if(f != -1)
return scores[f];
else
return null;
}
private int findScore(String d)
{
for(int i=0 ;i<numOfScores; i++)
{
if(scores[i].getDate().equals(d))
return i;
}
return NOTFOUND;
}
public String toString()
{
String t = "Name: " + name + " ID Number: " + idNum + " Home Course: " + homeCourse + " ";
t += "Score Date Course Course Rating Course Slope ";
for(int i=0 ;i<numOfScores; i++)
{
t += scores[i].toString() + " ";
}
return t;
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------
//Score.java
import javax.swing.plaf.SliderUI;
public class Score {
private String course;
private String date;
private int score;
private double courseRating;
private int courseSlope;
Score()
{
course = "default";
date = "00/00/00";
score = 40;
courseRating = 60;
courseSlope = 55;
}
Score(String c,String d, int s,double cR,int cS)
{
course = c;
date = d;
if(s >= 40 && s<=200)
score = s;
else
{
s=9999;
System.out.println("Score data must be between 40 - 200");
}
if(cR >= 60 && cR <= 80)
courseRating = cR;
else
{
cR = 9999;
System.out.println("course rating must be between 60 - 80");
}
if(cS >= 55 && cS <= 155)
courseSlope = cS;
else
{
cS = 9999;
System.out.println("course slope must be between 55 - 155");
}
}
public String getCourse()
{
return course;
}
public String getDate()
{
return date;
}
public int getScore()
{
return score;
}
public double getCourseRating()
{
return courseRating;
}
public int getSlope()
{
return courseSlope;
}
public String toString()
{
return score + " " + date + " " + course + " " + courseRating + " " + courseSlope;
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------
//GolfTester.java
public class GolfTester {
public static void main(String[] args){
Golfer g1 = new Golfer("John Smith", "Bay Hill CC");
g1.addScore("Bay Hill CC",75, 69.5, 123, "6/3/12");
g1.addScore( "AC Read",77, 70.4, 128,"7/23/12");
System.out.println("Displaying all scores: ");
System.out.println(g1.toString());
System.out.println("Deleting score date: 7/23/12 -> ");
g1.deleteScore("7/23/12");
System.out.println(g1.toString());
System.out.println("getScore date: 6/3/12 -> ");
System.out.println(g1.getScore("6/3/12"));
}
}
--------------------------------------------------------------------------------------------------
OUTPUT
-------------------------------------------------
Displaying all scores:
Name: John Smith
ID Number: 1000
Home Course: Bay Hill CC
Score Date Course Course Rating Course Slope
75 6/3/12 Bay Hill CC 69.5 123
77 7/23/12 AC Read 70.4 128
Deleting score date: 7/23/12 ->
Name: John Smith
ID Number: 1000
Home Course: Bay Hill CC
Score Date Course Course Rating Course Slope
75 6/3/12 Bay Hill CC 69.5 123
getScore date: 6/3/12 ->
75 6/3/12 Bay Hill CC 69.5 123
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.