/* * BaseParticipant is an abstract (i.e., partial) implementation of the Partic
ID: 3538606 • Letter: #
Question
/*
* BaseParticipant is an abstract (i.e., partial) implementation of the Participant interface, providing common
* functionality to concrete implementations to reduce code duplication. This class uses the default access modifier
* (which is the case when public/protected/private is omitted), and cannot be accessed by classes outside of its
* package. The closest thing to this in Objective-C is to create put the classes Header and Source files in a sub-
* folder named to indicate it is not for general use (such as Abstract, Base, Internal, et cetera).
*/
/*default*/ abstract class BaseParticipant implements Participant {
private String name;
private Event preferredEvent;
private int raceTime;
private boolean couldNotFinish;
protected BaseParticipant(String name, Event preferredEvent) {
this.name = name;
this.preferredEvent = preferredEvent;
this.raceTime = 0;
this.couldNotFinish = false;
}
public String getName() {
return this.name;
}
public Event getPreferredEvent() {
return this.preferredEvent;
}
public boolean isDisqualified() {
return this.couldNotFinish;
}
public void addTime(int additionalRaceTime) {
this.raceTime += additionalRaceTime;
}
public void setCouldNotFinish() {
this.couldNotFinish = true;
}
public int getTime() throws CouldNotFinishException {
if(this.couldNotFinish) {
throw new CouldNotFinishException();
}
return this.raceTime;
}
protected int metersPerMinute(Event event) throws CouldNotFinishException {
if(this.couldNotFinish || (Math.random() < 0.1 && event != preferredEvent)) {
throw new CouldNotFinishException();
}
/*
* The values below are averages for Half Ironman Triathlons. A more robust simulation would take the
* distance into account when calculation the pacing.
*/
int metersPerMinute;
switch(event) {
case SWIMMING:
metersPerMinute = 43;
break;
case CYCLING:
metersPerMinute = 500;
break;
case RUNNING:
metersPerMinute = 157;
break;
default:
throw new CouldNotFinishException();
}
return metersPerMinute;
}
}
Explanation / Answer
//header file
//save as BaseParticipant.h
@interface BaseParticipant : NSObject <Participant> {
NSString * name;
Event * preferredEvent;
int raceTime;
BOOL couldNotFinish;
}
@property(nonatomic, retain, readonly) NSString * name;
@property(nonatomic, retain, readonly) Event * preferredEvent;
@property(nonatomic, readonly) BOOL disqualified;
@property(nonatomic, readonly) int time;
- (id) init:(NSString *)name preferredEvent:(Event *)preferredEvent;
- (void) addTime:(int)additionalRaceTime;
- (void) setCouldNotFinish;
- (int) metersPerMinute:(Event *)event;
@end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.