Question: Use Java to 1. Implement an abstract class in Java to produce reusable classes. 2. Develop a program with several classes. 3. Extend the functionality

Use Java to 1. Implement an abstract class in Java to produce reusable classes. 2. Develop a program with several classes. 3. Extend the functionality of existing classes. 4. Develop a program using an ArrayList of object references. 5. Use method calls to perform logic steps involved in calculations. 6. Implement a solution involving polymorphism. Prep Readings: Java zyBooks Chapters 1 - 10. Project Requirements: 1. It turns out that Bowling uses a handicap system similar to golf. In both sports a handicap is used to allow players at different skill levels to compete. The basic objective of Programming Project 3 is to create classes that enable you to calculate player's handicap. The games you will be working with are Golf and Bowling. Both games require adding scores and calculating handicaps. This functionality can be realized by implementing inheritance including an abstract class and polymorphism. In this project will be extending the Score and Golfer class we developed in Project 2 and adding classes required to calculate a Bowlers handicap. Below are the steps we will use to calculate handicaps in both games. 2. Your code should ensure all instance field are valid as described in their requirements. If not create a user defined exception called FieldOutOfBounds that 1. Display an error message to the standard output stating that the field must be set between specific values and continue. 2. Set the field value to 9999. 3. For example, the bowling score must be between 0 and 300(inclusive) so the exception would be throw and the message would state: The bowlers score must be between 0 and 300(inclusive) 3. The Bowling handicap is calculated using the following steps: 1. Calculate the average of the bowlers last 5 scores. 2. Calculate the difference between the established base bowling average which in our case is 200 and the bowlers average. 3. Calculate the final handicap by taking 80% of the results of step 2. 4. Example 1. The bowlers last 5 scores average is 180. (Assume scores are entered from oldest to newest) 2. 200 - 180 = 20. 3. 80% of 20 is 16. 4. The bowlers handicap is 16 so we would add 16 to his next score. Note if the bowler's average is higher than 200, there handicap would be subtracted from their next score. 4. The Golf handicap index is calculated using the following steps: 1. Calculate the stroke differential of each the last 10 scores. (Assume scores are entered from oldest to newest) 1. Stroke differential is calculated using the following formula: 2. Differential = (Score - course Rating) x (113 / Slope) : round to 2 decimal places. 2. Calculate the average of the 5 lowest differentials from step 1. 3. Calculate the final handicap by taking 96% o f the results of step 2. round to 2 decimals places 4. Example 1. The golfers last 10 differentials are: 10.2, 11.5, 12.0, 9.8, 8.2, 9.9, 10.0 15.4, 12.3. 9.5. 2. The lowest 5 differentials are: 9.8, 8.2, 9.9, 10.0, 9.5 3. Average of 5 lowest differentials is: 47.4 / 5 = 9.48 4. 96% of 9.48 is 9.1 5. The handicap index is 9.1. Note that like bowling a golfer handicap can be a negative number. 5. The Classes from Project 2 (Golfer and Score) will be reused with the following modifications: 1. Golfer Class - Must extend the Player abstract class (listed below) 1. Change from Array of Score objects to ArrayList of Score Objects. 2. Add a calculateHandicap method that returns a double representing the golfers current handicap. (Player's class abstract method) 3. Modify methods as required to accommodate the changes in the Score class. 4. Note: that Name and IDnum are stored in the Player class. 5. The toString methods return should be format similar to Project 2. However, add the current handicap to the output. See Bowler class for an example. 2. Score Class 1. Modify the course instance variable from a String to a Course object (see below) that contains the course name, course rating and course slope. 2. Remove the course rating and course slope instance fields. 3. Modify the rest of the methods and constructor to accommodate the change in the storage of course information. 4. Score is a gross score thus without any handicap added or subtracted. 5. The toString methods return should be format similar to Project 2. 6. The Course class 1. Instance Variables 1. courseName - a String representing the name of the course. 2. courseRating - a double representing the course rating, it must be between 60 and 80. (inclusive). 3. courseSlope - an int representing the course slope, it must be between 55 and 155 (inclusive). 2. Methods 1. Constructor - sets all instance fields from parameters 2. 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 4. toString - returns a nicely formatted String that contains its instance fields such as: Bay Hill CC 69.5 123 7. The Bowler Class - Must extend the Player class listed below 1. Instance Variables 1. teamName - A String representing the bowler's team. 2. ArrayList of BowlerScore objects - stores all the bowler's scores. 2. Methods 1. Constructor - sets name and teamName from parameters and uses the static variable nextIDNum to retrieve the next available ID number. Creates ArrayList. 2. Constructor - default constructor, sets all instance field to a default value. Creates ArrayList. 3. A calculateHandicap method, that returns a double representing the golfers current handicap. (Player's class abstract method) 4. Access 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 5. addScore - receives a BowlerScore object and adds it to the ArrayList of BowlerScores. 6. toString - returns a nicely formatted String that contains its instance fields: John Smith ID number: 1234 Team Name: Waffle House Current Handicap: 16 Score Date Lane 139 6/3/14 Cordova Lanes 145 7/23/14 Felton Lanes 7. The BowlerScore Class 1. Instance Variables 1. laneName - a String representing the name of the lane 2. score - an int representing a game's score, it must be between 0 and 300 (inclusive), The score is the gross score, without adding or subtracting the handicap. 3. date - a String representing the date in format mm/dd/yy 2. Methods 1. Constructor - sets all instance fields from parameters 2. 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 4. toString - returns a nicely formatted String that contains the score and its instance fields 139 6/3/12 Cordova Lanes ? 8. The Player Class 1. Instance Variables 1. name - A String representing the player's name 2. IDNum- A unique integer that identifies each player 2. Static Variable - int nextIDNum - starts at 1000 and is used to generate the next Player's IDNum 3. Methods 1. Constructor - sets name and uses the static variable nextIDNum to retrieve the next available ID number. 2. Constructor - default constructor, sets all instance field to a default value. 3. Accessor and Mutator methods for instance variables. 4. toString - returns a neatly formatted String representing the player's information and all their scores. 5. calculateHandicap - abstract method that returns a double representing the players handicap 9. The PlayerTester 1. This class should consist 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. 2. Players, Golf Course and Score information will be read from a file. A sample file is provided. Use this file format to aid in grading. Using file processing makes entering and testing program with data sets much more efficient. 3. The file created should include valid and invalid data that fully tests the application. Simple text file processing is covered in additional Slide and example in the TextFileProcess.zip on our course web site. 4. Included file called data.txt contains Player's data. Your project will be graded using a similar input file. Your project must run with an input file formatted as data.txt is formatted. Note the file format is as follows: 1. Number of scores 2. Character designation of type of player (G - golfer, B - bowler) 3. Name data etc. 4. Score data ? 5. Create an ArrayList of Players objects. 1. Populate the ArrayList with both Golfer and Bowler objects. 2. After the ArrayList is filled, call the toString method on each object in the ArrayList. Ensure each iteration calls the correct object. 10. Create UML Class Diagram for the final version of your project. Follow the guidelines as outlined in Project 2. 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 10 files for this assignment: o Golfer.java - The Golfer class o Score.java - The Golfer's Score class o Course.java - The score's Course class o Bowler.java - The Bowler class o BowlerScore.java - The Bowler's Score class o Player.java - An abstract super class of players o PlayerTester.java - A driver program for your project o FieldOutOfBounds.java User defined exception class o data.txt

/** The Course class represents all item required to accurately depict a golf course

COP5007 Project #: 3 File Name: Course.java */

public class Course {

//Instance Variables //courseName - a String representing the name of the course. private String courseName; //CourseRating - a double representing the course rating, it must be between 60 and 80. (inclusive) private double courseRating; //courseSlope - an int representing the course slope, it must be between 55 and 155 (inclusive). private int courseSlope; //Methods //Constructor - sets all instance fields from parameters //Default Constructor - sets all instance fields to a default value. Chained... public Course() { this("Bay Hill CC", 69.5, 123); } public Course(String inCourseName, double inCourseRating, int inCourseSlope) { setCourseName(inCourseName); setCourseRating(inCourseRating); setCourseSlope(inCourseSlope); } //Access and mutator methods for all instance variables. Mutator method should be used to set all instance fields public String getCourseName() { return courseName; }//end getCost public double getCourseRating() { return courseRating; }//end getCourseRating public int getCourseSlope() { return courseSlope; }//end getCourseSlope

public void setCourseName(String inCourseName) { this.courseName = inCourseName; }//end setCourseName public void setCourseRating(double inCourseRating) { if ( 60 <= inCourseRating && inCourseRating <= 80 ) { this.courseRating = inCourseRating; } else { printError(); setCourseRating(80); } }//end setCourseRating public void setCourseSlope(int inCourseSlope) { //slope ratings range from a minimum of 55 (very easy) to a maximum of 155 (extremely difficult) if ( 55 <= inCourseSlope && inCourseSlope <= 155 ) { this.courseSlope = inCourseSlope; } else { printError(); setCourseSlope(155); } }//end setCourseSlope

//toString - returns a nicely formated String that contains its instance fields such as: public String toString() { String returnString = ""; returnString = getCourseRating() + "\t\t\t" + getCourseSlope() +"\t\t\t" + getCourseName() + " "; return returnString; } //printError public void printError() { System.out.println("Invalid input! Field set to max valid input."); }

}

---------------------------------------------------------------------------------------

File Name: Golfer.java

import java.text.DecimalFormat; public class Golfer implements Player {

//Instance Variables. //name - A String representing the golfer's name private String name; //homeCourse - A String representing the golf course where the player's handicap is keep. private String homeCourse; //IDNum - A unique integer that identifies each golfer. private int IDNum; //Array - stores all the golfer's scores. (NOTE: MUST USE AN ARRAY, CANNOT USE AN ARRAYLIST) private Score[] arrScore = new Score[9]; //Verbal requirement of no more than 10 scores //Static Variable - int nextIDNum - starts at 1000 and is used to generate the next Golfer's IDNum static int nextIDNum = 1000; private int numScores = 0;

//Constructor - default constructor, sets all instance field to a default value. Creates Array. //Example /* 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 */ public Golfer() { this("John Smith", "Bay Hill CC"); } //Methods //Constructor - sets name and homeCourse from parameters and uses the static variable nextIDNum to retrieve the next available ID number. Creates Array. public Golfer(String inName,String inHomeCourse) { setName(inName); setHomeCourse(inHomeCourse); setNextIDNum(nextIDNum++); setIDNum(getNextIDNum()); } public void setName(String inName) { this.name = inName; }//end setName

public void setHomeCourse(String inHomeCourse) { this.homeCourse = inHomeCourse; }//end setHomeCourse

public void setIDNum(int inIDNum) { this.IDNum = inIDNum; }//end setIDNum

public void setNextIDNum(int innextIDNum) { this.IDNum = innextIDNum; }//end setIDNum

public String getName() { return name; }//end getName

public String getHomeCourse() { return homeCourse; }//end getHomeCourse

public int getIDNum() { return IDNum; }//end getIDNum

public int getNextIDNum() { return IDNum; }//end getIDNum //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. public void addScore(int inScore, String inDate, String inCourseName, double inCourseRating, int inCourseSlope) {

Score newScore = new Score(inScore, inDate, inCourseName, inCourseRating, inCourseSlope); arrScore[numScores++] = newScore;

} //found use for overload with score object pass public void addScore(Object inScore) { arrScore[numScores++] = (Score) inScore; } public boolean deleteScore(String inDate) { if (findScore(inDate) == -1) { return false; } else { arrScore[findScore(inDate)]=null; return true; } } //getScore - returns a score object based on the score date. If not found returns null; public Score getScore(String inDate) { if (findScore(inDate) == -1) { return null; } else { Score foundScore = arrScore[findScore(inDate)]; return foundScore; } } //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; public int findScore(String inDate) { final int NOTFOUND = -1; int index = -1; for (int i=0; i< arrScore.length; i++) { if ( arrScore[i] != null) { //by val by ref so I cast if (arrScore[i].getDate().contains(inDate) ) index = i; } } if (index == -1) return NOTFOUND; else return index; } //lowestScore - returns the Score object of the lowest score. Note in golf the lower the score the better. Returns null if no scores entered. public Score lowestScore() { int lowIndex = 0; if ( arrScore.length != 0) { for (int i=0; i< arrScore.length; i++) { if ( arrScore[i] != null) { if ( arrScore[i].getScore() < arrScore[lowIndex].getScore()) { lowIndex = i; } } } return arrScore[lowIndex]; }else { return null; } }

public String toString() { String returnString = getName() + "\t"+ "\t"+"ID number: " + getIDNum() + "\t"+"\t"+"Home Course: " + getHomeCourse() + " " + " "; returnString += "Score" + "\t\t" + "Date" + "\t\t\t" + "Course Rating" + "\t\t" + "Course Slope" + "\t\t" + "Course" + " " ; String scores = ""; for (int i=0; i< arrScore.length; i++) { if ( arrScore[i] != null) scores = scores + arrScore[i].toString(); } returnString = returnString + scores; return returnString; }

//A calculateHandicap method, that returns a double representing the golfers current handicap. public double calculateHandicap() { double handiCap = 0.0; //diff //ToDo round to 2 decimal places. DecimalFormat df = new DecimalFormat("####0.00"); //Calculate the stroke differential of each the last 10 scores. double diff = 0.0; Course scoreCourse; //Stroke differential is calculated using the following formula: //Differential = (Score - course Rating) x (113 / Slope) : round to 2 decimal places. for (int i=0; i< arrScore.length; i++) { if ( arrScore[i] != null) { //setCourse(inCourseName, inCourseRating, inCourseSlope); scoreCourse = arrScore[i].getCourse(); diff = diff + ( arrScore[i].getScore() - scoreCourse.getCourseRating() )* ( 113/ scoreCourse.getCourseSlope() ) ; } } //Calculate the average of the 5 lowest differentials from step 1. handiCap = (double) diff/5;//assumption from video already sorted //Calculate the final handicap by taking 96% of the results of step 2. round to 2 decimals places handiCap = Double.valueOf( df.format(handiCap*(.96) ) ); return handiCap; } }

----------------------------------------------------------------------------------------------

File Name: Score.java */ import java.util.*; import java.text.*;

public class Score { //Instance Variables //courseName - a object representing the name of the courseName, courseRating and courseSlope. private Course aCourse; //score - an int representing a 18 hole score, it must be between 40 and 200 (inclusive) private int score;//this is one reason I like to name variables like intScore //date - a String representing the date in format mm/dd/yy private String date;

//Default Constructor - sets all instance fields to a default value. Chained... public Score() { this(75, "6/3/12", "Bay Hill CC", 69.5, 123); }

//Constructor - sets all instance fields from parameters // 75 6/3/12 Bay Hill CC 69.5 123 public Score(int inScore, String inDate, String inCourseName, double inCourseRating, int inCourseSlope) { setScore(inScore); setDate(inDate); setCourse(inCourseName, inCourseRating, inCourseSlope); }

//Access and mutator methods for all instance variables. Mutator method should be used to set all instance fields

public int getScore() { return score; }//end getScore public String getDate() { return date; }//end getDate

public Course getCourse() { return aCourse; }//end getDate public void setScore(int inScore) { if ( 40 < inScore && inScore < 200 ) { this.score = inScore; } else { printError(); setScore(199); } }//end setScore public void setDate(String inDate) { //regex to whitelist dates else set default to y2k if ( inDate.matches("\\d{2}/\\d{2}/\\d{4}") || inDate.matches("\\d{1}/\\d{1}/\\d{4}") || inDate.matches("\\d{2}/\\d{1}/\\d{4}") || inDate.matches("\\d{1}/\\d{2}/\\d{4}")) { //Use below without Gregorian Calendar //this.date = inDate; //GregorianCalendar +5 points //GregorianCalendar(int year, int month, int date) String[] arrDatePart = inDate.split("[/]"); GregorianCalendar gcDate = new GregorianCalendar(Integer.parseInt("20"+arrDatePart[2].toString()), Integer.parseInt(arrDatePart[0])-1, Integer.parseInt(arrDatePart[1]) ); Date d = gcDate.getTime(); DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT); String s = df.format(d); this.date = s; } else { printError(); setDate("01/01/0000");//y2k } }//end setDate

public void setCourse(String inCourseName, double inCourseRating, int inCourseSlope) { this.aCourse = new Course(inCourseName, inCourseRating, inCourseSlope); }

public String toString() { String returnString = ""; returnString = getScore() + "\t\t" + getDate() + " \t\t"; returnString = returnString + aCourse.toString(); return returnString; } //printError public void printError() { System.out.println("Invalid input! Field set to max valid input."); }

}

-----------------------------------------------------------------------------------------------

File Name: Bowler.java */ import java.util.*; public class Bowler implements Player { //Instance Variables private String name; //homeLane - A String representing the bowling lane where the player's handicap is keep private String homeLane; //IDNum - A unique integer that identifies each bowler. private int IDNum; //ArrayList<>BowlerScore> - stores all the bowler's scores. ArrayList scores = new ArrayList(); //Verbal requirement of no more than 10 scores //Static Variable - int nextIDNum - starts at 1000 and is used to generate the next bowler static int nextIDNum = 1000; //Methods //Constructor - default constructor, sets all instance field to a default value. Creates ArrayList. public Bowler() { this("John Smith", "Codova Lanes"); } //Constructor - sets name and homeLane from parameters and uses the static variable nextIDNum to retrieve the next available ID number. Creates ArrayList. public Bowler(String inName,String inHomeLane) { setName(inName); setHomeLane(inHomeLane); setNextIDNum(nextIDNum++); setIDNum(getNextIDNum()); } //A calculateHandicap method, that returns a double representing the bowlers current handicap. public double calculateHandicap() { double avg = 0; double handiCap= 0; //safety checks if (scores == null || scores.isEmpty()) return 0.0; String strTemp; for(int i = 0; i < 5 ; i++) { avg = avg + scores.get(i).getScore(); } //not sure about integer division so cast... avg = ((double) avg)/5; //Calculate the difference between the base bowling average which in our case is 200 and the bowlers average. handiCap = 200-avg; //Calculate the final handicap by taking 80% of the results of step 2. handiCap = (handiCap*.8); return handiCap; /*Example The bowlers last 5 scores average is 180. 200 - 180 = 20. 80% of 20 is 16. The bowlers handicap is 16 so we would add 16 to his next score. Note if the bowler's average is higher than 200, there handicap would be subtract from their next score. */ } //Access 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 public void setName(String inName) { this.name = inName; }//end setName

public void setHomeLane(String inHomeLane) { this.homeLane = inHomeLane; }//end setHomeLane

public void setIDNum(int inIDNum) { this.IDNum = inIDNum; }//end setIDNum

public void setNextIDNum(int innextIDNum) { this.IDNum = innextIDNum; }//end setIDNum

public String getName() { return name; }//end getName

public String getHomeLane() { return homeLane; }//end gethomeLane

public int getIDNum() { return IDNum; }//end getIDNum

public int getNextIDNum() { return nextIDNum; }//end getIDNum //addScore - receives a BowlerScore object and adds it to the ArrayList of BowlerScores. (Interface method) public void addScore(int inScore, String inDate, String inLaneName) { BowlerScore newScore = new BowlerScore(inScore, inDate, inLaneName); scores.add(newScore);

}

//addScore - receives a BowlerScore object and adds it to the ArrayList of BowlerScores. (Interface method) public void addScore(Object inScore) { scores.add( (BowlerScore) inScore);

} //toString - returns a nicely formated String that contains its instance fields: public String toString() { String returnString = getName() + "\t"+ "\t"+"ID number: " + getIDNum() + "\t"+"\t"+"Home Lane: " + getHomeLane() + " " + " "; returnString += "Score" + "\t\t" + "Date" + "\t\t\t" + "Lane" + " " ; String strScores = ""; for (int i=0; i< scores.size(); i++) { if ( scores.get(i) != null) strScores = strScores + scores.get(i).toString(); } returnString = returnString + strScores; return returnString; }

}

--------------------------------------------------------------------------------------------------

File Name: BowlerScore.java */ import java.util.*; import java.text.*; public class BowlerScore { //laneName - a String representing the name of the lane private String laneName; //date - a String representing the date in format mm/dd/yy private String date; //score - an int representing a game's score, it must be between 0 and 300 (inclusive) private int score; //Default Constructor - sets all instance fields to a default value. Chained... public BowlerScore() { this(139, "6/3/12", "Cordova Lanes"); } //Constructor - sets all instance fields from parameters public BowlerScore(int inScore, String inDate, String inLaneName) { setScore(inScore); setDate(inDate); setLaneName(inLaneName); }

//Access and mutator methods for all instance variables. Mutator method should be used to set all instance fields

public int getScore() { return score; }//end getScore public String getDate() { return date; }//end getDate

public String getLaneName() { return laneName; }//end getCost //139 6/3/12 Codova Lanes //between 0 and 300 (inclusive) public void setScore(int inScore) { if ( 0 <= inScore && inScore <= 300 ) { this.score = inScore; } else { printError(); setScore(0); } }//end setScore public void setDate(String inDate) { //regex to whitelist dates else set default to y2k if ( inDate.matches("\\d{2}/\\d{2}/\\d{2}") || inDate.matches("\\d{1}/\\d{1}/\\d{2}") || inDate.matches("\\d{2}/\\d{1}/\\d{2}") || inDate.matches("\\d{1}/\\d{2}/\\d{2}")) { //Use below without Gregorian Calendar //this.date = inDate; //GregorianCalendar +5 points //GregorianCalendar(int year, int month, int date) String[] arrDatePart = inDate.split("[/]"); GregorianCalendar gcDate = new GregorianCalendar(Integer.parseInt("20"+arrDatePart[2].toString()), Integer.parseInt(arrDatePart[0])-1, Integer.parseInt(arrDatePart[1]) ); Date d = gcDate.getTime(); DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT); String s = df.format(d); this.date = s; } else { printError(); setDate("01/01/00");//y2k } }//end setDate

public void setLaneName(String inLaneName) { this.laneName = inLaneName; }//end setLaneName

//toString - returns a nicely formated String that contains the score and its instance fields //139 6/3/12 Codova Lanes public String toString() { String returnString = ""; returnString = getScore() + "\t\t" + getDate() + " \t\t" + getLaneName() + " "; return returnString; } //printError public void printError() { System.out.println("Invalid input! Field set to max valid input."); }

}

-----------------------------------------------------------------------------------------------------------

File Name: PlayerTester.java

*/

import java.util.Scanner;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.util.*;

public class PlayerTester

{

//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.

public static void main(String[] args)

{

//-------------------------------------------------------------------------------------

//Score(int inScore, String inDate, String inCourseName, double inCourseRating, int inCourseSlope)

Score myScore = new Score();

System.out.println( myScore.toString() );

Score myScore2 = new Score(98, "9/8/83", "Bay CC ", 60, 123);

System.out.println( myScore2.toString() );

System.out.println("Finished with Score testing ");

System.out.println("-------------------------Begin Testing Golfer-----------------");

Golfer[] myGolfer = new Golfer[9];

myGolfer[0] = new Golfer();

myGolfer[0].addScore(55,"9/9/99", "Jackson CC", 70,123);

myGolfer[0].addScore(66,"9/9/00", "Some other place", 71,123);

myGolfer[1] = new Golfer("Bob Barker", "The Price is Wrong");

myGolfer[1].addScore(77, "9/8/83", "Bay CC ", 72, 123);

myGolfer[2] = new Golfer("Bad InputGuy", "Numbers all wrong");

System.out.println("3 bad numeric inputs coming up....");

myGolfer[2].addScore(-5, "10/12/14", "Bay CC", 5.10, 222222);

System.out.println("-----------------------------Testing getscore before deleting it");

myGolfer[2].addScore(myGolfer[0].getScore("9/9/99"));

System.out.println("Lowest score coming up....");

System.out.println(myGolfer[0].toString());

System.out.println("Lowest score from above: " + myGolfer[0].lowestScore().toString() );

System.out.println("Golfer getScore returns next line...." +" "+ myGolfer[0].toString());

System.out.println("Deleting score 9/9/00....");

if (myGolfer[0].deleteScore("9/9/00") ) System.out.println("Deleted score from 9/9/00 ");

for (int i=0; i< myGolfer.length; i++)

{

if ( myGolfer[i] != null)

{

System.out.println(myGolfer[i].toString());

}

}

//--------------------------------Project 2 Still Works------------------------------------

//Players, Golf Course and Score information will be read from a file. Since we haven't covered file processing yet, a sample file is provided that shows how this is done. Using file processing makes entering and testing program with data sets much more efficient.

//The file created should include valid and invalid data that fully tests the application.

//Included is data.txt which is a sample input file that contains Player's data. Your project will be graded using an similar input file.

//Your project must run with an input file formated as data.txt is formated. Note the file format is as follows:

//Number of scores

//Character designation of type of player (G - golfer, B - bowler)

//Name data etc

//Score data

//Create an ArrayList of Players (Interface type).

//Populate the ArrayList with both Golfer and Bowler objects.

//After the ArrayList is filled, call the toString method on each object in the Arraylist. Ensure each iteration calls the correct object.

ArrayList aListOfPlayers = new ArrayList();

Scanner fileIn = null; // Initializes fileIn to an empty object

try

{

// Attempt to open the file

FileInputStream file = new FileInputStream("data.txt");

fileIn = new Scanner(file);

int numOfScores = 0;

String typeScore = "";

String playerCourse = "";

String scoreData = "";

String player="";

String course="";

while(fileIn.hasNextLine())

{

/*

numOfScores = fileIn.nextInt();

fileIn.nextLine();

typeScore = fileIn.nextLine();

playerCourse = fileIn.nextLine();

System.out.println("There are " + numOfScores + " " + typeScore + " for " + playerCourse);

System.out.println("The scores are: ");

*/

//numOfScores = fileIn.nextInt();

numOfScores = Integer.parseInt(fileIn.nextLine() );

//todo -- make sure this is typescore not whole line

typeScore = fileIn.nextLine().toString();

//System.out.println(typeScore);

playerCourse = fileIn.nextLine().toString();

//System.out.println(playerCourse);

String delims = "[,]";

//Player at i = 0 -------------course at i = 1

String[] arrPlayerCourse = playerCourse.split(delims);

//System.out.println(arrPlayerCourse[0].toString());

//System.out.println(arrPlayerCourse[1].trim().toString());

//for splitting the score into parts

String[] arrScoreParts;

//System.out.println("Before type compare");

//Populate the ArrayList with both Golfer and Bowler objects.

if (typeScore.contains("G") )

{

//System.out.println("Before making myGolfer");

Golfer myAutoGolfer = new Golfer(arrPlayerCourse[0].trim().toString(),arrPlayerCourse[1].trim().toString());

//System.out.println("Outside for loop 1");

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

{

//System.out.println("Inside for loop 1");

scoreData = fileIn.nextLine().toString();

//System.out.println(scoreData.toString() );

arrScoreParts = scoreData.split(delims);

//System.out.println("About to add score");

//used for reference -- myGolfer.addScore(77, "9/8/83", "Bay CC ", 72, 123);

// ------------------0 Course, 1 Score , 2 date, 3 rating, 4 slop

myAutoGolfer.addScore(Integer.parseInt(arrScoreParts[1].trim().toString()), arrScoreParts[2].trim().toString(),arrScoreParts[0].trim().toString(), Double.parseDouble(arrScoreParts[3].trim().toString()), Integer.parseInt(arrScoreParts[4].trim().toString()) );

//System.out.println("addScore(int inScore, String inDate, String inCourseName, double inCourseRating, int inCourseSlope)" + arrScoreParts[1].trim().toString() + arrScoreParts[2].trim().toString() +arrScoreParts[0].trim().toString() + arrScoreParts[3].trim().toString() + arrScoreParts[4].trim().toString() );

}

if ( numOfScores >= 5)

{

System.out.println( myAutoGolfer.calculateHandicap() );

}

aListOfPlayers.add((Player)myAutoGolfer);

}

if (typeScore.contains("B") )

{

//For reference addScore(inScore, inDate, inLaneName)

Bowler myBowler = new Bowler(arrPlayerCourse[0],arrPlayerCourse[1]);

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

{

scoreData = fileIn.nextLine();

arrScoreParts = scoreData.split(delims);

//System.out.println(arrScoreParts[1] + "-----" + arrScoreParts[2] + "-----" + arrScoreParts[0]);

//Integer.parseInt(arrScoreParts[1]), arrScoreParts[2],arrScoreParts[0]

BowlerScore myBowlerScore = new BowlerScore(Integer.parseInt(arrScoreParts[1].trim().toString()), arrScoreParts[2].trim(),arrScoreParts[0].trim());

myBowler.addScore(myBowlerScore);

}

if ( numOfScores >= 5)

{

System.out.println( myBowler.calculateHandicap() );

}

aListOfPlayers.add((Player)myBowler);

}

//After the ArrayList is filled, call the toString method on each object in the Arraylist.

//Ensure each iteration calls the correct object.--not sure what this means

for (Player aPlayer : aListOfPlayers)

{

System.out.println( aPlayer.toString() );

}

}

}

catch (FileNotFoundException e)

{

System.out.println("File not found.");

System.exit(0);

}

finally//fixes potential resource leaks

{

fileIn.close();

}

}

}

-----------------------------------------------------------------------------

File Name: Player.java */

public interface Player {

public double calculateHandicap();

//addScore - Takes in a parameter of type Objects that represent the a particular games score object. public void addScore(Object inObject); //toString - returns a neatly formated String representing the player and all his scores. public String toString();

}

------------------------------------------------------------------------------

data.txt

6 G Richard, AC Read Solutia,75,10/23/2013,70.5,113 ACRead,82,01/21/2014,68.5,123 Solutia,75,10/23/2013,70.5,113 ACRead,82,01/21/2014,68.5,123 Solutia,75,10/23/2013,70.5,113 ACRead,82,01/21/2014,68.5,123 3 B Joe Johnson, Felton Lanes Felton Lanes, 112,09/22/2012 Bowlarama, 142,09/24/2012 Cordova Lanes,203,09/24/2012 2 G Tiger Woods, Isleworth Augusta,68,04/08/2013,76.2,135 Bethpage Black,71,06/21/2012,76.6,144 5 B Earl Anthony , Firestone Lanes Firestone Lanes, 223,09/22/1968 Bowlarama, 246,09/24/1976 Firestone Lanes, 223,09/22/1968 Bowlarama, 246,09/24/1976 Cordova Lanes,210,09/24/1977 6 G Richard, AC Read Solutia,999,999/23/2013,999,999 ACRead,999,01/21/2014,68.5,123 Solutia,75,999,70.5,113 ACRead,82,01/21/2014,999,123 Solutia,75,10/23/2013,70.5,999 ACRead,82,01/21/2014,68.5,123 6 B Earl Anthony , Firestone Lanes Firestone Lanes, 223,09/22/1968 Bowlarama, 999,999/24/9999 Cordova Lanes,210,09/24/1977 Firestone Lanes, 223,09/22/1968 Bowlarama, 246,09/24/1976 Cordova Lanes,210,09/24/1977

------------------------------------------------------------------------------------------

I have 2 problems here in the above solution to the Requirements

1) I need to make user defined exception called FieldOutOfBounds that Display an error message to the standard output stating that the field must be set between specific values and continue.

Set the field value to 9999.

For example, the bowling score must be between 0 and 300(inclusive) so the exception would be throw and the message would state: The bowlers score must be between 0 and 300(inclusive)

2) I have a strange bug with my date conversion that set many valid dates to invalid most errors resolved to array parse not having trim but others persist. The default construtor and parametrized constructor giving Invalid dates 1/1/00...

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!