A04

Due Date: Thursday, October 8
File(s) to be submitted: Validator.java, A04.java
Sample Output: SampleOutput.html


SUBMIT   /   Check

Sets, Interfaces and Lambda Expressions

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.

Details

The Validator

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:

evens.isValid(2); // returns true evens.isValid(3); // returns false

The Validator can be used to test an entire array or collection:

evens.areValid(new int[]{2, -4, 10}); // returns true evens.areValid(List.of(2, 10, 19)); // returns false evens.areValid(Set.of(-2, 100, 195044)); // returns true

The Validator interface has a method to combine two other Validators. Suppose positive is a Validator<Integer>. Then:

Validator<Integer> evenPos = Validator.combine(even, positive);
creates a Validator that checks whether the item (or collection) is both positive and even.

The Sample Driver Program

Create a program named A04. It must carry out the following steps, pausing after each step:

  1. Introduce itself and you as its author.
  2. Create the evens Validator mentioned above and show that it gives the right answers for 2 and 3.
  3. Create the positive Validator mentioned above and show that it gives the right answers for 104 and -3.
  4. Create a Validator that combines the evens Validator above and another validator that checks whether a number is in the range 10 to 100 (inclusive).
  5. Read numbers from the user until they enter a negative number. For each number, report whether it is an even numbers between 10 and 100.
  6. Create an empty TreeSet of Strings.
  7. Create a Validator that confirms that a String has a length between two and five (inclusive).
  8. Read lines from the user, until the user enters an empty line. For each line, check whether all the words on that line meet the requirement above. If so, add all those words to the set. If not, report that they're not being added.
  9. Print out the set of words.

Hints

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.

String[] words = line.split("\\s+");
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 (\) 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 \.
The \s part 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"].


SUBMIT   /   Check