Due Date:
Thursday, October 8
File(s) to be submitted:
Validator.java, A04.java
Sample Output:
SampleOutput.html
Summary
Create a Validator interface with abstract, default and static methods.Create a program that uses Validators to get and validate input from the user.
I've given you some code snippets below that you can use in that program.Make good use of lambda expressions.
Use good programming design, including DRY (Don't Repeat Yourself) methodology.
Validator is a generic interface
for checking whether an item or collection of items
meet certain criteria.
For example,
if evens
is a Validator
that checks whether an Integer is even,
then:
The Validator can be used to test an entire array or collection:
The Validator interface has a method to combine two other Validators.
Suppose positive is a Validator<Integer>.
Then:
Create a program named A04. It must carry out the following steps, pausing after each step:
evens Validator mentioned above
and show that it gives the right answers for 2 and 3.
positive Validator mentioned above
and show that it gives the right answers for 104 and -3.
evens Validator above
and another validator that checks whether a number is in the range 10 to 100
(inclusive).
Recall that a class or interface can be declared generic
by adding a type parameter after its name
(e.g., public interface Bag<T>).
Recall how the type parameter is used
to specify the return, parameter and local variable types.
The Validator interface will have four methods,
but only one will be abstract.
The others will have default bodies
or be static.
Why "four"? Aren't there just three?You're going to need two different versions of
areValid. One for arrays, and one for Collections.And even tho' the test program I asked you to create doesn't use them, you do need to implement them both. I will be checking.
The two default methods
do pretty much the same thing.
Look back in the notes
to see if there's an easy way
to make one of them call the other one.
The method to combine two Validators is pretty simple. It can be done in one line using a lambda expression.
The method to combine two Validators
needs to make sure that the two Validators
it's been given
both validate the same data type.
Since this method is static,
it method itself needs to be made generic.
Recall that a method is made generic
by adding a type parameter
before its return type
(e.g., private static <T> void printList(List<T> list)).
You might be thinking:
Hey! This interface is already generic.
How come I gotta make this method generic, too?
The answer is that the type parameter for the interface
only applies to objects of type Validator.
When a method is static,
it's not attached to any object.
Thus it needs its own type parameter.
To read multiple numbers on one line into an array, read in the whole line, then ask that line to split itself on spaces.
The String\s+is what's called a regular expression. This particular expression means any positive number of space characters.The command has two backslashes (The\) because Java Strings treat the backslash as an escape character. You need to type two of them so that Java knows you really want one. If you were to print it out, it'd have only one\.\spart means any space character (including a TAB character), while the+part means any positive number of what comes before.You don't need to understand regular expressions for this course, but it's something it's good to know about. In the example code above, if you just used
" "instead of"\\s+", then your program would split"two spaces"into["two", "", "spaces"]instead of into["two", "spaces"].