Lab python



Computer Scientist love baseball because it has stats. I might have made that up.

This will be more practice on object oriented programming.

To Do

You are to write a program to keep track of a baseball players on base percentage, and batting average. Also by default the program should be able to give the total at bats and the official at bats that the player has recorded. Plus the output of each stat should include the players name.

How it works

We will be keeping track of on base percentage (OB%) and batting average, these are different. Batting averages are lower than OB% normally. Batting average is based on a successful base hits.
Where OB% is based on getting on base, whether it’s a successful hit, a free pass by base on balls, or arriving on a defensive error.

If you don’t know baseball here is the scenerio:

A baseball player will walk to homeplate to face the other team’s pitcher. When this happens several things can happen to change the batters stats. Even though the player is facing pitches from the opposing pitcher the result might not be a an official at bat according to the results that occur.

I am going to simplify it here a bit, but when a player is at bat they have four possible outcomes, each has a different result to the players stats.

The easiest one is the player gets out. In this scenerio the amount of at bats increases, but nothing else changes….if you’re a batter this is not good.

The next result will be when the player hits the ball and the defense messes up the play and the batter gets on base because of the defensive players oops. This is called an error. In this scenerio the batter gets credit for being on base, but they do not get credit for getting a hit. It helps their OB%, but hurts their batting average. As far as batting average goes if the official scorer thinks the only reason the batter got on base was because the defense made a mistake it counts as an out toward the batting average.

The next result is the pitcher on the other team throws four pitches out of the “strike zone”, this allows the batter to get a free pass on base. This does not count as an official at bat, so it doesn’t count in batting average, but does count in OB%.
The fourth result is the batter gets a base hit of any kind. This increases the OB% and the batting average.


Chipper Jones comes to bat and gets a hit.
Chipper Jones comes to bat and gets out.
Chipper Jones comes to bat and gets out.
Chipper Jones come to bat and gets four balls for a free pass.
Chipper Jones come to bat and gets on base by a defensive error.

Chipper Jones batting average would be 1 for 4 or .250
Chipper Jones OB% would be 3 for 5 or .600

Work Needed

Create a class that keeps track of a baseball player.
Write methods to keep track of an at bat.
Write methods to calculate OB% and batting average.

Questions you need to handle

What instance fields do you need?

How many methods do you need?

How do you handle a single at bat, one method? Multiple methods? Do you have a method call other methods?
Think about another programmer using this class, how would they want to use your class.

How do you handle outputting the data? Do you have a method that calculates and prints? Do you have methods that calculates and returns?

These are just some examples of questions. There are many ways to handle this problem, but some are definitely better than others. Maximize usability for outsiders using this class without having redundant code.

Test your class

Write a driver that creates two or three batters, give them names. Record results for five to eight at bats. Output the results of OB% and batting average for each player.

Last Updated on February 11, 2019 by EssayPro