***I have alredy completed this project but need help tweaking what I have. I ne
ID: 3668231 • Letter: #
Question
***I have alredy completed this project but need help tweaking what I have. I need to add the lowestScore method (in the instructions for the project), need to make sure all my methods are tested and I'm not sure how to do this (something about setting variables to 9999). Please comment on whatever you change so I will know and understand. Also, any help on drawing a simple class diagram would be helpfull to. I have copy and pasted the program that I have so far. Thanks!!
Project Outcomes:
Develop a Java program that uses:
decision constructs
looping constructs
basic operations on an Array of objects (find, change, access all elements)
more than one class and has multiple objects
static variables
Prep Readings:
Absolute Java textbook, Chapters 1 - 6.
Project Requirements:
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.
The Golfer Class
This is a class designed to represent a golfer including his name, home course, id number and an Array of Score Objects. The golfers score information consist of the score, course name where the score was shot, the par score for the course, the courses rating and course slope, and the date score was shot. Each field is explained in detail below in the explanation of the Score Class. 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.
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 all the golfer's scores. (NOTE: MUST USE AN ARRAY, CANNOT USE ANY OTHER TYPE OF JAVA COLLECTION OBJECT)
Static Variable - int nextIDNum - starts at 1000 and is used to generate the next Golfer's IDNum
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.
Accessor and mutator methods for all variables. 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, course rating, course slope, date and score. 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;
lowestScore - returns the Score object of the lowest score. Note in golf the lower the score the better. Returns null if no scores entered.
toString - returns a nicely formatted 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
The Score Class
Definitions for non-golfers ( for more definitions go to usga.org)
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 handicapp) 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.
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
Constructor - sets all instance fields from parameters
Default Constructor - sets all instance fields to a default value.
Accessor and mutator methods for all instance variables. Mutator method should be used to set all instance fields
toString - 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
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 prior to exiting.
Create UML Class Diagram for the final version of your project. The diagram should include
All instance variables, including type and access specifier (+, -);
All methods, including parameter list, return type and access specifier (+, -);
The Golfer and Score class, the GolferTester does not need to be included.
Refer to the UML Distilled pdf on the content page as a reference for creating class diagrams.
A Link to a drawing program, Dia, is also posted on the content page
Challenge (bonus 5 points) - use a GregorianCalendar object to handle the date in the score class.
Bonus (5 points) - Turn in a self graded grade sheet.
Submission Requirements:
Your project must be submitted using the instructions below. Any submissions that do not follow the stated requirements will not be graded.
1. You should have 5 files for this assignment:
Golfer.java The Golfer class
Score.java The Score class
GolfterTester.java A driver program for your Golfer class
Simply UML Class diagram of your two Classes (Golfer and Score)(Dia file or image file , jpg, gif, pdf etc)
Self graded Grade sheet.
Golfer.html - The javadoc file for the Golfer Class. (Do not turn in)
Score.html - The javadoc file for the Golfer Class(Do not turn in)
2. Remember to compile and run your program one last time before you submit it. If your program will not compile, the graders will not be responsible for trying to test it.
_________________________________________________________________________________
Golfer Class
public class Golfer
{
private String name;
private String homeCourse;
private int idNum;
private Score[] scores;
private int numOfScores;
private static int nextIDNum = 1000;
private final int NOTFOUND = -1;
public Golfer()
{
name = "";
homeCourse = "";
idNum = 0;
scores = new Score[10];
numOfScores = 0;
nextIDNum = 1000;
}
public Golfer(String aName, String aHomeCourse)
{
setName(aName);
setHomeCourse(aHomeCourse);
setIDNum();
numOfScores = 0;
nextIDNum++;
scores = new Score[10];
}
public String getName()
{
return name;
}
public String getHomeCourse()
{
return homeCourse;
}
public int getIDNum()
{
return idNum;
}
public void setName(String aName)
{
name = aName;
}
public void setHomeCourse(String aHomeCourse)
{
homeCourse = aHomeCourse;
}
public void setIDNum()
{
idNum = nextIDNum + 1;
nextIDNum++;
}
public void addScore(String aCourse, int aScore, double aCourseRating, int aCourseSlope, String aDate)
{
Score scr = new Score(aCourse, aScore, aCourseRating, aCourseSlope, aDate);
scores[numOfScores] = scr;
numOfScores++;
}
public boolean deleteScore(String aDate)
{
int index = findScore(aDate);
if(index == NOTFOUND)
return false;
while(index < numOfScores - 1)
{
scores[index] = scores[index + 1];
index++;
}
scores[index] = null;
numOfScores--;
return true;
}
public Score getScore(String aDate)
{
int index = findScore(aDate);
if(index == NOTFOUND)
return null;
return scores[index];
}
private int findScore(String aDate)
{
for(int i = 0; i < numOfScores; i++)
{
if(scores[i].equals(aDate))
return i;
}
return NOTFOUND;
}
public String toString()
{
String result = "";
result = result + String.format("%-13s ID number: %-6d Home Course: %-13s ", getName(), getIDNum(), getHomeCourse());
result = result + String.format("%-13s%-16s%-18s%-16s%-13s ", "Score", "Date", "Course", "Course Rating", "Course Slope");
for(int i = 0; i < numOfScores; i++)
{
result = result + scores[i];
}
return result;
}
}
Score Class
public class Score
{
private String course;
private int score;
private double courseRating;
private int courseSlope;
private String date;
public Score()
{
course = "";
score = 9999; //I just changed these to 9999 for testing if that is correct?
courseRating = 9999;
courseSlope = 9999;
date = "";
}
public Score(String aCourse, int aScore, double aCourseRating, int aCourseSlope, String aDate)
{
setCourse(aCourse);
setScore(aScore);
setCourseRating(aCourseRating);
setSlope(aCourseSlope);
setDate(aDate);
}
public String getCourse()
{
return course;
}
public int getScore()
{
return score;
}
public double getCourseRating()
{
return courseRating;
}
public int getSlope()
{
return courseSlope;
}
public String getDate()
{
return date;
}
public void setCourse(String aCourse)
{
course = aCourse;
}
public void setScore(int aScore)
{
if(aScore >= 40 && aScore <= 200)
score = aScore;
else
System.out.println("Invalid Score!");
}
public void setCourseRating(double aCourseRating)
{
if(aCourseRating >= 60 && aCourseRating <= 80)
courseRating = aCourseRating;
else
System.out.println("Invalid Course Rating!");
}
public void setSlope(int aCourseSlope)
{
if(aCourseSlope >= 55 && aCourseSlope <= 155)
courseSlope = aCourseSlope;
else
System.out.println("Invalid Slope!");
}
public void setDate(String aDate)
{
date = aDate;
}
public String toString()
{
return String.format("%-13d%-16s%-18s%-16.2f%-13d ", getScore(), getDate(), getCourse(), getCourseRating(), getSlope());
}
}
GolferTester
import java.util.*;
public class GolferTester
{
public static void main(String[] args)
{
Golfer golfer = new Golfer("John Smith", "Bay Hill CC");
golfer.addScore("Bay Hill CC", 75, 69.5, 123, "6/3/12");
golfer.addScore("AC Read", 77, 70.4, 128, "7/23/12");
System.out.println(golfer);
}
}
Explanation / Answer
// Golfer.java
public class Golfer
{
private String name;
private String homeCourse;
private int idNum;
private Score[] scores;
private int numOfScores;
private static int nextIDNum = 1000;
private final int NOTFOUND = -1;
public Golfer()
{
name = "";
homeCourse = "";
idNum = 0;
scores = new Score[10];
numOfScores = 0;
nextIDNum = 1000;
}
public Golfer(String aName, String aHomeCourse)
{
setName(aName);
setHomeCourse(aHomeCourse);
setIDNum();
numOfScores = 0;
nextIDNum++;
scores = new Score[10];
}
public String getName()
{
return name;
}
public String getHomeCourse()
{
return homeCourse;
}
public int getIDNum()
{
return idNum;
}
public void setName(String aName)
{
name = aName;
}
public void setHomeCourse(String aHomeCourse)
{
homeCourse = aHomeCourse;
}
public void setIDNum()
{
idNum = nextIDNum + 1;
nextIDNum++;
}
public void addScore(String aCourse, int aScore, double aCourseRating, int aCourseSlope, String aDate)
{
Score scr = new Score(aCourse, aScore, aCourseRating, aCourseSlope, aDate);
scores[numOfScores] = scr;
numOfScores++;
}
public boolean deleteScore(String aDate)
{
int index = findScore(aDate);
if(index == NOTFOUND)
return false;
while(index < numOfScores - 1)
{
scores[index] = scores[index + 1];
index++;
}
scores[index] = null;
numOfScores--;
return true;
}
public Score getScore(String aDate)
{
int index = findScore(aDate);
if(index == NOTFOUND)
return null;
return scores[index];
}
private int findScore(String aDate)
{
for(int i = 0; i < numOfScores; i++)
{
if(scores[i].equals(aDate))
return i;
}
return NOTFOUND;
}
public Score lowestScore(){
if(numOfScores == 0) return null;
Score min = scores[0];
for(int i = 1; i < scores.length; i++){
if(min.getScore() > scores[i].getScore()) min = scores[i];
}
return min;
}
public String toString()
{
String result = "";
result = result + String.format("%-13s ID number: %-6d Home Course: %-13s ", getName(), getIDNum(), getHomeCourse());
result = result + String.format("%-13s%-16s%-18s%-16s%-13s ", "Score", "Date", "Course", "Course Rating", "Course Slope");
for(int i = 0; i < numOfScores; i++)
{
result = result + scores[i];
}
return result;
}
}
// Score.java
public class Score
{
private String course;
private int score;
private double courseRating;
private int courseSlope;
private String date;
public Score()
{
course = "";
score = 9999; //I just changed these to 9999 for testing if that is correct?
courseRating = 9999;
courseSlope = 9999;
date = "";
}
public Score(String aCourse, int aScore, double aCourseRating, int aCourseSlope, String aDate)
{
setCourse(aCourse);
setScore(aScore);
setCourseRating(aCourseRating);
setSlope(aCourseSlope);
setDate(aDate);
}
public String getCourse()
{
return course;
}
public int getScore()
{
return score;
}
public double getCourseRating()
{
return courseRating;
}
public int getSlope()
{
return courseSlope;
}
public String getDate()
{
return date;
}
public void setCourse(String aCourse)
{
course = aCourse;
}
public void setScore(int aScore)
{
if(aScore >= 40 && aScore <= 200)
score = aScore;
else
System.out.println("Invalid Score!");
}
public void setCourseRating(double aCourseRating)
{
if(aCourseRating >= 60 && aCourseRating <= 80)
courseRating = aCourseRating;
else
System.out.println("Invalid Course Rating!");
}
public void setSlope(int aCourseSlope)
{
if(aCourseSlope >= 55 && aCourseSlope <= 155)
courseSlope = aCourseSlope;
else
System.out.println("Invalid Slope!");
}
public void setDate(String aDate)
{
date = aDate;
}
public String toString()
{
return String.format("%-13d%-16s%-18s%-16.2f%-13d ", getScore(), getDate(), getCourse(), getCourseRating(), getSlope());
}
}
// GolferTester.java
import java.util.*;
public class GolferTester
{
public static void main(String[] args)
{
Golfer golfer = new Golfer("John Smith", "Bay Hill CC");
golfer.addScore("Bay Hill CC", 75, 69.5, 123, "6/3/12");
golfer.addScore("AC Read", 77, 70.4, 128, "7/23/12");
System.out.println(golfer);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.