Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Array solution for Tennis Game

 
Sheriff
Posts: 15043
252
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me try to help you scratch the itch you have to use an array. This is the only thing I can think of in this program where an array would be useful.

An array can be used as a simple map of sorts. That is, each index in the array can serve as a key to a specific value. In Java, array indices are always 0, 1, 2, 3, etc. In this problem, you have the score values of 0, 15, 30, 40.

So, the only situation that makes sense to me to use an array is to translate from 0, 1, 2, 3 to 0, 15, 30, 40, respectively. That is, this mapping suggests an array like this:

But again, I wouldn't have started with this since it's an implementation detail. I would have let more tests eventually lead me to this implementation. Nothing that I showed you so far would tell me I needed this implementation though. What I've show up to this point really has been about discovering an intuitive and natural API. The implementation would come later, maybe after four or five other tests have been made to pass.
 
Junilu Lacar
Sheriff
Posts: 15043
252
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I wrote:this mapping suggests an array like this:


You might be wondering how I came to the above code that uses an array. Here's kind of what's going through my head with this code. Scoring in tennis is very inconsistent, right? The first point you get, your score is 15. With a second point you get 30. That's a difference of 15. If you were thinking logically, you might think that with the next point you win, your score should be 45. After all, 30 + 15 equals 45, right? But the geniuses who came up with the scoring system in tennis decided your score should be 40 instead. And then the next point, you just outright win the game. You don't even get a score! Is the winning score 50 (40 + 10)? Is it 55 (40 + 15)? Or is it 45 (40 + 5)? No, the winning score doesn't even matter! You just win with the previous score you had!

As the comic Brian Regan describes it, it's mathematically ludicrous (see his comedy bit about that here: https://www.youtube.com/watch?v=om6iH-AiMr4, starting at 3:03).

So, to keep the code simpler (and saner), I would just keep track of points won by each player with 0, 1, 2, 3 instead. Then I'd use those values to get the score for display purposes. That is, I might write this implementation:

I want to emphasize that I wouldn't be coming up with this code just off the top of my head. I would have already written tests that lead me to write this kind of implementation. Because I'm so used to the test-first approach and using it to inform my design decisions, coming up with the implementation before I have tests that suggest a design feels very backwards and uncomfortable to me.

Some might be quick to point out that this doesn't take into account Deuce, Ad-In, and Ad-Out. They would be right to point it out. But I would say, worry about your basic scoring first. The Deuce, Ad-In, Ad-Out situations are special circumstances that you'll try to accommodate after you have a clean implementation (that is, you have passing tests and you've refactored the code).  
 
Junilu Lacar
Sheriff
Posts: 15043
252
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The last design I showed actually starts to lend itself well to an MVC design.

The Game class is your model. It encapsulates the state of the game. From your requirements, the user would select a menu option to award the server a point, and another option to award the receiver a point. Another option allows them to see what the score was. That would naturally lead you to write something like this:

This is the kind of code you'd expect to see in a Controller class. Again, I wouldn't be able to come up with this kind of code if I didn't already have tests that suggested this kind of logic. So to me, it's really important to try to tell a story with the tests so that you have a better idea of how that story translates / gets implemented as code.

Starting from the nitty-gritty details takes your focus away from that big picture and you usually end up with bits and pieces of code that don't really fit well together, is difficult to understand, and is almost impossible to test.
 
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

So I am really trying different versions out here, but I need some real help, cause I am running out of time. Please

This is something else I've been trying




received:

serverLove
receiverLove
server15
receiver15
server30
receiver30

expected
serverLove
receiverLove
server15
receiver15
server30
receiver30


1. I would really like to solve this problem with a pojo(player) + logic(game) + interface(the scanner functionality) + Main.java(just to call the scanner interface)
2. I think I need a way of validating/counting in real time the result. Or I need an array to push results
3. Help!

Menu:
1. give "15" to server or
2. give "15 to receiver
3. give "30" to server or
4. give "30" to receiver
5. give "40" to server or
6. give "40" to receiver
7. give Game to server or "40" if previously receiver gained point or
8. give Game to receiver or "40" if previously server gained point
9. give Deuce
10. give "advantage" to server or
12. give "advantage" to receiver
13. give "Deuce" if server or receiver "Deuce"
12. give Win to server if "advantage" && "game" server or
13. give Win to receiver if "advantage" && "game" server or

Can i get more help, please?
 
Marshal
Posts: 68115
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
GThere must be a better way to do it that all those ifs. Can you create a Score class which has the next score as a field (love↦15, etc.)? And I still think you should have a Player class.
 
Junilu Lacar
Sheriff
Posts: 15043
252
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lg Long wrote:
Menu:
1. give "15" to server or
2. give "15 to receiver
3. give "30" to server or
4. give "30" to receiver
5. give "40" to server or
6. give "40" to receiver
7. give Game to server or "40" if previously receiver gained point or
8. give Game to receiver or "40" if previously server gained point
9. give Deuce
10. give "advantage" to server or
12. give "advantage" to receiver
13. give "Deuce" if server or receiver "Deuce"
12. give Win to server if "advantage" && "game" server or
13. give Win to receiver if "advantage" && "game" server or


That is very different from what you posted before:


Interface:
Options: Press 1 to add point to Player1 - > response: Player 1 has 'wateva score the player has, if he won or at deuce'
           Press 2 to add point to Player 2  - > response: Player 1 has 'wateva score the player has, if he won or at deuce'
           Press 3 to check score
           Press 4 to exit.


Frankly, if you have the first 13-option menu above, you might as well be scoring with pencil and paper. The point of writing a scoring program is so the program can keep track of the score for you. With that menu, you're basically keeping track of the score yourself and just having the program say whatever it is you tell it to say. That would be a wrong solution.
 
Junilu Lacar
Sheriff
Posts: 15043
252
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a JUnit 5 test class I used to implement most of the program logic. I wrote these one test at a time, making each one pass before going on the next test. When I finished writing the last test and made it pass, I just needed to refactor my code a little bit to clean it up (remove duplication, rename things so they make sense) and I was done.

My mindset all throughout this process was to keep expanding on the story I wanted to tell. I hope you can see how the story evolves and expands as you read through this test. Ignore the parts you don't understand, like the lambda expressions and the functional interface, for example. Just read each test and see if it makes sense to you.

The naming convention that has a test name start with "it" is very common nowadays. The "it" refers to the class under test. In this case, "it" refers to the TennisGame class so "it_starts_at_love_all" means "the TennisGame class starts at love all."


To give you an idea of the kind of expressiveness I strive for when I refactor my code, here's the implementation I have for the TennisGame.getScore() method. Again, I strive to make it read like a story, not a technical manual:


I realize all this is probably more than what you're currently able to digest given that you just want to finish your assignment. However, if you really want to learn how to do this, I'd encourage you to come back at a later time when you're not under so much pressure to get done and can take your time to go through this process so you learn how to write the program in a systematic and orderly way.

Good luck.
 
Junilu Lacar
Sheriff
Posts: 15043
252
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For anyone following this thread and you're curious about the @DisplayNameGenerator annotation at the top of the unit test I posted, here's what it looks like when I run the tests in IntelliJ IDEA:



I believe this is available in JUnit 5.6 and later.
 
Lg Long
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:






Junilu, I understand that i need to get the testing methodology. I remember when I first started play chess, I wasn't testing and just coming with a solution. But as I played along and became comfortable with patterns, it was easier for me to "TDD and carry on". Hope this makes sense.

Thank you for the help. Probably it's not what you where hopping for and my effort will make you smile. It would make me, cause I do realise how stubborn && noob I am.

This what I came up with:



Problems that I knew i will have from the beginning. The points as you said "genius". That is why I kept on using String, but how can I assign String to int server; receive??

The "Deuce" can go on and on and on, therefore I need a solution for that. Can be a while loop? Can Be the switch cases, the solution??

How can I get rid of the previous messages: Deuce, advantage if the player met the criteria to win the game? switch cases?


Thinking to open a new thread to work in parallel for enums and hash maps solution. I will also have an assessment, so I need to know these data structures by Monday. At least have some solid examples that I know really well and apply the principles to any problem i will get.

I had a previous post concerning ArrayList, I had an assessment, which was this tennis game. I did ok for 30 min code: in scanner when user adds 4 instances of a "player" object prints player won.
The post was about a NaturalReservation, but I was able to apply the principles to the tennis game.

So, help
 
Lg Long
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just thought on how to solve the array length problem.

when one player has advantage and the other catches up, I decrement.

So at the moment there is a lot of duplication, and I aim to use parameters in the methods.

Some code hints, to make it easier on me?
 
Junilu Lacar
Sheriff
Posts: 15043
252
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lg Long wrote:when one player has advantage and the other catches up, I decrement.


That is essentially what I did with my implementation. Deuce was always a 3 points to 3 points situation, Ad-in meant the server was up one point, and Ad-out meant the receiver was up one point.
 
Junilu Lacar
Sheriff
Posts: 15043
252
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lg Long wrote:That is why I kept on using String, but how can I assign String to int server; receive??


To be clear, despite my earlier example, I did end up using a String[] for the scores just because of the "Love" thing. I only had elements up to "40" though.

I didn't include the other scores like "Deuce," "Ad-In," and "Ad-Out" because they are all special cases that don't really fit a mapping to the points won. So my implementation has a lot of conditional logic to handle those special cases. I thought of refactoring a bit more but it looked good enough for me last night so I just stopped when I got all the tests to pass and eliminated most of the duplication in the code.
 
Junilu Lacar
Sheriff
Posts: 15043
252
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lg Long wrote:I understand that i need to get the testing methodology.


Well, as I said, the test-first programming approach is really more about design than it is a way to test code. Testable code tends to be more loosely coupled, more encapsulated, and better organized. That's all about design, not testing. By writing the tests first, you're forcing yourself to think about things that lead to better designs, not just coming up with an implementation that works. I find that for most programmers, if they're focused solely on implementation details then you can pretty much expect their designs to be less than ideal.
 
Lg Long
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am kinda stuck. I think I need to take a nap so I can think of how to build the methods to check for hasWinner(), isTied(), atAdvantagePoint, normalScore() <- what is this one??

I understand the lambda syntax. What gave me a headache is the interface and the run(), almost seams like a builder...
 
Junilu Lacar
Sheriff
Posts: 15043
252
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To give you a better idea of my thought process around test/design, let's examine this test:

I was also thinking about MVC when I was writing this test. The design-related question in my mind was "How would a GameController interact with a Game?"

The answer I had was, "Well, the user would be presented with a menu of options, they'd select the 'Server wins point' option, and then the GameController will tell the Game to award a point to the server. That is, I'd expect to write something like what I already partially showed before:

So if you read between the lines of the test, you can easily fill in where the interactions between the GameController, the View, and the Game (model) occur:

So with a little imagination, you can use the test as a "sketch" of what would happen when your class under test (the TennisGame class) is used in a real program scenario (the GameController code that I outlined above). The assertions help verify that the sketch you are drawing out plays out like you expected it to.
 
Junilu Lacar
Sheriff
Posts: 15043
252
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lg Long wrote:What gave me a headache is the interface and the run(), almost seams like a builder...


Don't focus on that stuff. Focus more on the tests themselves, not the plumbing, and try to understand the thought processes in coming up with that code. Then use it as a guide for telling the story in your own way.
 
Junilu Lacar
Sheriff
Posts: 15043
252
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lg Long wrote:normalScore() <- what is this one??


Maybe that's not the best name since you're asking about it. The idea there was that after exhausting all the checks for various special conditions like hasWinner(), isDeuce(), etc., you come down to the "normal" case where the score is Love - 15, or 15 - 30, or 30 - Love, etc. That is, the score is not a "exceptional/special" form but rather a "normal" form. Maybe "regularScore" or "basicScore" might convey that idea better, I don't know.
 
Junilu Lacar
Sheriff
Posts: 15043
252
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding the scoring for Deuce/Ad-In/Ad-Out, I drew from my experience as a ball boy and scorer for my dad when he played tennis with his buddies (this was many years ago, when I was still in grade school). The courts they played on had abacus-like scoreboards, with white disks for regular points (15, 30, 40) and a red disk for advantage point. Basically, a game would start at Love all with the scoreboard looking something like this:

RWWW---||---WWWR

Let's say the server score is on the left side and they won a point, so I'd slide a white disk to the middle to make score 15 - Love:

RWW---W||---WWWR

This is what 30-30 would look like:

RW---WW||WW---WR

This would be what 40-40 (Deuce) looked like:

R---WWW||WWW---R

This is what Ad-In (server had 1-pt advantage) would look like:

---RWWW||WWW---R

And this is what Ad-Out (receiver had 1-pt advantage) would look like:

R---WWW||WWWR---

My Deuce/Ad-In/Ad-Out logic basically follows this scheme: Deuce is alway 3-3, Ad-In would be 4-3, and Ad-Out would be 3-4. This is why I have methods like isDeuce(), atAdvantagePoint(), and isServerUp(). So if any player had an advantage point and they lost the next point, their points would be decremented to get back to Deuce (3-3), just like how I would do it in the abacus-like scoreboard.

Again, this leads to very expressive code to represent different scenarios, like this:

(I'm purposely leaving some context out to encourage you to think about where this kind of logic might be appropriately placed in the program. Note also that I have since renamed isServerUp() to serverHasAdvantage())
 
Lg Long
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:GThere must be a better way to do it that all those ifs. Can you create a Score class which has the next score as a field (love↦15, etc.)? And I still think you should have a Player class.



That was my intention.
To have a TennisPlayer class -> Pojo; have a TennisGame class - > Logic; TennisInterface -> Menu(scanner functionality - Probably similar to NatureReserve ArrayList Project); Main(program entry point) < = > Interface interface = new Interface; interface.gameMenu();

In my opinion this is a model I would like to embrace as, I will be continuing doing exercises of this type.

How would you use a TennisPlayer Pojo in the TennisGame

My design would have been to have some methods to take parameters, to become attributes inside the method and to be assigned to player and point.
Does what I say makes sense?

//pseudocode
pointMetod(TenisPlayer, ScoreChoce ){

  if(receiver()){
     receiverPoint()
  }
}

but in the same time, this might lead to, better pen and paper.
 
Junilu Lacar
Sheriff
Posts: 15043
252
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lg Long wrote:
//pseudocode
pointMetod(TenisPlayer, ScoreChoce ){

  if(receiver()){
     receiverPoint()
  }
}


That does not look good at all to me. 1 - it's not object-oriented, even though you'd be calling it a "method" and you're putting it some class. 2 - the semantics are confusing.

What exactly are you trying to say with that?
 
Lg Long
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Lg Long wrote:normalScore() <- what is this one??


Maybe that's not the best name since you're asking about it. The idea there was that after exhausting all the checks for various special conditions like hasWinner(), isDeuce(), etc., you come down to the "normal" case where the score is Love - 15, or 15 - 30, or 30 - Love, etc. That is, the score is not a "exceptional/special" form but rather a "normal" form. Maybe "regularScore" or "basicScore" might convey that idea better, I don't know.



I would call it upTo30PointsScore()
 
Lg Long
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Lg Long wrote:
//pseudocode
pointMetod(TenisPlayer, ScoreChoce ){

  if(receiver()){
     receiverPoint()
  }
}


That does not look good at all to me. 1 - it's not object-oriented, even though you'd be calling it a "method" and you're putting it some class. 2 - the semantics are confusing.

What exactly are you trying to say with that?



Right:

So, I think I am getting confused and this is spanning for to much time :

The interface is solid, as I worked with Campbell on the ArrayList Nature project
I was hopping I could follow the same structure in this scenario as well.

Junilu, you are a rock. I took a nap and still tired. You are on all the time. There's an A.I. covering you? -> Kidding. Thank you for holding on.

Can we try and solve my model and then I can put thinking in your solution as well?




Sometimes it takes me a while to post the comment. By the time I get to post, I am logged off. When I log in I am off the page. Is this something I can solve?
 
Junilu Lacar
Sheriff
Posts: 15043
252
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lg Long wrote:Sometimes it takes me a while to post the comment. By the time I get to post, I am logged off. When I log in I am off the page. Is this something I can solve?


On the login page, I think there's a checkbox you can select to stay logged in.
 
Junilu Lacar
Sheriff
Posts: 15043
252
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your menu loop looks better to me although I don't understand why you're passing in a new TennisPlayer instance every time you're awarding a point. That doesn't make sense to me.

It's not quite there yet as far as having an MVC design though. In my implementation, I had a View that encapsulated everything that involved display and user input, a GameController that had the game loop, and the Game itself as the Model. That's it. I did not see any need for a Player class since it would have practically no responsibility or interesting behavior.

To me, a Player class in this context falls under the "trying to model the real world too much" trap which leads to unnecessary complexity. An OO program can certainly draw inspiration and analogies from real-world objects but OO is really about proper assignment of responsibility and behavior. There are many "complete fabrication" type classes that are absolutely valid in an OO design sense even if they have no real-world equivalent.
 
Junilu Lacar
Sheriff
Posts: 15043
252
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also don't understand why your SCORE array has all those elements. There are many score combinations than what you have listed and those are mssing. Also, "40-11" is not a valid score in tennis.

And "Ad In" means "server's Advantage" and "Ad out" means "receiver's Advantage" -- so it's not "Add" as in addition, it's "Ad" as in ADvantage.
 
Lg Long
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:I don't understand why you're passing in a new TennisPlayer instance every time you're awarding a point. That doesn't make sense to me.



How can i pass the same instance??


In my implementation, I had a View that encapsulated everything that involved display and user input, a GameController that had the game loop, and the Game itself as the Model.



Can I see you implementation??


That's it. I did not see any need for a Player class since it would have practically no responsibility or interesting behavior.



The player class might not have an interesting responsibility. But I did mention it needs to be easy to scale, so Imagine you need a factory to check for the name of the player.


To me, a Player class in this context falls under the "trying to model the real world too much" trap which leads to unnecessary complexity. An OO program can certainly draw inspiration and analogies from real-world objects but OO is really about proper assignment of responsibility and behavior. There are many "complete fabrication" type classes that are absolutely valid in an OO design sense even if they have no real-world equivalent.



I understand, but for now I need to solve my model. Which I understand. So i need some help with this. I feel that I am dragged from it for days now.
Can I get some help with this, please? Even if you think it's wrong. I need to do this and then I will have it your way to, as I am willing to learn more then just one way.
Now i feel that I am tied in some arguments that get me no were. No offence and I appreciate your help!
 
Lg Long
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:I also don't understand why your SCORE array has all those elements. There are many score combinations than what you have listed and those are mssing. Also, "40-11" is not a valid score in tennis.

And "Ad In" means "server's Advantage" and "Ad out" means "receiver's Advantage" -- so it's not "Add" as in addition, it's "Ad" as in ADvantage.



Indeed, it was a mistake as it was late.

For now just keep all the possible result in the array.
Again this is my model and I want to do it this way for now. The might be better solutions or less better, but i don't get nowhere with this.
I need to deliver.

And I am more confused then I was when I came here . I need help on solving it my way, for now. Not being dragged on other solutions

So plan:

1. how can I assign a value from the array to an instance of a player?
private static final String[] SCORE = {"Love-all", "15-all", "30-all", "40-ll", "Deuce", "Add-on", "Add-out", "Game"};
static TennisPlayer serverPoints = new TennisPlayer("server");

static int serverScore(int scoreChoice) {
       //I can't do this anymore, how do I solve it?
       serverPoints = SCORE[scoreChoice];
   }

Do I need to ? in player??




and then?? in TennisGame class


 
Campbell Ritchie
Marshal
Posts: 68115
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know, but I always get suspicious when I see the word static. Why is that method marked static? I have a rule of thumb about they keyword static:-

If you haven't got a good explanation for saying static, it's a mistake.

And another rule of thumb:-

Adding static because the compiler said something about non‑static is a bad explanation.

 
Lg Long
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Don't know, but I always get suspicious when I see the word static. Why is that method marked static? I have a rule of thumb about they keyword static:-

If you haven't got a good explanation for saying static, it's a mistake.

And another rule of thumb:-

Adding static because the compiler said something about non‑static is a bad explanation.



as I thought I will be using the method inside the TennisGame class and only there.
 
Campbell Ritchie
Marshal
Posts: 68115
258
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's another bad explanation. If you only use the method inside the class, mark it private rather than static.
 
Junilu Lacar
Sheriff
Posts: 15043
252
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lg Long wrote:Now i feel that I am tied in some arguments that get me no were. No offence and I appreciate your help!


I understand how that must feel for you. However, you also have to understand what it's like from our perspective.

From our side, we see things that you're doing wrong and we want to guide you away from it. If you see someone about to walk into a busy street and get run over, you want to help by pulling them away to safety, right? Nobody in their right mind is going to jump into the road with that person and attempt to help them cross the street. They'd run the risk of getting run over themselves! That's why it's hard for anyone to go along with the way you're trying to go if they recognize that it's going to lead to something bad anyway. Many of us here are professional programmers so it goes against our very nature to help anyone write bad code. We want to help them write good code.

Secondly, we can't show you full implementations that we've done to solve your problem. We have a strict policy against that sort of thing around here, especially when it involves school work.

We certainly can let you continue to fumble around and try different things out but until you gather your thoughts and organize them into one coherent stream, I'm afraid you'll continue to miss the mark. Without clarity on your part, we'd also be at a loss for how to help you because if you don't know where you're going, how can we help you get there? It's a no-win situation.

Take this line of code as an example:

I don't understand what you're trying to do there so I asked you. But then you came back and asked "How can I pass the same instance?" Well, the point of my question was to get you to think about your decision to do it that way. What I really was implying was "Don't do it that way." But you came back and asked "Well, how do I do it anyway?"

It's kind of like you stepped into a busy road and I asked you "Why are you stepping into that busy road?" (even though I really wanted to say "Get off the road!") and you reply "Well, how can I cross this road anyway?" Do you see how this kind of conversation is pretty much doomed from the start?
 
Junilu Lacar
Sheriff
Posts: 15043
252
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll try one more time to help you organize your thoughts. You need to implement MVC and you've actually started down a good path with that. You already have code for a Controller, that's the game loop that you kind of patterned after an example I gave. Here's a breakdown of responsibilities of different classes that would make up MVC:

View (GameView class)
1. Displays menu
2. Gets input from user
3. Displays status of the game (as provided by the model, through the controller)

Controller (GameController class)
1. Tells the View to display menu
2. Tells the View to get user input (the action)
3. Dispatches user input to appropriate actions
4. Calls appropriate methods in the model to carry out actions requested
5. Passes results of the action performed back to the view for display, if any.

Model (Game class)
1. Keep track of the state of the game - keeps score and determines if game is over
2. Provide methods to change the state of the game - serverWinsPoint(), receiverWinsPoint()
3. Provide method to report the state of the game (getScore(), hasWinner())

That's it! With the four menu options you're supposed to provide to the user (1-Award server a point, 2-Award receiver a point, 3-Show score, 4-Exit) you have everything in the above description to implement a program that does all that.
 
Lg Long
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:
"Don't do it that way." But you came back and asked "Well, how do I do it anyway?"



So I am back on Junilu's tests as I realised I was going nowhere with my way of coding. Just as I say the previous post I was about to do this one.

So, at the moment the first test is passing. Love-all, as I don' have player names.

Should we have player names?

What condition do I need to put to make the next test to pass? I guess I am not going to get a full response, but I need some hints.

If I take out of the getScore()

Test:


Fails

Expected :Love all
Actual   :0: Love, 0 Love



Test:

Fails

Expected :15 - Love
Actual   :1: Love, 0 Love



Now, from this I understand that I do not assign the values from the array to my receiver and server. This is happening because they are int, probably and they take the values as int.
If I change the


private int serverPoints;
private int receiverPoints;
to
private String serverPoints;
private String receiverPoints;



I can't increment anymore, as incrementation doesn't work on Strings. So I have a blocker.






 
Lg Long
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

That's it! With the four menu options you're supposed to provide to the user (1-Award server a point, 2-Award receiver a point, 3-Show score, 4-Exit) you have everything in the above description to implement a program that does all that.



I can do this. I suck at logic and I know it, but I won't give up.

Need a walk thought!
 
Campbell Ritchie
Marshal
Posts: 68115
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . options you're supposed to provide to the user . . .

Four of them. That makes it so simple for the user, who only ever sees those four options.
 
Junilu Lacar
Sheriff
Posts: 15043
252
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lg Long wrote:I can do this. I suck at logic and I know it, but I won't give up.


That's the spirit! Although you shouldn't get down on yourself like that. You're learning. Confusion, fumbling, and failing is part of the learning process.

It's easy for people like me to organize a design quickly because I've been doing this for years. It's much harder when you haven't done it as much. So keep your chin up, keep calm, focus on small problems, and keep going!
 
Junilu Lacar
Sheriff
Posts: 15043
252
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Should we have player names?

Nothing in your stated requirements says you need player names. To keep things simple, I would stick with just "Server" and "Receiver" for now. That can be easily changed later if you do decide to have player names.
 
Junilu Lacar
Sheriff
Posts: 15043
252
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start with trying to solve the second test first, the one that says "it gives the server score first". Only write as much code as you need to solve that. That is, instead of this:

try something simpler, like this:

You'll have to implement plainScore() correctly to get that second test to pass though. We can talk about how to do that next.
 
Junilu Lacar
Sheriff
Posts: 15043
252
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Going back over this thread, I already explained to you how you'd use a SCORE array as a quick-and-dirty map to translate from 0, 1, 2, 3 to "Love", "15", "30", "40" (You don't want to map "Love all" by the way)

Also, you've already seen code that can format a String to your liking. Here are some examples:

So if you had values for serverScore and receiverScore, how would you produce a string like "Love - 15" or "15 - 15" or "30 - 40" with the serverScore always the first one printed (the score on the left of the "-")?
 
Ew. You guys are ugly with a capital UG. Here, maybe this tiny ad can help:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!