Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Her ligger løsningsforslaget til eksamen 2021. I kombinasjon med kildekoden vil vi beskrive hva oppgaven spurt etter, samt Vi vil senere legge til ofte forekommende feil og misforståelser i kombinasjon med kildekoden .

På gitlab finner dere full kode til alle deler i oppgaven.

...

Expand
titleDel 8 - Funksjonelle grensesnitt og lister (10 %)

Fyll ut UniversityHandbookUtils sine metoder for operasjoner på en liste med Course-objekter.

Følgende metoder skal implementeres:

  • getCoursesWithPredicate(Collection courses, Predicate p) - Returnerer alle emner i samlingen som tilfredstiller predikatet.
  • getNonPrequisiteCourses(Collection courses) - Returnerer alle emner i samlingen som ikke har noen forkunnskapskrav.
  • containsImpossibleCourse(Collection courses) - Returnerer hvorvidt samlingen av emner inneholder et emne som er umulig. Et umulig emne er definert som et emne X som har et forkunnskaps krav, som har emnet X som forkunnskapskrav.

    Expand
    titleKodeskjelett del 8


    Code Block
    languagejava
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collection;
    import java.util.List;
    import java.util.function.Predicate;
    
    public class UniversityHandbookUtils {
    
    	/**
    	 * Get all courses that matches the given predicate
    	 * 
    	 * @param courses The list of courses to check for
    	 * @param p       The predicate that should be matched on
    	 *
    	 * @return A collection of courses that satisfy the predicate.
    	 */
    	public static Collection<Course> getCoursesWithPredicate(Collection<Course> courses, Predicate<Course> p) {
    		...
    	}
    
    	/**
    	 * Get all courses that does not have any prerequisites
    	 * 
    	 * @param courses The list of courses to check for
    	 * @return A collection of course without any prerequisites
    	 */
    	public static Collection<Course> getNonPrequisiteCourses(Collection<Course> courses) {
    		...
    
    	}
    	
    	/**
    	 * Returns whether the handbook contains an impossible course. A course is
    	 * deemed impossible if any of the prerequisite of the course has the current
    	 * course as a prerequisite. Only direct dependencies need to be checked. You
    	 * do not need to worry about transitive dependencies. That means if TDT4100 has
    	 * a dependency on TDT4110 and TDT4110 has a dependency on TDT4100 it is impossible.
    	 * 
    	 * A transitive dependency that does not need to be checked is if TDT4100 has a
    	 * dependency on TDT4110, TDT4110 has dependency on TDT4200 and TDT4200 has a
    	 * dependency on TDT4100.
    	 * 
    	 * @param courses The list of courses to check for
    	 * @return whether the courses contains an impossible course
    	 */
    	public static boolean containsImpossibleCourse(Collection<Course> courses) {
    		...
    	}
    }
    



    Expand
    titleLF


    Code Block
    languagejava
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collection;
    import java.util.List;
    import java.util.function.Predicate;
    import java.util.stream.Collectors;
    
    public class UniversityHandbookUtils {
    
    	/**
    	 * Get all courses that matches the given predicate
    	 * 
    	 * @param courses The list of courses to check for
    	 * @param p       The predicate that should be matched on
    	 *
    	 * @return A collection of courses that satisfy the predicate.
    	 */
    	public static Collection<Course> getCoursesWithPredicate(Collection<Course> courses, Predicate<Course> p) {
    		return courses.stream().filter(p).collect(Collectors.toList());
    
    	}
    
    	/**
    	 * Get all courses that does not have any prerequisites
    	 * 
    	 * @param courses The list of courses to check for
    	 * @return A collection of course without any prerequisites
    	 */
    	public static Collection<Course> getNonPrequisiteCourses(Collection<Course> courses) {
    		return courses.stream().filter(course -> course.getPrerequisites().size() == 0).collect(Collectors.toList());
    	}
    	
    	/**
    	 * Returns whether the handbook contains an impossible course. A course is
    	 * deemed impossible if any of the prerequisite of the course has the current
    	 * course as a prerequisite. Only direct dependencies need to be checked. You
    	 * do not need to worry about transitive dependencies. That means if TDT4100 has
    	 * a dependency on TDT4110 and TDT4110 has a dependency on TDT4100 it is impossible.
    	 * 
    	 * A transitive dependency that does not need to be checked is if TDT4100 has a
    	 * dependency on TDT4110, TDT4110 has dependency on TDT4200 and TDT4200 has a
    	 * dependency on TDT4100.
    	 * 
    	 * @param courses The list of courses to check for
    	 * @return whether the courses contains an impossible course
    	 */
    	public static boolean containsImpossibleCourse(Collection<Course> courses) {
    		return courses.stream().anyMatch(course -> course.getPrerequisites().stream().anyMatch(prerequesite -> prerequesite.getPrerequisites().contains(course)));
    
    	}
    }