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 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.
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.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Karen Barlow wrote:Note: Do not use Arrays, ArrayLists, or sorting algorithms to solve this question.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Karen Barlow wrote:
Note: Do not use Arrays, ArrayLists, or sorting algorithms to solve this question.
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?
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.Junilu Lacar wrote:. . . This is not how you would program in real life and I see no value in teaching programming this way. . . .
(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 + "!");
}
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ł Baczyński wrote:. . .
So you could write:. . .
Karen Barlow wrote:Note: Do not use Arrays, ArrayLists, or sorting algorithms to solve this question.
Carey Brown wrote:Contestant, that had a name and a score with a constructor that filled those in from a Scanner.
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.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.
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.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here