This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Building Blockchain Apps and have Michael Yuan on-line!
See this thread for details.
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

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

You'll have to implement plainScore() correctly to get that second test to pass though. We can talk about how to do that next.



So I made the test pass, but I still struggle to understand how to increment the String



A soon as I think about incrementation, I fail

 
Marshal
Posts: 68115
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Design problem: the lowest score isn't love‑all but love.
Design problem: find out what the following code will print:-Syntax problem: you can only apply the ++ operator to a number.
 
Lg Long
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Design problem: the lowest score isn't love‑all but love.
Design problem: find out what the following code will print:-Syntax problem: you can only apply the ++ operator to a number.



Asked this question yesterday!
So how do I solve that?

At the moment I am blocked because of syntax and I can't seem to get a straight answer.
 
Lg Long
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Until someone replyes how to solve that problem. This is passing the tests.

This starts to torment me.

 
Lg Long
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone answer if this is possible or not?
 
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:So I made the test pass, but I still struggle to understand how to increment the String


You don't. See, this is one of those situations where you keep wanting to do the wrong thing and we're trying to steer you away from it.

Let me give you another example. Say you had airline priority levels of  "Basic Economy", "Main2", "Main1", "Preferred", "Business", and "First". These are all strings. However, you want to be able to track the level as a number, so you can easily perform math on it, like to go from one level to the next, you'd just increment the level field.

If I wanted to track the level as a number but wanted to display the equivalent priority as a String, then I'd need a way to map the level number to the priority string, right? So, as I have said a few times already, an array is a way to do exactly that, using the index as key.


See how this works? You'd upgrade someone by increasing the level by 1 (a numeric operation) but then use that value as an index into the String array to get the name for display. If level = 1, then the PRIORITY[level] maps to "Main2". If you increase level to 3, then PRIORITY[level] is "Preferred" and when you increase it to the maximum possible index value of 5, then PRIORITY[level] will give you "First Class".

Now, try applying this idea so you can keep track of points won by a player with an int and get "Love", "15", "30", or "40" as their equivalent score for display.
 
Lg Long
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you!

I need to go for a walk now, so I can keep doing this. I want to have a happy path by the end of today, so I can apply it to other scenarios as well.
Coming back to pass the other tests!

 
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
Yes. Now you're getting somewhere. Go on to the next test. You might want to circle back to the first one that says "It starts at Love All"

This is really a special case of when both players have the same number of points.

The logic is "If both players have the same number of points, then display the score (either player's score will do since they have the same score) followed by the word 'all'."

So instead of "Love - Love" as the plainScore() method will give you, you want to check for the special "tied" condition and display "Love all" instead. This same logic should handle the scenarios where the score is tied at 15 and 30 as well. At 40-40, you have another special scenario, where you're going to display "Deuce" instead of "40 all".

We'll talk about refactoring that to something simpler once you get it working.
 
Lg Long
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, just wanted to pass this one to pass.
Good idea to do the one with Love-all

 
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
Yes! So, you see how you're slowly building up your program to make the tests you have so far pass? Each test forces you to add a little bit of code without disturbing the code you have that already works. This looks great!
 
Lg Long
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I need to tell my program that if serverWinsPoint() || winnerWinsPoint() haven't been instantiated to return Love all


Can i get some hints from another example?
 
Lg Long
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I keep getting logged out and i can't find that button to keep me logged in. Any other sigestions?
 
Lg Long
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Lg Long
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
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
What idea are you trying to express with this code?

If the method is always going to return true anyway, what's the point of having it as a boolean method?
 
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
It seems to me you've fallen back into a confused state. To get out of it, go back to the tests and gather your thoughts. Figure out what the next bit of functionality is that you want to implement.
 
Lg Long
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I was trying to to was to check that none of the values from the array apply to either server or recieever, sort of like null. But they are not null, because the game starts at Love Love. So i was thinking how could I fool the game in to not thinking it starts at "Love" to print "Love all".

I realised i don't, I just had to ckeck that it starts at Love
 
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:So I need to tell my program that if serverWinsPoint() || winnerWinsPoint() haven't been instantiated to return Love all


That doesn't make any sense at all. It really is as simple as "When the game starts, the score is 'Love all'" -- if you want to be a little bit more technical, "The score for a new Game is 'Love all'"

I already gave you hint that "Love all" is just another case of a "tied score". There are only three cases: "Love all", "15 all", and "30 all". When the score is 40-40, you don't say "40 all", you say "Deuce" instead. So 40-40 is another special case to handle.

Look at the pattern:
0 - 0 ==> "Love all"
15 - 15 ==> "15 all"
30 - 30 ==> "30 all"

What is the condition implied by the left side and how do you come up with the right side. You already have everything you need to do this.
 
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
Try to think in as simple terms as possible. The code you've written is way too complicated for the task of printing "Love all" when both players have 0 points each, "15 all" when both players have 1 point each, and "30 all" when both players have 2 points each. I'm practically giving you the logic you need to write there.
 
Lg Long
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Lg Long
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code up to date with tests passing





Next batch of tests to pass

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


@FunctionalInterface
interface Procedure {
   void run();

   default Procedure andThen(Procedure after){
       return () -> {
           this.run();
           after.run();
       };
   }

   default Procedure compose(Procedure before){
       return () -> {
           before.run();
           this.run();
       };
   }
}



This is what you've used to spare you duplicating code. For now I will stick with duplication



   public String getScore() {
       if (hasWinner()) {
           return winner();
       }
       if (isTied()) {
           return isDeuce() ? "Deuce" : tiedScore();
       }
       if (atAdvantagePoint()) {
           return isServerUp() ? "Ad in" : "Ad out";
       }
       return normalScore();
   }




This is where I need to change my present method getScore to normalScore and build the new method getScore() with the new methods in it.
 
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:


You can simplify this further. There's a lot of duplicated logic there, particularly on lines 2 - 11. What is the idea behind the formula of SCORE[serverPoints] == "Love" && SCORE[receiverPoints] == "Love", SCORE[serverPoints] == "15" && SCORE[receiverPoints] == "15", and SCORE[serverPoints] == "30" && SCORE[receiverPoints] == "30"?

Hint: there's only ONE idea behind all three of those expressions. You should be able to express that idea in one line rather than three separate lines.
 
Campbell Ritchie
Marshal
Posts: 68115
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code is only pretending to show correct use of the == operator. Be sure to read that link.
 
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
Campbell is right. I'd avoid comparing the Strings anyway because if SCORE[serverPoints] equals SCORE[receiverPoints], doesn't it follow that serverPoints and receiverPoints have a similar relationship? That's really the idea your going for, isn't it?

What I'm getting at is that you can reduce the three specific if-statements you have now down to one general if-statement that covers all three cases. And it will be much simpler than any of the expressions you have now.
 
Lg Long
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


What I'm getting at is that you can reduce the three specific if-statements you have now down to one general if-statement that covers all three cases. And it will be much simpler than any of the expressions you have now.



Hope I am going to finish tmrw. First finish then remove duplication Thank you for today.
 
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
You already finished one little bit of functionality and made the test pass. Don't try to move on until you remove the duplication. If you keep moving forward with the duplication, it's going to get in the way of your progress. You want to clean up small messes as you make them. You mgiht think that you can get done faster by postponing refactoring but every time you add functionality on top of messy code, it will get progressively harder to work with and understand and you will just get progressively slower and slower. As a wise man once said, "The only way to go fast is to go well."

So remove duplication NOW.
 
Marshal
Posts: 6869
182
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lg Long wrote:Hope I am going to finish tmrw.


I have a small administrative duty to be done: At Coderanch we frown upon acronyms and contraction that aren't well established.  NASA is fine but BRB is not.  This is because they can be confusing to people who aren't native speakers of English.  tmrw = tomorrow may be obvious to us, but to others it may not be.
 
Lg Long
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


So remove duplication NOW.





I am confused on this. It seams as I need to create 2 methods:
     
So, in my understanding winner(), checks for rulles and the we have a boolean method called hasWinner??
 
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
1. you can simplify the formula SCORE[serverPoints].equalsIgnoreCase(SCORE[receiverPoints]) even further. Also, it's a formula. I already showed you (and you even quoted it recently) how to refactor so the code expresses the IDEA behind the formula. What does that expression mean? And if SCORE[serverPoints] is the same as SCORE[receiverPoints], what does that imply about serverPoints and receiverPoints?

2. winner() simply gives back the String to display when there's a winner. It could be "Server wins" or "Game, Server" or whatever message you want to display when someone has won. The hasWinner() method is really simple: it's a single return statement. No "rules" evaluated in my implementation.
 
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
You might be wondering why I go through all the trouble of extracting expressions to their own methods. The goal is expressiveness. Code spends more time being read than it spends being written. If you can make code easier to read, then you're saving yourself and other people time.

Some might point to that code and say I'm refactoring too much and that I'm creating performance overhead with all these method calls. I would argue that a good optimizing compiler will inline all those one-line methods anyway, so why not take advantage of that and make the code easier to understand? That way I get the readability benefit while taking full advantage of the optimization capabilities that the compiler has. It's actually a win-win.
 
Campbell Ritchie
Marshal
Posts: 68115
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . a win-win.

No, that's called win all
 
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

Campbell Ritchie wrote:

Junilu Lacar wrote:. . . a win-win.

No, that's called win all


I see what you did there. Good one.  
 
Lg Long
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1. you can simplify the formula SCORE[serverPoints].equalsIgnoreCase(SCORE[receiverPoints]) even further. Also, it's a formula. I already showed you (and you even quoted it recently) how to refactor so the code expresses the IDEA behind the formula. What does that expression mean? And if SCORE[serverPoints] is the same as SCORE[receiverPoints], what does that imply about serverPoints and receiverPoints?




I am going down on my concentration, so unless it is of outmost necessity to simplify even more. I will invoke 'if it works it is good enough'. otherwise, help.
If you give me a better answer I don't think you brake the rules of the ranch, I came up with a solution. I am a noob, it is pointless to tore me apart. I understand it is correct.
But following the rules doesn't need to be in a pedantic way
Code, does.
 
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
Not trying to tear you apart. I'm trying to help you see how to do it in a better way. But I get it, I can be a tough coach with all the pushing and prodding. If you feel you've given all you can give, that's fine.

Simplicity can be tough to see, I get it.  If SCORE[serverPoints] equals SCORE[receiverPoints] then doesn't it follow that serverPoints == receiverPoints? And what's the idea behind that formula? Isn't it that "the score is tied"? Hence, the method isTied() is simply this:


I don't see this as being really complicated. It's just a matter of clearing your mind of all the clutter and confusion and coming up with the simplest way to express an idea. That's all.
 
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 really hope you can look at the critiques I've given as constructive criticism. Again, everything I've suggested was given with the hope that you'd see a better way out of your current situation. But then again, it seems I've pushed you beyond your limit and maybe it's my turn to give it a rest now.  Good luck.
 
Lg Long
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope, you are my hero rancher!


Was working on it. I do get confused a lot. And I can't remember what I did wrong or good.
Should be tough on you too to see me struggling and not giving up. Good that I still have time, I should finish by tomorrow, otherwise I won't be able to study all I've done.

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

Junilu Lacar wrote:I really hope you can look at the critiques I've given as constructive criticism. Again, everything I've suggested was given with the hope that you'd see a better way out of your current situation. But then again, it seems I've pushed you beyond your limit and maybe it's my turn to give it a rest now.  Good luck.



You did, that's what coaches do. So thank you. And be sure that I take all the critique in a healthy way.
So, be sure I think of you with appreciation!

I am also working, so I have to solve other problems too. That is why my energy is divided.
Going for a walk now. Will continue when I come back!
 
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:Nope, you are my hero rancher!


Thanks. Then I will proceed with padded gloves on

You're actually doing a lot better than you were just a couple of days ago, so don't get discouraged now.

Here's something to think about:

This is what that code is doing:
1 - if the server has won, "Server wins" -- you (kind of) have this, good.
2 - if the receiver has won, "Receiver wins" -- you also kind of have this, good.
3 - if nobody has won, give the score. (Wait, what? )

Consider #3 - is that the right thing to do? First, doesn't the getScore() method already have that line of code? Why duplicate it here?

Let me put it in a different way. Suppose I was trying to explain to you how this works and I said: To answer the question "Who is the winner?" you will need to call the winner() method. If the server has won, the answer will be "Server wins," otherwise if the receiver has won the answer will be "Receiver wins," otherwise the winner is the score.

That last part doesn't make sense does it? But that's what the return statement on line 21 basically says.

If the code doesn't tell a sensible story, that's a signal to you that it's not the correct logic. I like to think of it as the code telling me "Hey, programmer, that's not what I want to do! Fix 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
Here's another thing to think about: as soon as a player wins a point, you can immediately determine if they have won the game or not. What methods do you have that get called whenever a player wins a point? That's where you should be checking if the player just won the game. If you determine that a player has won the game, then you can set a field that will tell you which player just won. If that field has no value, then you know there's no winner yet.
 
A feeble attempt to tell you about our stuff that makes us money
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!