• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

How to amend my program so that if there was a tie?

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all, I was wondering if anyone could give me some insight on the second part of the following question, on how to amend my java program to consider a tie with contestant's.

(First part of question) Write a Java application that is designed to help determine the winner of a video game contest. The program takes in the names and points-scored of three competitors. Then it prints out who came in first (highest score), who is second and who is third (lowest score). If any of the scores are <0, stop the program with an error immediately.

(Second part of question) Amend your program so that if there is a tie, that is taken into account. For example, if the top two players are tied, they are both awarded first place and the remaining player gets third. if the last two players are tied they both get second. If all three players are tied they all get first, and so on.
Note: Do not use Arrays, ArrayLists, or sorting algorithms to solve this question.

(How I did the first part of the question is displayed below)...as far as the second part of the question is concerned, do I just change some things that I wrote in the first part of my edit, ie.) >=. I literally have no idea how I would edit the program to take into account a tie. ANY HELP IS GREATLY APPRECIATED!!

First part of question I wrote as follows:

Any insight on how to do the second part is greatly appreciated. Thank you in advance.
 
Bartender
Posts: 2237
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your solution does not scale well. It may work for 3 players.
What if there are 4 players? Or 1000? How many ifs would you need to write?

I would create a class that would implement Comparable (comparing by score) that would hold player's name and score.
Then it would be easy to sort a list of such objects and handle ties.
 
Bartender
Posts: 11103
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:Your solution does not scale well. It may work for 3 players.
What if there are 4 players? Or 1000? How many ifs would you need to write?

I would create a class that would implement Comparable (comparing by score) that would hold player's name and score.
Then it would be easy to sort a list of such objects and handle ties.

Agreed, but it doesn't take into account the fine print: Note: Do not use Arrays, ArrayLists, or sorting algorithms to solve this question.
 
Paweł Baczyński
Bartender
Posts: 2237
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ouch! Its like: finish a marathon but don't use legs.

If this doesn't need to scale I would create three variables: first, second and third.
Then I would assign I1 to first. Then I would check if I2 is greater than I1 and assign them both to first and second accordingly.
Then... Maybe you figure out the rest

And welcome to the Ranch, Karen!!!
 
Karen Barlow
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the feedback and the greetings...

I really appreciate your suggestion, I've tried what you recommended but I can't seem to wrap my head around in which order to input it. I am so very new to programming that I haven't quite got the grasp of the basics. When you suggest that I assign I1 to first, then does that mean i assign I2 to second, and I3 to third? I was under the impression that since I am determining if there was a tie the I should be using (==)?

I'm still very lost.
 
Paweł Baczyński
Bartender
Posts: 2237
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, if you are lost, then StopCoding (this is a link).
How would you do that on paper?
 
Karen Barlow
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I just tried to break it down on paper, but not sure if it makes sense, and if it does make sense, then I don't know how I would go about inputting this into an editor.
The following is what I have figured out...

If there is a tie??

if I1's score equals I2's score, and they're greater than I3's score, then I1 and I2 are in First Place and I3 is in Third place.
if I1's score equals I2's score, and they're less than I3's score, then I1 and I2 are in Second Place and I3 is in First place.
if I1's score, I2's score, and I3's score all equal each other then all player's get First place.

if I1's score equals I3's score, and they're greater than I2's score, then I1 and I3 are in First Place and I2 is in Third place.
if I1's score equals I3's score, and they're less than I2's score, then I1 and I3 are in Second Place and I2 is in First place.
if I1's score, I2's score, and I3's score all equal each other then all player's get First place.

if I2's score equals I3's score, and they're greater than I1's score, then I2 and I3 are in First Place and I1 is in Third place.
if I2's score equals I3's score, and they're less than I1's score, then I2 and I3 are in Second Place and I1 is in First place.
if I1's score, I2's score, and I3's score all equal each other then all player's get First place.


Do I appear to be on the right track, and if so, is there any advice as to how I would write that for java?
 
Marshal
Posts: 80747
486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Yes, that sort of logic will work, but you will have to rewrite it entirely for four competitors and when you have got that to work you would have to rewrite it for five competitors and if a sixth competitor takes part … etc. etc.

If you are not allowed to use arrays etc. you are presumably not allowed to use the really good functionality of things like Stream either. So you are going to have to break things down and restrict things. It says 1st is highest score and 3rd is lowest score, so you are going to have to restrict yourself to three players. Then you have the two options: there is a tie and there isn't. You aren't going to be able to write all that lot in the main() method. Nor should you even try. Divide and conquer. Find a way to get the top middle and bottom of the three marks. Find a way to test whether there is a tie. But all those things represent different methods. I can think of several ways to do that.

Another way to do it: get 1st 2nd 3rd then check later whether any of them is tied and if so promote them.

But you cannot get any of that logic to work on screen. Not until you have got it to work on paper. Once you have it on paper and written down in words of one syllable, you will probably find it quite easy to turn into code.
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:[A]ll those things represent different methods.

Another way to do it: get 1st 2nd 3rd then check later whether any of them is tied and if so promote them.


This is a CRITICAL thing to learn when writing software. Separate out distinct "jobs" into methods. It shouldn't matter HOW you get the three scores, but assuming you have them, you would create a method to determine who wins. If I were writing psuedo code, I'd start with this

1) get scores in (note that here i dont' care who has the best score, i'm only getting the input)
2) arrange the scores and determine first place, second place, third place
3) output results

None of these in any way rely on any other. Methods are your friend. they're like lego blocks. Each one does something very simple that's easy to understand. Then you combine them into something huge and cool. Note that each of the three items above might be best done with several methods...
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Karen Barlow wrote:Note: Do not use Arrays, ArrayLists, or sorting algorithms to solve this question.


God, I hate restrictions like that. They're so darn vague.
1. Are you allowed to use a Stack?
2. Any successful code you write WILL BE a sort algorithm, so the restriction is oxymoronic.

Winston
 
Paweł Baczyński
Bartender
Posts: 2237
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regarding point 2, if we went deeper we could argue that you can't write any Java program without using arrays...
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Karen Barlow wrote:
Note: Do not use Arrays, ArrayLists, or sorting algorithms to solve this question.


[RANT]
Nothing against you but this kind of thing annoys the out of me. What is the teaching objective here, to learn how not to write a program? SMH. Sometimes I think the folks in academia are just so out of touch with industry and walking around with their heads in the clouds. This is not how you would program in real life and I see no value in teaching programming this way. It's like teaching kids how to drive but they can only use a horse and buggy.

[/RANT]
 
Carey Brown
Bartender
Posts: 11103
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Karen Barlow wrote:Well I just tried to break it down on paper, but not sure if it makes sense, and if it does make sense, then I don't know how I would go about inputting this into an editor.
The following is what I have figured out...

If there is a tie??

if I1's score equals I2's score, and they're greater than I3's score, then I1 and I2 are in First Place and I3 is in Third place.
if I1's score equals I2's score, and they're less than I3's score, then I1 and I2 are in Second Place and I3 is in First place.
if I1's score, I2's score, and I3's score all equal each other then all player's get First place.

if I1's score equals I3's score, and they're greater than I2's score, then I1 and I3 are in First Place and I2 is in Third place.
if I1's score equals I3's score, and they're less than I2's score, then I1 and I3 are in Second Place and I2 is in First place.
if I1's score, I2's score, and I3's score all equal each other then all player's get First place.

if I2's score equals I3's score, and they're greater than I1's score, then I2 and I3 are in First Place and I1 is in Third place.
if I2's score equals I3's score, and they're less than I1's score, then I2 and I3 are in Second Place and I1 is in First place.
if I1's score, I2's score, and I3's score all equal each other then all player's get First place.


Do I appear to be on the right track, and if so, is there any advice as to how I would write that for java?


Others have pointed out all the flaws with this assignment in general. I would say your approach would satisfy the requirements so just go ahead and get it over with and hopefully the instructor will have you go one to something more fruitful.
 
Campbell Ritchie
Marshal
Posts: 80747
486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . This is not how you would program in real life and I see no value in teaching programming this way. . . .

I had the good fortune to be taught Java™ at college by people who didn't think that way. Maybe we didn't use Lists in the first few weeks, but that was only because we had never heard of Lists.
 
Karen Barlow
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I of course also prefer a more efficient way of doing things, but I guess I have to learn the basics first the hard way...regardless, thank you for all you're comments. Ok, back to my dilemma...

(Second part of question) Amend your program so that if there is a tie, that is taken into account. For example, if the top two players are tied, they are both awarded first place and the remaining player gets third. if the last two players are tied they both get second. If all three players are tied they all get first, and so on.
Note: Do not use Arrays, ArrayLists, or sorting algorithms to solve this question.





if(I1 == I2 && I1 > I3){
System.out.println("First place: " + S1 + " & " + S2 + "!");
if (I2>I3){
System.out.println("Third place: " + S3 + "!");
}
if(I1 == I2 && I1 < I3){
System.out.println("First place: " + S3 + "!");
if (I2<I3){
System.out.println("Second place: " + S1 + "&" + S2 + "!");
}



When I try to run it in java the following is what occurs in command prompt...(this scenario is when I1=I2, and I3 is greater).
What does occur is as follows:
Please enter contestant's name:
I1
I2
I3
Please enter I1's score: 55
Please enter I2's score: 55
Please enter I3's score: 60

First place: I3!
Second place: I2!
Third place: I1!


What should occur is:
First place: I3
Second place: I1 & I2


When I run the opposite scenario (I1=I2, and I3 is less), what I'm trying to achieve occurs, in other words this way works.
Please enter contestant's name:
I1
I2
I3
Please enter I1's score: 55
Please enter I2's score: 55
Please enter I3's score: 50

First place: I1! & I2!
Third place: I3!


Does anyone know what I may have done wrong?? (I don't want to continue entering the next scenarios in, until I have figured out where my mistake is).
Hopefully I was thorough enough to explain the steps I have taken.

 
Paweł Baczyński
Bartender
Posts: 2237
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I disagree with the point that you need to learn it the hard way. Programming is meant to make your life easier!

I don't see anything in the assignment stating that you can't write your own class.
So I would write a class that would hold both score and player's name.

Then I would introduce variables to keep which score is first, which second and which is third.

Then I would split the solution into methods. For example, given three scores, find the first one, the second one and the third one.

So you could write:If you have this sorted out, finding ties would be very easy. You would need to compare first with third. If they are equal, all players are tied to 1st place.
Else, if first score equals second, there are two players sharing first place and one at third place.
Else, if second score equals third, there is one winner, and two players sharing second place.
Else, there is no ties.

Also, I can see that you have ignored this part If any of the scores are <0, stop the program with an error immediately.
This is fine, you can add it later. Just don't forget about it.
 
Campbell Ritchie
Marshal
Posts: 80747
486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:. . .
So you could write:. . .

That is exactly the sort of thing I was thinking of. As you can see, you will need to get 99% of that code out of the main method. All those if‑elses make for code difficult to read and therefore difficult to maintain. Also any errors are harder to find and correct.

Paweł is correct to suggest a class with name and score. I would suggest you create a Result enum:You can therefore give each competitor object a Result field. Unfortunately the easiest way to use that enum probably entails converting it to an array
 
Paweł Baczyński
Bartender
Posts: 2237
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Karen Barlow wrote:Note: Do not use Arrays, ArrayLists, or sorting algorithms to solve this question.


It says: do not use Arrays. Upper case. So I would not use java.util.Arrays class.
And I would use any list implementation that is not an ArrayList if I felt I need one. Like LinkedList.
I would be just stictly following specs.

The teacher would not like me.
 
Campbell Ritchie
Marshal
Posts: 80747
486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In which case my enum might be permissible after all.
 
Carey Brown
Bartender
Posts: 11103
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My approach was to create two classes:
Contestant, that had a name and a score with a constructor that filled those in from a Scanner.
Contestants, that had c1, c2, c3 variables to hold a set of three Contestant's. Use your original comparison logic to initialize c1/c2/c3 in order.
In main() you'll only have to compare adjacent set values for equality, e.g. set.c1.score == set.c2.score.
 
Paweł Baczyński
Bartender
Posts: 2237
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Contestant, that had a name and a score with a constructor that filled those in from a Scanner.


I don't know if I understand you correctly. Do you want to use Scanner inside a constructor?
That is not a good idea. This would broke the single responsibility principle.
The class would have two responsibilities: keep values and read input.
 
Carey Brown
Bartender
Posts: 11103
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:

Carey Brown wrote:Contestant, that had a name and a score with a constructor that filled those in from a Scanner.


I don't know if I understand you correctly. Do you want to use Scanner inside a constructor?
That is not a good idea. This would broke the single responsibility principle.
The class would have two responsibilities: keep values and read input.

Yeah, I broke a few golden rules for expediency, including not making fields private. It was just a quick and dirty attempt to see how small it could be made.
 
Carey Brown
Bartender
Posts: 11103
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fixed your concern with constructor:
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Yeah, I broke a few golden rules for expediency, including not making fields private. It was just a quick and dirty attempt to see how small it could be made.


Hmmm. My advice: DON'T DO THAT.

How much time does it take to type the word "private", and why should things be quick and dirty?
Why not quick and clean?

Winston's Golden Rule: There are two qualifiers in the Java language that are almost always underused - even by experts - private and final.

So: Don't.

Winston
 
reply
    Bookmark Topic Watch Topic
  • New Topic