import com.cis399.assignment1.model.Distance; import com.cis399.assignment1.mode
ID: 3538605 • Letter: I
Question
import com.cis399.assignment1.model.Distance;
import com.cis399.assignment1.model.Event;
import com.cis399.assignment1.model.Participant;
import com.cis399.assignment1.model.implementation.CyclingParticipant;
import com.cis399.assignment1.model.implementation.RunningParticipant;
import com.cis399.assignment1.model.implementation.SwimmingParticipant;
public class Driver {
public static void main(String[] args) {
/*
* A Map will be used to hold the rosters for the three events in the Triathlon. The equivalent class
* in the Foundation API that you can use in Objective-C is the NSMutableDictionary. Note that in order
* to modify the Dictionary after construction we must use an NSMutableDictionary instance, because the
* NSDictionary class is immutable (similar to String in Java).
*/
Map<Event, List<Participant>> mainRoster = new HashMap<Event, List<Participant>>();
mainRoster.put(Event.SWIMMING, new ArrayList<Participant>());
mainRoster.put(Event.CYCLING, new ArrayList<Participant>());
mainRoster.put(Event.RUNNING, new ArrayList<Participant>());
/*
* Interface are first class types in Java, so they can be used on their own to specify a type (and
* consequently they share a namespace with Classes). The Objective-C equivalent, the Protocol, is not
* a first class type, and has to be specified in addition to a valid class type; to say that a type is
* can be any class of a particular protocol we must use the keyword id for the class type, which is
* essentially a wildcard class type in Objective-C. The syntax would look something like this:
* id <SomeProtocolName> variableName;
*/
addParticipantToRoster(mainRoster, new CyclingParticipant("Charles"));
addParticipantToRoster(mainRoster, new SwimmingParticipant("Jason"));
addParticipantToRoster(mainRoster, new RunningParticipant("Kurt"));
Distance distanceToRace = Distance.HALF_IRONMAN;
for(Event event : Event.values()) {
for(Participant participant : mainRoster.get(event)) {
if(!participant.isDisqualified()) {
try {
System.out.println(participant.getName() + " is about to begin the " + event + " event.");
int finishTime = participant.performEvent(event, distanceToRace);
participant.addTime(finishTime);
System.out.println(participant.getName() + ", who prefers " + participant.getPreferredEvent() + ", finished the " + event + " event in " + finishTime + " minutes; Bringing the total race time to " + participant.getTime() + " minutes.");
} catch (CouldNotFinishException e) {
participant.setCouldNotFinish();
System.out.println(participant.getName() + ", who prefers " + participant.getPreferredEvent() + ", could not finish the " + event + " and was disqualified.");
}
}
}
}
Participant winner = null;
for(Participant participant : mainRoster.get(Event.RUNNING)) {
try {
if(!participant.isDisqualified() && (winner == null || winner.getTime() > participant.getTime())) {
winner = participant;
}
} catch (CouldNotFinishException e) {
// Intentionally Left Blank
}
}
if(winner != null) {
try {
System.out.println(" " + winner.getName() + ", who prefers " + winner.getPreferredEvent() + ", wins first place with a total time of " + winner.getTime() + " minutes!");
} catch (CouldNotFinishException e) {
winner = null;
}
}
if(winner == null) {
System.out.println(" No one finished the race!");
}
}
private static void addParticipantToRoster(Map<Event, List<Participant>> mainRoster, Participant participant) {
mainRoster.get(Event.SWIMMING).add(participant);
mainRoster.get(Event.CYCLING).add(participant);
mainRoster.get(Event.RUNNING).add(participant);
}
}
Explanation / Answer
//header file called 'Driver.h'
//assumes the other imports in the java file to be available as Obj-C header files as listed below
#import "Distance.h"
#import "Event.h"
#import "Participant.h"
#import "CyclingParticipant.h"
#import "RunningParticipant.h"
#import "SwimmingParticipant.h"
@interface Driver : NSObject {
}
+ (void) main:(NSArray *)args;
@end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.