Name (print clearly): _________________________________________________
Student ID (print clearly; 1 digit per underline): _____ _____ _____ _____ _____ _____
CSE 231s Quiz 11: Student Credits and Optional<T>
The University Registrar has assigned you the task of checking to ensure that every student is carrying the appropriate number of credits.
Their database is organized by courses.
Given the API below write a
combination of high-level design document and pseudocode to solve this problem.
final int MINIMUM_ALLOWED_CREDITS = ...;
class Course:
List<Student> getStudents()
int getNumCredits()
Course[] courses = ...;
Imagine while building the selectNextUnfilledSquare method on the RowMajorSquareSearchAlgorithm you built a utility method isFilled:
private static boolean isFilled(ImmutableSudokuPuzzle puzzle, Square square) {
if(!puzzle.getValue(square).equals(Optional.empty())) {
return true;
} else {
return false;
}
}
Given the documentation for java.util.Optional<T>, implement a better method below:
-
Returns an empty Optional instance. No value is present for this
Optional.
- API Note:
- Though it may be tempting to do so, avoid testing if an object is empty
by comparing with
== against instances returned by
Optional.empty(). There is no guarantee that it is a singleton.
Instead, use isPresent().
- Type Parameters:
T - The type of the non-existent value
- Returns:
- an empty
Optional
-
public static <T> Optional<T> of(
T value)
Returns an Optional describing the given non-null
value.
- Type Parameters:
T - the type of the value
- Parameters:
value - the value to describe, which must be non-null
- Returns:
- an
Optional with the value present
- Throws:
NullPointerException - if value is null
-
If a value is present, returns the value, otherwise throws
NoSuchElementException.
- API Note:
- The preferred alternative to this method is
orElseThrow().
- Returns:
- the non-
null value described by this Optional
- Throws:
NoSuchElementException - if no value is present
-
public boolean isPresent()
If a value is present, returns true, otherwise false.
- Returns:
true if a value is present, otherwise false
-
public boolean isEmpty()
If a value is not present, returns true, otherwise
false.
- Returns:
true if a value is not present, otherwise false