public enum Distance { SPRINT (750, 20000, 5000), // Easy mode OLYMPIC (1500, 40
ID: 3538576 • Letter: P
Question
public enum Distance {
SPRINT(750, 20000, 5000), // Easy mode
OLYMPIC(1500, 40000, 10000), // Respectable
HALF_IRONMAN(1930, 90000, 21090), // Impressive
IRONMAN(3860, 180000, 42200); // Ouch
private int swimDistance;
private int cycleDistance;
private int runDistance;
private Distance(int swimDistance, int cycleDistance, int runDistance) {
this.swimDistance = swimDistance;
this.cycleDistance = cycleDistance;
this.runDistance = runDistance;
}
public int getDistance(Event event) {
int meters;
switch(event) {
case SWIMMING:
meters = this.swimDistance;
break;
case CYCLING:
meters = this.cycleDistance;
break;
case RUNNING:
meters = this.runDistance;
break;
default:
meters = 0;
System.err.println("Distance.getDistance() encountered an unknown Event type.");
};
return meters;
}
}
Explanation / Answer
typedef enum {
SPRINT,
OLYMPIC,
HALF_IRONMAN,
IRONMAN
} Distance;
typedef struct {
int swimDistance;
int cycleDistance;
int runDistance;
} Distance_Fields;
static Distance_Fields Distance_Data[4] = {
{ 750, 20000, 5000}
, { 1500, 40000, 10000}
, { 1930, 90000, 21090}
, { 3860, 180000, 42200}
};
Distance DistanceValueOf(NSString *text);
Distance DistanceDescription(Distance value);
int Distance_getDistance(id<Distance> e, Event * event);
Distance DistanceValueOf(NSString *text) {
if (text) {
if ([text isEqualToString:@"SPRINT"])
return SPRINT;
else if ([text isEqualToString:@"OLYMPIC"])
return OLYMPIC;
else if ([text isEqualToString:@"HALF_IRONMAN"])
return HALF_IRONMAN;
else if ([text isEqualToString:@"IRONMAN"])
return IRONMAN;
}
return -1;
}
Distance DistanceDescription(Distance value) {
switch (value) {
case SPRINT:
return @"SPRINT";
else case OLYMPIC:
return @"OLYMPIC";
else case HALF_IRONMAN:
return @"HALF_IRONMAN";
else case IRONMAN:
return @"IRONMAN";
}
return nil;
}
id Distance_init(id<Distance> e, int swimDistance, int cycleDistance, int runDistance) {
if (self = [super init]) {
swimDistance = swimDistance;
cycleDistance = cycleDistance;
runDistance = runDistance;
}
return self;
}
int Distance_getDistance(id<Distance> e, Event * event) {
int meters;
switch (event) {
case SWIMMING:
meters = swimDistance;
break;
case CYCLING:
meters = cycleDistance;
break;
case RUNNING:
meters = runDistance;
break;
default:
meters = 0;
[System.err println:@"Distance.getDistance() encountered an unknown Event type."];
}
return meters;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.