Skip to main content
留学咨询

辅导案例-CS 113

By May 15, 2020No Comments

CS 113 Assignment Working with classes and objects (reading files, instance methods) Due on Friday, May 1 at 4:00 pm. Automatic extensions to May 7 if needed. Part I: In the first part of this assignment, you will create a BaseballPlayer class which will represent a baseball player on a team. 1. Create a new directory called baseball. In that directory, create a BaseballPlayer class with the following instance variables: int number; String firstName; String lastName; String position; char battingArm; char throwingArm; int weight; int height; // height in inches String birthDate; // in format mm/dd/yyyy String birth1; String birth2; Be sure that all the variables have private access modifiers. 2. Create a constructor for the BaseballPlayer object which takes parameters corresponding to each of the above instance fields. 3. Create set and get methods for each of the above instance fields. 4. Create a method called getFullName which returns a String which consists of the firstName followed by the lastName, with a space between. 5. Create a method called getHanded which returns a String which consists of the battingArm, followed by a slash, followed the throwingArm, like this: B/R (this means both hands to hit (switch-hitter) and right hand to throw) 6. Create a method called getBirthplace which returns a String which consists of the contents of the String birth1, followed by a comma, followed by a space, followed by the contents of the String birth2 7. Create a method called getHeightString which returns a String which shows the height in feet and inches, like this: 6’2″ . The height field is in inches. Hint: to insert a quote mark inside a string, escape it with a backslash. 8. Create a method called getBirthYear which returns an int that is equal to the year listed in the birthdate string (yyyy). 9. Create a method called getAge which returns an int that is the difference between 2019 and the birthYear (hint: call getBirthYear) 10. Create a method called toString which returns a String consisting of the number, the full name (use the getFullName method), the position, the batting/throwing arm (use the getHanded method), the height (use the getHeightString method), the weight, the age, and the birthplace (use getBirthplace method). Format your results so they look like this: 5 Nick Williams RF L/L 6’3″ 195Lbs 26yrs Galveston, Texas 11. To debug your BaseballPlayer.java file, I recommend that you write a small client class which tests each of your methods to ensure that they work as designed. Part 2: In this part of the assignment, you will write a client a program to use and manipulate your BaseballPlayer class 1. Download the file phillies2019.txt into the same directory as your BaseballPlayer.java file. This .txt file contains a list of records containing the Spring 2019 roster for the Philadelphia Phillies, the local baseball team. There are exactly 29 lines in the file; the first line is a header showing the fields in each record, and the rest of the lines are the 28 players on the Spring 2019 roster. The first two lines of phillies2019.txt look like this: Number,Firstname,lastname,Position,Bats,Throws,wt(lbs),height(in),birthdate,birth1,birth2 23,Aaron,Altherr,RF,R,R,223,77,1/14/1991,Landstuhl,Germany The fields in each record are comma delimited. 2. Create a new client class with a filename BaseballRoster.java which contains a main method to run the program. The BaseballRoster program should do the following: (1) Create an array of BaseballPlayer objects to hold all the players in the roster. (2) Open the phillies2019.txt file and read in each line, using a Scanner (note that you will want to skip the first line). Use a try-catch block around the code that opens the file; if a FileNotFound Exception is caught, your code should issue a sensible error message and exit (call System.exit(-1)). (3) Within each line, use the “split” method that we learned in class to break the line into its comma-separated components, and then pass each component (after converting it to the proper type, if necessary), to the BaseballPlayer constructor and put the new object in the array of baseball players. Take careful note of the type of each parameter in the constructor, and be sure to pass that field as that type, as the split method turns everything into separate String values. For example, for the number field, it expects an int, and for the battingArm field, it expects a parameter of the char type. You can use the Integer.parseInt method to convert a String to an int type; to convert a String to a char, you may simply pass it to the charAt method (e.g. values[4].charAt(0)). (4) Once the array is created, use a loop (either a regular for loop or an enhanced for loop) to print out a roster of all the players (see the example printout). (5) Create a search function in the form of a loop which prompts the user for whether they wish to search for player number, player name (either last or first), or player birthplace (state or, if not US, country). Your function should produce a listing of all players that meet those criteria. If there are no matches, issue an appropriate error message and ask again. Continue to loop until the user presses 0 (see the example printout). Challenge: Create another version of BaseballRoster (call it BaseballRoster2.java) which uses an ArrayList, rather than an array, to store the BaseballPlayer objects. How to hand in your assignment: Upload your BaseballPlayer.java, BaseballRoster.java, and, if you chose to do the challenge problem, the BaseballRoster.java files, along with your reflection. The “official” due date for this assignment is May 1, but, since this is part of your final exam grade, you may have an “automatic extension” to 11:59 pm on May 7 if needed. The Reflection must include: a. Name and email b. What was/were the most difficult part(s) of this assignment, if anything? c. What did you like about this assignment, if anything? d. What did you particularly dislike about this assignment, if anything? e. How much time, approximately, did you spend coding on this assignment? f. Any other comments about the assignment? I hope you enjoy this assignment! Thank you for being such a great class! Example runs: java BaseballRoster Player Roster: 23 Aaron Altherr RF R/R 6’5″ 223Lbs 28yrs Landstuhl, Germany 52 Jose Alvarez P L/L 5’11” 190Lbs 30yrs Barcelona, Venezuela 64 Victor Arano P R/R 6’2″ 220Lbs 24yrs Cosamaloapan, Mexico 49 Jake Arrieta P R/R 6’4″ 225Lbs 33yrs Farmington, Missouri 58 Seranthony Dominguez P R/R 6’1″ 185Lbs 25yrs Esperanza, Dominican Republic 56 Zach Eflin P R/R 6’6″ 211Lbs 25yrs Orlando, Florida 48 Jerad Eickhoff P R/R 6’4″ 244Lbs 29yrs Evansville, Indiana 7 Maikel Franco 3B R/R 6’1″ 229Lbs 27yrs Azua, Dominican Republic 9 Phil Gosselin 2B R/R 6’1″ 200Lbs 31yrs West Chester, Pennsylvania 3 Bryce Harper RF L/R 6’3″ 220Lbs 27yrs Las Vegas, Nevada 16 Cesar Hernandez 2B B/R 5’10” 167Lbs 29yrs Valencia, Venezuela 37 Odubel Herrera CF L/R 5’11” 206Lbs 28yrs Zulia, Venezuela 17 Rhys Hoskins LF R/R 6’4″ 240Lbs 26yrs Sacramento, California 96 Tommy Hunter P R/R 6’3″ 250Lbs 33yrs Indianapolis, Indiana 4 Scott Kingery SS R/R 5’10” 180Lbs 25yrs Phoenix, Arizona 15 Andrew Knapp C B/R 6’1″ 204Lbs 28yrs Roseville, California 22 Andrew McCutchen RF R/R 5’11” 195Lbs 33yrs Fort Meade, Florida 46 Adam Morgan P L/L 6’1″ 37Lbs 29yrs Tampa, Florida 50 Hector Neris P R/R 6’2″ 227Lbs 30yrs Villa Altagracia, Dominican Republic 93 Pat Neshek P B/R 6’3″ 221Lbs 39yrs Madison, Wisconsin 12 Juan Nicasio P R/R 6’4″ 252Lbs 33yrs San Francisco de Macoris, Dominican Republic 27 Aaron Nola P R/R 6’2″ 200Lbs 26yrs Baton Rouge, Louisiana 24 Roman Quinn CF B/R 5’10” 170Lbs 26yrs Port St. Joe, Florida 10 J.T. Realmuto C R/R 5’10” 213Lbs 28yrs Del City, Oklahoma 30 David Robertson P R/R 5’11” 195Lbs 34yrs Birmingham, Alabama 2 Jean Segura SS R/R 5’10” 220Lbs 29yrs San Juan, Dominican Republic 21 Vince Velasquez P R/R 6’3″ 203Lbs 27yrs Mon
tclair, California 5 Nick Williams RF L/L 6’3″ 195Lbs 26yrs Galveston, Texas Enter 1 to search on player number, 2 to search on name, 3 to search on player birthplace, or 0 to quit: 1 Enter the number to search: 37 37 Odubel Herrera CF L/R 5’11” 206Lbs 28yrs Zulia, Venezuela Enter 1 to search on player number, 2 to search on name, 3 to search on player birthplace, or 0 to quit: 1 Enter the number to search: 52 52 Jose Alvarez P L/L 5’11” 190Lbs 30yrs Barcelona, Venezuela Enter 1 to search on player number, 2 to search on name, 3 to search on player birthplace, or 0 to quit: 1 Enter the number to search: 5 5 Nick Williams RF L/L 6’3″ 195Lbs 26yrs Galveston, Texas Enter 1 to search on player number, 2 to search on name, 3 to search on player birthplace, or 0 to quit: 1 Enter the number to search: 99 Sorry, no matches! Enter 1 to search on player number, 2 to search on name, 3 to search on player birthplace, or 0 to quit: 2 Enter the name to search: Vince 21 Vince Velasquez P R/R 6’3″ 203Lbs 27yrs Montclair, California Enter 1 to search on player number, 2 to search on name, 3 to search on player birthplace, or 0 to quit: 2 Enter the name to search: Maria Sorry, no matches! Enter 1 to search on player number, 2 to search on name, 3 to search on player birthplace, or 0 to quit: 2 Enter the name to search: Andrew 15 Andrew Knapp C B/R 6’1″ 204Lbs 28yrs Roseville, California 22 Andrew McCutchen RF R/R 5’11” 195Lbs 33yrs Fort Meade, Florida Enter 1 to search on player number, 2 to search on name, 3 to search on player birthplace, or 0 to quit: 3 Enter the state or (if not US) country: Pennsylvania 9 Phil Gosselin 2B R/R 6’1″ 200Lbs 31yrs West Chester, Pennsylvania Enter 1 to search on player number, 2 to search on name, 3 to search on player birthplace, or 0 to quit: 3 Enter the state or (if not US) country: Venezuela 52 Jose Alvarez P L/L 5’11” 190Lbs 30yrs Barcelona, Venezuela 16 Cesar Hernandez 2B B/R 5’10” 167Lbs 29yrs Valencia, Venezuela 37 Odúbel Herrera CF L/R 5’11” 206Lbs 28yrs Zulia, Venezuela Enter 1 to search on player number, 2 to search on name, 3 to search on player birthplace, or 0 to quit: 3 Enter the state or (if not US) country: Dominican Republic 58 Seranthony Dominguez P R/R 6’1″ 185Lbs 25yrs Esperanza, Dominican Republic 7 Maikel Franco 3B R/R 6’1″ 229Lbs 27yrs Azua, Dominican Republic 50 Hector Neris P R/R 6’2″ 227Lbs 30yrs Villa Altagracia, Dominican Republic 12 Juan Nicasio P R/R 6’4″ 252Lbs 33yrs San Francisco de Macoris, Dominican Republic 2 Jean Segura SS R/R 5’10” 220Lbs 29yrs San Juan, Dominican Republic Enter 1 to search on player number, 2 to search on name, 3 to search on player birthplace, or 0 to quit: 3 Enter the state or (if not US) country: Australia Sorry, no matches! Enter 1 to search on player number, 2 to search on name, 3 to search on player birthplace, or 0 to quit: 3 Enter the state or (if not US) country: Sorry, no matches! Enter 1 to search on player number, 2 to search on name, 3 to search on player birthplace, or 0 to quit: 0 Goodbye!

admin

Author admin

More posts by admin