I’m thinking of adding an “Approaches” section for this exercise, comparing both the traditional and modern switch statements. Below are the proposed solutions for both approaches:
Traditional switch Statement
public class FootballMatchReports {
public static String onField(int shirtNum) {
String playerDescription = "";
switch (shirtNum) {
case 1:
playerDescription = "goalie";
break;
case 2:
playerDescription = "left back";
break;
case 5:
playerDescription = "right back";
break;
case 3:
case 4:
playerDescription = "center back";
break;
case 6:
case 7:
case 8:
playerDescription = "midfielder";
break;
case 9:
playerDescription = "left wing";
break;
case 11:
playerDescription = "right wing";
break;
case 10:
playerDescription = "striker";
break;
default:
throw new IllegalArgumentException();
}
return playerDescription;
}
}
Modern switch Statement (Java 12 and Later)
public class FootballMatchReports {
public static String onField(int shirtNum) throws IllegalArgumentException {
return switch(shirtNum) {
case 1 -> "goalie";
case 2 -> "left back";
case 3, 4 -> "center back";
case 5 -> "right back";
case 6, 7, 8 -> "midfielder";
case 9 -> "left wing";
case 10 -> "striker";
case 11 -> "right wing";
default -> throw new IllegalArgumentException();
};
}
}
Discussion
I believe both approaches would make a great addition to the exercise’s “Approaches” section. However, I’m unsure whether learning exercises typically include such a section, as I haven’t seen any examples yet. The traditional approach highlights the use of switch with break statements, while the modern approach demonstrates the more concise, functional style introduced in Java 12.
@kahgoh and @everyone, what are your thoughts on adding this “Approaches” section?
Both switch statements and expressions are already covered in the associated concept for this learning exercise. That concept introduces what’s needed for Football Match Report, so students should already be exposed to both of these.
Approaches highlight different ways to solve an exercise, and generally concept exercises teach a specific way. These approaches aren’t different from the way students are intended to solve the exercise.
Thank you both, @BNAndras and @tasx for your feedback! I now understand that the Approaches section is meant to highlight distinctly different ways to solve an exercise, while this proposal mainly compares styles rather than fundamentally different solutions.
@BNAndras mentioned that both switch statements and expressions are covered in the associated concept. However, I noticed that the concepts only provide examples of traditional switch statements, not the modern ones. I believe a comparison of the two could still be beneficial for learners.
If you think this would work better as an article rather than part of the exercise, I’m happy to write one. Perhaps it could be linked in the mentor guide or as additional reading for those curious about Java’s newer features.
That said, if you feel this isn’t a valuable addition since both approaches ultimately rely on switch, I completely understand and will consider this topic closed.
Hello! Thanks for your enthusiasm for helping. It’s really appreciated!
To add some wider context…
Articles and Approaches are designed for Practice Exercises, not Learning Exercises. I’m not even sure they will appear for Learning Exercises. That’s because:
Learning Exercises should contain everything a student needs to know in the introduction.md and further information in the after.md.
Practice Exercises are designed to be solved in many different ways, and in fact part of the joy is exploring the different possibilities. Approaches and articles serve to highlight those differences.
So really, one of a couple things is happening here:
You’re wanting to improve the after.md here;
Or maybe you’re thinking the exercise has different possible valid solutions, which is either wrong (as it’s supposed to be solved in one way in line with the introduction) or the exercise isn’t explicit enough as to what a student is supposed to be using/learning in this exercise. (I’ve not looked at the content, so I’m not commenting on that).
Whereas really, what would be good is to take a popular Practice Exercise and explore these different approaches in that.
Thank you so much for the clarification and detailed explanation! It’s clear now that Articles and Approaches are specifically designed for Practice Exercises, not Learning Exercises. Given this, I understand that my suggestion doesn’t align with the intended purpose of the exercise format.
Since the current proposal doesn’t fit within the scope of Learning Exercises and doesn’t warrant an article or an Approaches section for this context, I’ll go ahead and close this topic.
Thank you again for your time and insights—it’s always helpful to learn more about the structure and philosophy behind Exercism!