Results 1 to 2 of 2
  1. #1
    diego is offline Member
    Join Date
    Dec 2007
    Rep Power

    Default Small tennis simulation in Java

    Hello, I am a new programmer and I have Several questions about how to implement object oriented features in my tennis simulation being written in Java. Index of /apliques/2007/2/ProjetoTenis/src

    First I will tell how my simulation works so that you all can understand better what I need to do, how to do it and especially how to do it in an object oriented way (can have some generic programming too)

    This is how the program works
    I am doing a little tennis program in which I implement the simulation of a game of a tennis match based on random number generation and probabilities. For example, it chooses a number between 1 to 2, and if the result is 1, the player hits the first serve. If not, then the second one.
    Then, when he hits any of the two serves, it sorts a number between 1 to 10. In the first serve case, if the number is anything between 1 to 8, the server wins the point. In the second serve case, the server wins the point if the value choosed is anything between 1 to 5.

    I have three big classes, one that has the main method and creates an object that represents a tennis match.
    This object calls for a method of the class "Game", the class responsible for 'managing' a game from a tennis match.
    For now the only thing this "Game" class is doing, is holding the method responsable for controlling the serves, that as was said in the above paragrapher, are the responsables for deciding who is going to win a point. The method "saque" (serve in english) chooses a serve and then send it to a certain method of the "Pontos" (points) class to select who winned the point.This class contains three methods, each one for one of the three possibilities: first serve in, second serve in and double fault. When the winner of the point is choosed, I would like it to return the two variables that represent the points that each player will have after the point is finished,
    back to method "saque" (serve) of the Game class.

    This brings me to the first problem: methods cannot return two values. So, can I create an object composed of two fields and return it? If I can, can you guys show how to do it?. If not, what you suggest?. I need to return them because I intend to create in the "Game" class, another method, one that will control the game, or in another words, finish the program when a player collects enough points to win the game.

    Then in the "game" class, I would like to send these two values, whether they will be in an object or in another form to the "control" method that I intend to create in the same class. This brings to the second question which is: when these points will be returned, should I create this "control method" in the same class and send the values to it, or should I do something completely different to allow bigger modularization?.

    And the last question comes to the surface due to the fact that the point count changes during the game. The way it is now. at each new point 15points will be given to the variable that represents the score of one of the players.
    How should I change this value according to occasion? Should I create an object of the type "Point" to do this?

    Thanks for all the suggestions and recomendations.

  2. #2
    Leprechaun is offline Member
    Join Date
    Dec 2007
    Rep Power


    In your problem of a method only returning one value, I would suggest using an array. This way you could have as many values you needed stored in that one array.

    I'm not sure about your second question.

    0-15-30-40-50 (That's how I remember tennis scoring going)...
    I would make a variable, say addTotal, which initially equals 15. Then a condition directly after the line of code where you increment the score like so:
    Java Code:
    if (totalScore == 15) 
    addTotal -= 5; //which makes it 10
    Then, when you start a new game reset the addTotal to 15.

    Hope some of that helped...
    Last edited by Leprechaun; 12-02-2007 at 01:34 AM. Reason: Changin tag type

Similar Threads

  1. Building small web application in java for practice.
    By Saurabh321 in forum New To Java
    Replies: 1
    Last Post: 02-01-2008, 03:38 PM
  2. Small scale Java Editor
    By Greenfrog99 in forum AWT / Swing
    Replies: 0
    Last Post: 01-27-2008, 08:46 PM
  3. Replies: 1
    Last Post: 01-05-2008, 07:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts