"Advanced" Java Practice
Java has many advanced features that may take some effort to understand how to use properly (or even well :) but that require very little syntax. In most cases, they may not impact your design dramatically, but they can make your code much cleaner and understanding them well can help you think about design more deeply.
Here are ones we have discussed in class
To work on this exercise, fork the original project lab_advanced
into your own repository so you can edit, commit, and push your group's own changes.
Work in pairs to implement the changes described below to example code we have discussed this semester. Create a commit for each step below as your programming pair completes it.
Bins
- Change the
fitDisksAndPrint()
method so that it takes an third parameter of typeConsumer<List<Integer>>
whose instances transform a list in some way (e.g.,sort
,shuffle
, orreverse
). Within thefitDisksAndPrint()
method, you should simply call the parameter'saccept()
method with the correct value. Within themain()
method, change the calls to thefitDisksAndPrint()
method to provide this third parameter using Lambdas rather than writing out the complete code to declare and instantiate class instances that implementConsumer<List<Integer>>
.
Roulette
- Create a
Factory
class that manages returningBet
subclasses. Initially, use an array (or perhapsList<Bet>
) of hard coded subclasses. TheFactory
should also handle printing out the menu of choices rather than theGame
class doing that.
- Change the
Factory
class to use reflection instead of hardcoding the creation of theBet
subclasses. Initially, hardcode the strings in your collection instead ofBet
subclass instances. Note, you will either need to add a default constructor to eachBet
subclass or create some logic to provide the description and odds for each subclass. Your implementation does not really matter for this step because the focus here is on understanding reflection, the next step will provide a more general solution.
- Create a properties file that maps the complete
Bet
subclass name to its odds and description (on one line separated by a comma for easy parsing). Read this property file to determine which subclasses your factory should create so that nothing is hardcoded in yourFactory
class.
Browser
- Change the
makeButton()
method so that it that takes aString
parameter instead of a Lambda and uses reflection to convert that to a method. Change the call tosetOnAction()
to callinvoke()
on theMethod
object created using the given string. - Change the
makeButton()
method so that it that only takes oneString
parameter (i.e., no extra parameter telling it what action to perform). Instead create another properties file that maps the button's key name (the same one used in the original properties file) to the name of the method to invoke when it is pressed. After loading the both properties files, you should be able to use the one given string to look up both the text for the button to display (in the original resource file) and the action for the button to perform (in the new resource file). Again, this reduces the assumptions and what is hardcoded in your program.
Your Projects
If you have completed all of the practice steps, try updating some of your own code with some of these features (such as, enumerated types in Game or Cell Society, lambdas in Cell Society or SLogo, reflection in any program).
Submission
At the end of class, use Gitlab's Merge Request to submit your pair's refactored code to the lab's organization repository. Make sure the NetIDs of everyone in the group are in the title of your Merge Request.