- Created by Unknown User (mattiamn), last modified on 26.01.2022
You are viewing an old version of this page. View the current version.
Compare with Current View Page History
« Previous Version 7 Next »
Her ligger løsningsforslaget til eksamen 2021. I kombinasjon med kildekoden vil vi beskrive hva oppgaven spurt etter, samt ofte forekommende feil og misforståelser.
På gitlab finner dere full kode til alle deler i oppgaven.
Oppgaven handler om å holde oversikt over et pågaende vaksinestudie.
public class VaccineTrialVolunteer { public VaccineTrialVolunteer(String id, boolean placebo) { ... } public String getId() { ... } /* Whether the volunteer was given a placebo or the actual vaccine */ public boolean isPlacebo() { ... } /* Whether the volunteer got sick during the trial period, * the default value for this should be false */ public boolean gotSick() { ... } /* * Updates whether the participant got sick during the trial period */ public void setGotSick(boolean gotSick) { ... } } public class VaccineTrial { // Add any needed fields here /** * Adds a new VaccineTrialVolunteer to the trial * * @param id The id of the volunteer * * @param placebo Whether the volunteer was given a placebo, or the actual * vaccine */ public void addVolunteer(String id, boolean placebo) { // TODO } /** * Returns whether the vaccine's effectiveness rate is higher than the provided * limit. The effectiveness of the vaccine is calculated as follows: * * 1- (number of people that received the vaccine and got sick/ * number of people that got sick) * * If there is no sick people, the vaccine is not effective * * @param limit A limit to compare against * * @throws IllegalArgumentException If limit is not between (including) 0 and 1. * * @return Whether the vaccine effectiveness rate is higher than the limit */ public boolean isMoreEffectiveThanLimit(double limit) { ... } /** * Updates the sick state of a VaccineTrialVolunteer * * @param id The id of the volunteer to set sick. * @throws IllegalArgumentException if there is no volunteer with the given id */ public void setSick(String id) { ... } /** * Get's the volunteer with the given ID * * @param id The id of the volunteer to set sick. * * @return The vaccine trial volunteer with the given ID. If the ID is not valid * for any volunteer, return null */ public VaccineTrialVolunteer getVolunteer(String id) { ... } }
Oppgave a) Utfyll klassen VaccineTrialVolunteer og alle metodene i denne klassen. Legg til nødvendige felt. VaccineTrialVolunteer skal ha følgende metoder:
- VaccineTrialVolunteer(String id, boolean placebo) - Oppretter en ny frivillig deltaker, med den gitte ID-en og hvorvidt deltakeren fikk placebo eller faktiske vaksinen.
- getId() - Henter ut ID-en til deltakeren.
- isPlacebo() - Returnere hvorvidt deltakeren fikk placebo eller vaksine.
- gotSick() - Returnere hvorvidt deltakeren ble syk i løpet av studien.
- setGotSick(boolean gotSick) - Oppdaterer om deltakeren ble syk i løpet av studien.
public class VaccineTrialVolunteer { private final String id; private final boolean placebo; private boolean gotSick; public VaccineTrialVolunteer(String id, boolean placebo) { this.id = id; this.placebo = placebo; } public String getId() { return this.id; } /* Whether the volunteer was given a placebo or the actual vaccine */ public boolean isPlacebo() { return this.placebo; } /* Whether the volunteer got sick during the trial period, * the default value for this should be false */ public boolean gotSick() { return this.gotSick; } /* * Updates whether the participant got sick during the trial period */ public void setGotSick(boolean gotSick) { this.gotSick = gotSick; } }
Oppgave b)
Fyll ut klassen VaccineTrial, og alle metodene i denne klassen. Legg til egne nødvendige felt. VaccineTrial skal ha følgende metoder:
- addVolunteer(String id, boolean placebo) - Legger til en deltaker i studien.
- isMoreEffectiveThanLimit(double limit) - Hvorvidt vaksinen var mer effektiv enn den oppgitte grensen.
- setSick(String id) - Oppdaterer deltakeren med den gitte ID-en til å ha blitt syk i løpet av studien.
- VaccineTrialVolunteer getVolunteer(String id) - Returnerer deltakeren med den gitte ID-en.
import java.util.ArrayList; import java.util.Collection; public class VaccineTrial { // Add any needed fields here private Collection<VaccineTrialVolunteer> volunteers = new ArrayList<>(); /** * Adds a new VaccineTrialVolunteer to the trial * * @param id The id of the volunteer * * @param placebo Whether the volunteer was given a placebo, or the actual * vaccine */ public void addVolunteer(String id, boolean placebo) { this.volunteers.add(new VaccineTrialVolunteer(id, placebo)); } /** * Returns whether the vaccine's effectiveness rate is higher than the provided * limit. The effectiveness of the vaccine is calculated as follows: * * 1- (number of people that received the vaccine and got sick/ * number of people that got sick) * * If there is no sick people, the vaccine is not effective * * @param limit A limit to compare against * * @throws IllegalArgumentException If limit is not between (including) 0 and 1. * * @return Whether the vaccine effectiveness rate is higher than the limit */ public boolean isMoreEffectiveThanLimit(double limit) { if (limit < 0 || limit > 1) { throw new IllegalArgumentException("Invalid limit"); } double numberOfSick = 0; double numberOfSickWithVaccine = 0; for (VaccineTrialVolunteer volunteer: volunteers) { if (volunteer.gotSick()) { numberOfSick++; if (!volunteer.isPlacebo()) { numberOfSickWithVaccine++; } } } double ratio = 1.0-(numberOfSickWithVaccine/numberOfSick); return ratio > limit; } /** * Updates the sick state of a VaccineTrialVolunteer * * @param id The id of the volunteer to set sick. * @throws IllegalArgumentException if there is no volunteer with the given id */ public void setSick(String id) { VaccineTrialVolunteer volunteer = this.getVolunteer(id); if (volunteer == null) { throw new IllegalArgumentException("invalid ID"); } volunteer.setGotSick(true); } /** * Get's the volunteer with the given ID * * @param id The id of the volunteer to set sick. * * @return The vaccine trial volunteer with the given ID. If the ID is not valid * for any volunteer, return null */ public VaccineTrialVolunteer getVolunteer(String id) { for (VaccineTrialVolunteer volunteer: volunteers) { if (volunteer.getId().equals(id)) { return volunteer; } } return null; } }
- No labels