Example code : introductory object/enum practice for CS152L at the University of New Mexico
Car example should be worked through first. Students will refresh their knowledge of the purpose of objects, what a constructor does, what member variables are, the scope of local variables in a method vs. scope of member varibales and when to use "this" keyword.
Students will then write basic getters for year, make and model and be able to explain why setters for these values don't make sense in this case (can't change year car was made after it's already been created) and discuss the importance of access modifiers.
Students will then talk about the purpose of enums, how they can be used to make code more readable and represent constants, and will write methods to paint the car a specific color. Make sure to mention why it can be useful to limit access in this way and not allow any color to be passed in.
Students will then write toString() and equals() methods. Lots of questions came up here for me, equals seems to be tough to grasp for students. It's important to explain the necessity of writing our own equals functions for objects.
The next example is a deck of cards. This expands more on enums and how they can be useful. I used the values() function to give students an example of how useful enums can be. Important to also talk about why we can directly print enums and don't need a toString function explicitly called.
Students will then write an equals method for Card, comparing two enums and getting more practice and understanding of the importance of equals.
*SOLUTION files have all the solutions to methods that students should finish in lab