Her ligger løsningsforslaget til eksamen kontinuasjonseksamen 2021. Vi vil senere legge til ofte forekommende feil og misforståelser i kombinasjon med kildekoden .
...
Expand |
---|
title | Del 7 - Objektstrukturer og java-teknikker (10 %) |
---|
|
Implementer metodene i Realtor - Realtor(double comission) oppretter et Realtor objekt med en gitt provisjon
- setCommission(double comission) Oppdaterer provisjonen til megleren
- addProperty(Property property) Legger til en eiendom i porteføljen til megleren
- calculateTotalCommission() - Regner ut total provisjonslønn basert på alle solgte boliger megleren har
- iterator() - Returnerer en iterator for å iterere gjennom alle eiendommene til megleren
Expand |
---|
|
Code Block |
---|
| package del5_8;
import java.util.Iterator;
public class Realtor implements Iterable<Property> {
/**
* Creates a Realtor object
*
* @param name the name of the realtor
* @param commission the commission the realtor takes for a sale
*/
public Realtor(String name, double commission) {
// TODO
}
/**
*
* @return the name of the realtor
*/
public String getName() {
// TODO
return null;
}
/**
*
* @param commission the new commission of the realtor
*
* @throws IllegalArgumentException if the commission not between (excluding) 0
* and (including) 100.
*/
public void setCommission(double commission) {
// TODO
}
/**
* Adds a property to the realtor's sale collection
*
* @param property a property
*/
public void addProperty(Property property) {
// TODO
}
/**
* The total commission is calculated as the sum of the highest bid of each sold
* property times the commission rate. The commission rate is calculated based
* on the realtor's current commission rate and does not need to consider
* historical commission rates
*
* A realtor with commission of 10 %, and two sold properties sold at 1000 each,
* would have a total commission value of 200
*
* @return the calculated commission of the realtor
*/
public double calculateTotalCommission() {
// TODO
return 0;
}
@Override
public Iterator<Property> iterator() {
// TODO Auto-generated method stub
return null;
}
/**
*
* @return an iterator to be able to iterate through all the properties of this
* realtor
*/
public Iterator<Property> iterable() {
return null;
}
public static void main(String[] args) {
Realtor realtor = new Realtor("test", 10);
// The following will only work if BusinessProperty and Property has the correct
// implementation
Property p = new Property("name", 1500);
p.bidReceived("BIDDER", 2000);
p.setIsSold();
realtor.addProperty(p);
// Should be 200
System.out.println(realtor.calculateTotalCommission());
}
}
|
|
Expand |
---|
|
Code Block |
---|
| package del5_8;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Realtor implements Iterable<Property>{
private List<Property> properties = new ArrayList<>();
private double commission;
private String name;
/**
* Creates a Realtor object
*
* @param name the name of the realtor
* @param comission the commission the realtor takes for a sale
*/
public Realtor(String name, double commission) {
this.setCommission(commission);
this.name = name;
}
public String getName() {
return this.name;
}
/**
*
* @param comission the new comission of the realtor
*
* @throws IllegalArgumentException if the commission not between (excluding) 0
* and (including) 100.
*/
public void setCommission(double commission) {
if (commission <= 0 || commission > 100) {
throw new IllegalArgumentException("Comission must be between 0 and 100");
}
this.commission = commission;
}
/**
* Adds a property to the realtor's sale collection
*
* @param property a property
*/
public void addProperty(Property property) {
this.properties.add(property);
}
/**
* The total comission is calculated as the sum of the highest bid of each sold
* property times the commission rate The comission rate is calculated based on
* the realtor's current commission rate and does not need to consider
* historical commission rates
*
* @return the calculated commission of the realtor
*/
public double calculateTotalCommission() {
return this.properties.stream().filter(p -> p.isSold()).mapToDouble(Property::getHighestBid)
.map(price -> price * (commission) / 100.0).sum();
}
@Override
public Iterator<Property> iterator() {
return this.properties.iterator();
}
public String toString() {
return this.name;
}
public static void main(String[] args) {
Realtor realtor = new Realtor("test", 10);
// Will only work if BusinessProperty and Property has the correct
// implementation
Property p = new Property("name", 1500);
p.bidReceived("BIDDER", 2000);
p.setIsSold();
realtor.addProperty(p);
// Should be 200
System.out.println(realtor.calculateTotalCommission());
}
}
|
|
|
...
Expand |
---|
title | Del 8 - Interfaces og Comparator (5 %) |
---|
|
Implementer metoden i RealtorComparator |
...
Expand |
---|
title | Del 9 - Funksjonelle grensesnitt og lister (10 %) |
---|
|
Fyll ut UniversityHandbookUtils sine metoder for operasjoner på en liste med Course-objekter. - getCourseNames(List courses) - Returnerer en liste med navnene til alle emnene
- getCourseProperties(List courses, Function<Course, String> function) - Returnerer alle emner transformerte ved hjelp av funksjonen
- calculateGradesSummary(List courses, BinaryOperator operator) - Returnerer resultatet av å kjøre operatoren på alle karaktersnittene til emnene
- getCoursesYouCanTake(List courses, List takenCourses) - Returnerer alle emnene hvor du tilfredstiller alle forkunnskapskravene (takenCourses representerer her emnene du tidligere har tatt)
Expand |
---|
|
Code Block |
---|
| package del9;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class Course {
private String courseName;
private List<Course> prerequisites = new ArrayList<>();
private double averageGrade;
public Course(String courseName, double averageGrade) {
this(courseName);
this.averageGrade = averageGrade;
}
public Course(String courseName) {
this.courseName = courseName;
}
public String getCourseName() {
return courseName;
}
public void setAverageGrade(double averageGrade) {
this.averageGrade = averageGrade;
}
public double getAverageGrade() {
return averageGrade;
}
public void addPrequisite(Course course) {
this.prerequisites.add(course);
}
public Collection<Course> getPrerequisites() {
return new ArrayList<>(prerequisites);
}
public String toString() {
return this.courseName;
}
}
package del9;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.function.BinaryOperator;
import java.util.function.Function;
public class UniversityHandbookUtils {
/**
*
* @param courses List of course objects
* @return a list of course names
*/
public static Collection<String> getCourseNames(Collection<Course> courses) {
// TODO
return Arrays.asList();
}
/**
*
* @param courses List of course objects
* @return a list of a course objects transformed by the function
*/
public static Collection<String> getCourseProperties(Collection<Course> courses,
Function<Course, String> function) {
// TODO
return Arrays.asList();
}
/**
*
* @param courses a list of course objects
* @param operator a binary operator
* @return the result of applying the operator across all average grades
*/
public static double calculateGradesSummary(Collection<Course> courses, BinaryOperator<Double> operator) {
// TODO
return 0;
}
/**
*
* @param courses List of course objects
* @param takenCourses List of course objects
* @return a list of courses where takenCourses contains all prerequisites
* needed to enroll in the course
*/
public static Collection<Course> getCoursesYouCanTake(Collection<Course> courses, Collection<Course> takenCourses) {
// TODO
return Arrays.asList();
}
public static void main(String[] args) {
Course tdt4109 = new Course("TDT4109", 3.23);
Course tdt4100 = new Course("TDT4100", 3.23);
Course tdt4120 = new Course("TDT4120", 3.23);
Course tdt1337 = new Course("TDT1337", 3.23);
Course tdt3713 = new Course("TDT3713", 3.23);
tdt4100.addPrequisite(tdt4109);
tdt4120.addPrequisite(tdt4109);
tdt4120.addPrequisite(tdt4100);
tdt1337.addPrequisite(tdt3713);
tdt3713.addPrequisite(tdt1337);
List<Course> courses = Arrays.asList(tdt4109, tdt4100, tdt4120, tdt1337, tdt3713);
// These two lines should print the same list of course names
System.out.println(getCourseNames(courses));
System.out.println(getCourseProperties(courses, c -> c.getCourseName()));
// Should print 16.15
System.out.println(calculateGradesSummary(courses, (prevGrade, currentGrade) -> prevGrade + currentGrade));
// Should print tdt4109, tdt4100 (order does not matter)
System.out.println(getCoursesYouCanTake(courses, Arrays.asList(tdt4109)));
}
}
|
|
Expand |
---|
|
Code Block |
---|
| package del9;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.stream.Collectors;
public class UniversityHandbookUtils {
/**
*
* @param courses List of course objects
* @return a list of course names
*/
public static List<String> getCourseNames(Collection<Course> courses) {
return courses.stream().map(Course::getCourseName).collect(Collectors.toList());
}
/**
*
* @param courses List of course objects
* @return a list of a course objects transformed by the function
*/
public static Collection<String> getCourseProperties(Collection<Course> courses,
Function<Course, String> function) {
return courses.stream().map(function).collect(Collectors.toList());
}
/**
*
* @param courses a list of course objects
* @param operator a binary operator
* @return a summary of all the grades of the courses by applying the binary
* operator
*/
public static double calculateGradesSummary(Collection<Course> courses, BinaryOperator<Double> operator) {
return courses.stream().map(Course::getAverageGrade).reduce(operator).get();
}
/**
*
* @param courses List of course objects
* @param takenCourses List of course objects
* @return a list of courses where takenCourses contains all prerequisites
* needed to enroll in the course
*/
public static Collection<Course> getCoursesYouCanTake(Collection<Course> courses, Collection<Course> takenCourses) {
return courses.stream().filter(course -> takenCourses.containsAll(course.getPrerequisites()))
.collect(Collectors.toList());
}
public static void main(String[] args) {
Course tdt4109 = new Course("TDT4109", 3.23);
Course tdt4100 = new Course("TDT4100", 3.23);
Course tdt4120 = new Course("TDT4120", 3.23);
Course tdt1337 = new Course("TDT1337", 3.23);
Course tdt3713 = new Course("TDT3713", 3.23);
tdt4100.addPrequisite(tdt4109);
tdt4120.addPrequisite(tdt4109);
tdt4120.addPrequisite(tdt4100);
tdt1337.addPrequisite(tdt3713);
tdt3713.addPrequisite(tdt1337);
List<Course> courses = Arrays.asList(tdt4109, tdt4100, tdt4120, tdt1337, tdt3713);
// These two lines should print the same list of course names
System.out.println(getCourseNames(courses));
System.out.println(getCourseProperties(courses, c -> c.getCourseName()));
// Should print 16.15
System.out.println(calculateGradesSummary(courses, (prevGrade, currentGrade) -> prevGrade + currentGrade));
// Should print tdt4109, tdt4100 (order does not matter)
System.out.println(getCoursesYouCanTake(courses, Arrays.asList(tdt4109)));
}
}
|
|
|
...