String notSecret = "RmFsbCBCcmVhayE="; String decoded = new String(java.util.Base64.getDecoder().decode(notSecret)); System.out.println(decoded);-- Age Old Saying
This class exercise explores functional interfaces, lambda expressions, and method references in conjunction with generic methods and interfaces. In this exercise, only the generic method signatures will be provided. Implementation details are left to the student.
- LO2.d: (Partial) Implement new generic methods, interfaces, and classes in a software solution.
- LO2.e: (Partial) Utilize existing generic methods, interfaces, and classes in a software solution.
- LO4.a: (Partial) Design, create and use interfaces in a software solution.
- LO4.b: (Partial) Utilize interface-based polymorphism in a software solution.
- The Java™ Tutorials: Generic Methods
- The Java™ Tutorials: Lambda Expression
- The Java™ Tutorials: Method References
java.util.function.Predicate
Interface Documentationjava.util.function.Function
Interface Documentationjava.util.function.IntFunction
Interface Documentation
In your notes, clearly answer the following questions. These instructions assume that you are logged into the Odin server.
NOTE: If a step requires you to enter in a command, please provide in your notes the full command that you typed to make the related action happen. If context is necessary (e.g., the command depends on your present working directory), then please note that context as well.
-
Use Git to clone the repository for this exercise onto Odin into a subdirectory called
cs1302-ce18
:$ git clone --depth 1 https://github.com/cs1302uga/cs1302-ce18.git
-
Change into the
cs1302-ce18
directory that was just created and look around. There should be multiple Java files contained within the directory structure. To see a listing of all of the files under thesrc
subdirectory, use thefind
command as follows:$ find src
-
Take a close look at the
printlnMappedMatches
method and its associated Javadoc inMoreLambdaFun.java
. The exact signature for this method is:private static <T> void printlnMappedMatches(T[] t, Predicate<T> p, Function<T, String> f)
Implement the
printlnMappedMatches
method inMoreLambdaFun.java
. You do not need to use a lambda for this step. You will only need to use theFunction<T, R>
andPredicate<T>
references to call the appropriate methods. -
At the end of the
main
method ofMoreLambdaFun
class:-
Declare a variable
bonusPred
of typePredicate<Quiz>
and assign to it, using a lambda expression, a reference to an object that tests if the quiz is a bonus quiz. Remember, you can always refer to the API documentation for the associated interface when trying to determine the layout of your lambda. -
Declare a variable
numberFunc
of typeFunction<Quiz, String>
and assign to it, using a lambda expression, a reference to an object that takes aQuiz
object as a parameter and returns aString
containing the quiz number and at least one other piece of information about the quiz (other than bonus). Remember, you can always refer to the API documentation for the associated interface when trying to determine the layout of your lambda.
-
-
Call the
printlnMappedMatches
using your newly created variables. Make sure to provide sufficient output so that it will be easy to convince your TA or instructor that everything is working properly. -
Compile your code and run it. Then, stage and commit all changes.
-
Using lambdas, write the code to print quiz numbers for all quizes that have a time limit that is under twenty minutes. Make sure to provide sufficient output so that it will be easy to convince your TA or instructor that everything is working properly.
-
Make sure your code passes the
checkstyle
audit then stage and commit all changes.
-
Take a close look at the
copyOf
method and its associated Javadoc inMoreLambdaFun.java
. The exact signature for this method is:public static <T> T[] copyOf(T[] a, int l, IntFunction<T[]> g)
Answer the following questions about this method in your notes:
-
What is the generic type parameter?
-
Specifically, what reference types can replace
T
? -
In order to call this method, we need a reference to an object of a class that implements
IntFunction<T[]>
. The interface is defined asIntFunction<R>
. Here, we replaceR
withT[]
. Keep this in mind when inspecting the API documentation forIntFunction<R>
.
Hint: See the
toArray
documentation from theGenList
project, which also takes anIntFunction
parameter.- Write the full, expanded method signature (with the replaced types) of the single, abstract method
of
IntFunction<T[]>
in your notes. Pay careful attention to the return type and the type of the formal parameter.
-
-
After carefully reading its Javadoc comment, implement the
copyOf
method inMoreLambdaFun.java
. You do not need to use a lambda for this step. -
Stage and commit all changes.
-
At the end of the
main
method of theMoreLambdaFun
class:-
Declare a variable
lessQuizzes
of typeQuiz[]
and, using yourcopyOf
method, assign to it a copy of the array referred to byquizzes
containing half as many quizzes (containing the first half of the quizzes). You should supply theg
parameter using a regular lambda expression.Hint: See the
toArray
documentation from theGenList
project, which also takes anIntFunction
parameter. -
Using a for-each loop, print the quiz numbers of all
Quiz
references inlessQuizzes
. -
Declare a variable
moreQuizzes
of typeQuiz[]
and, using yourcopyOf
method, assign to it a copy of the array referred to byquizzes
containing four (4) more spots for quizzes (allnull
). You should supply theg
parameter using an array constructor method reference. -
Using an appropriate loop, print the quiz numbers of all non-null
Quiz
references inmoreQuizzes
. Also, print the length of the array to make sure it is the proper size.
-
-
Make sure your code passes the
checkstyle
audit then stage and commit all changes.
-
Create and document a
static
method calledmakeQuizArray
that, if written correctly, a method reference for that method can be used to supply theg
parameter in a call to yourcopyOf
method. Then, at the end of themain
method of theMoreLambdaFun
class:- Declare a variable
twentyQuizzes
of typeQuiz[]
and, using yourcopyOf
, assign to it a copy of the array referred to byquizzes
containing twenty spots for quizzes (with trailingnull
values as described in the Javadoc forcopyOf
). You should supply theg
parameter using a method reference to the method you created in the previous step. - Using an appropriate loop, print the quiz numbers of all non-null
Quiz
references intwentyQuizzes
. Also, print the length of the array to make sure it is the proper size.
- Declare a variable
-
Make sure your code passes the
checkstyle
audit then stage and commit all changes.
Each student needs to individually submit their own work.
-
Create a plain text file called
SUBMISSION.md
directly inside thecs1302-ce18
directory with the following information.- Your name and UGA ID number;
- Collaborator names, if any; and
- If you created the API website, include the full link to the site you generated.
Here is an example of the contents of
SUBMISSION.md
.1. Sally Smith (811-000-999) 2. Collaborators: Joe Allen, Stacie Mack 3. https://webwork.cs.uga.edu/~user/cs1302-ce18-doc
-
Change directories to the parent of
cs1302-ce18
(e.g.,cd ..
fromcs1302-ce18
). If you would like to make a backup tar file, the instructions are in the submissions steps for ce02. We won't repeat those steps here and you can view them as optional. -
Use the
submit
command to submit this exercise tocsci-1302
:$ submit cs1302-ce18 csci-1302
Read the output of the submit command very carefully. If there is an error while submitting, then it will displayed in that output. Additionally, if successful, the submit command creates a new receipt file in the directory you submitted. The receipt file begins with rec and contains a detailed list of all files that were successfully submitted. Look through the contents of the rec file and always remember to keep that file in case there is an issue with your submission.
Note: You must be on Odin to submit.
Copyright © Michael E. Cotterell, Bradley J. Barnes, and the University of Georgia. This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License to students and the public and licensed under a Creative Commons Attribution-NonCommercial 4.0 International License to instructors at institutions of higher education. The content and opinions expressed on this Web page do not necessarily reflect the views of nor are they endorsed by the University of Georgia or the University System of Georgia.