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;
}
}