Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

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

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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:
    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!
    Last edited by helloworld922; October 26th, 2012 at 08:32 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Why do I need more than 1 class? - School project help.

    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.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    Kakihara (October 26th, 2012)

  4. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Why do I need more than 1 class? - School project help.

    Thank you for your quick reply, helloworld922.

    That makes sense, and I understand now.
    I guess I was just worried that I left something out, and was thinking too literally about needing multiple classes.

    Thanks again!

Similar Threads

  1. School Java Project Help (XML and links)
    By MC2170 in forum Java Theory & Questions
    Replies: 6
    Last Post: July 3rd, 2012, 09:44 AM
  2. High School Java Programming Class
    By SDennis52 in forum Collections and Generics
    Replies: 1
    Last Post: May 7th, 2012, 01:38 AM
  3. Please help! Need help with class project!
    By eldeeb_hasan in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 10th, 2012, 01:30 AM
  4. School java project, completely stuck
    By John1818 in forum Java Theory & Questions
    Replies: 0
    Last Post: November 18th, 2010, 04:10 AM
  5. school project
    By robin28 in forum Java Theory & Questions
    Replies: 13
    Last Post: November 12th, 2010, 09:11 AM