# Why do I need more than 1 class? - School project help.

• October 26th, 2012, 07:03 PM
Kakihara
Why do I need more than 1 class? - School project help.
Hello,

I recently started a project for my intro to Java course. The project is to create an app that calculates a baseball player's batting average and slugging percentage (many of you have likely heard of this project before). Here are the details for the application:

"This application calculates the batting average and slugging percentage for one or more baseball or softball players.
For each player, the application first asks for the number of at bats. Then, for each at bat, the application asks for the result.
After all the at-bat results are entered, the application displays the batting average and slugging percent.

The batting average is the total number of at bats for which the player earned at least one base divided by the number of at bats.
The slugging percentage is the total number of bases earned divided by the number of at bats.
Use an array to store the at-bat results for a player.
Validate the input so the user can enter only positive integers. For the at-bat results, the user’s entry must be 0, 1, 2, 3, or 4.
Validate the user’s response to the question “Another batter?” so the user can enter only Y, y, N, or n. If the user enters Y or y, calculate the statistics for another batter. Otherwise, end the program."

I only used one class with the different methods to accomplish this. Here is my pseudo code:

BattingAverage Class
MAIN:
Code :

```PRINT “Welcome to the Batting Average calculator.” DO numberAtBats = getNumberAtBats() PRINT “0 = out, 1 = single, 2 = double, 3 = triple, 4 = home run”   Int[] results array = new int[numberAtBats] \\ use numberAtBats as size of results array   FOR each position(numberAtBats) of batResults array IF batResult > 0 THEN ADD 1 to totalBatsWithBase ADD batResults to totalBasesEarned   Compute battingAverage as totalBatsWithBase / numberAtBats Compute sluggingPercentage as totalBasesEarned / numberAtBats   PRINT battingAverage and slugging Percentage WHILE getAnotherBatter()   getNumberAtBats: WHILE input is invalid PRINT “Enter number of times at bat: “ IF input is an integer input = numberAtBats If numAtBats > 0; input is valid ENDWHILE RETURN numberAtBats   getBatResults: WHILE input is invalid PRINT “Result for at-bat: “ IF input is an integer input = batResults IF batResults = 0-4 Input is valid RETURN batResults   getAnotherBatter: PRINT “Another batter?” IF choice = y RETURN true // start over ELSE RETURN false //End```

My problem is that the teacher said I need more than one class. I'm not sure why, as I believe this should function as is, but his response was this:
"You have one class where you need at least two. getAnotherBatter is a hint that your class is mixing two classes."

So my question is why do I need more than one class? Have I left something out?
Can anyone help me understand how I am mixing two classes, and why it will not work as one?
He mentioned that getAnotherBatter is a hint that I am mixing two classes, but I'm having trouble figuring out how.

Thanks for your time, and I appreciate any help!
• October 26th, 2012, 08:16 PM
helloworld922
Re: Why do I need more than 1 class? - School project help.
Quote:

So my question is why do I need more than one class? Have I left something out?
In the literal sense, no, you don't need more than one class. However, you should consider what benefits having more than one class will give you.

Consider what the ultimate goal of object-oriented programming is meant to do: it helps us (the developers) to organize our thoughts and improve developer efficiency. Object-oriented programming does this by organizing programs around objects, which we can use to arrange data into physical chunks which make sense together.

So in your program what would be a useful grouping? The most obvious one is a Player class. This class could hold the basic information associated with a Player, for example their at-bats. The Player class can also contain methods which will perform operations on a Player, for example calculating their batting average.

There's also another mode of thought often associated with object-oriented programming known as Model-View-Controller (MVC). This methodology separates the aspects of a program into 3 key areas:

1. The Model: This is the base data, including how it can interact with other data. This is the Player class.
2. The View: This is the portion of the program that an end-user can get or give feedback. This could be a GUI, command-line, or a robot which talks/listens to the user. In your program, you're interfacing with the command-line so the view is implemented already.
3. The Controller: This is the portion of the program which interfaces the view with the model. It takes the user feedback from the view and performs operations on the model. It also updates the view based off of the model data.

Following this MVC model we can arrive to a logical second choice for a class: an interface between or Player model class and user interaction with the command-line.
• October 26th, 2012, 09:08 PM
Kakihara
Re: Why do I need more than 1 class? - School project help.