Photo AI
Question 3
Complete the code as specified in QUESTION 3.1 and QUESTION 3.2. 3.1.1 Write code for a constructor method that initializes a Delivery trip object. The method shoul... show full transcript
Step 1
Answer
To create a constructor method for the Delivery trip object, we define a class with the parameters for departure city, destination city, and load. The constructor will initialize these attributes.
Example:
constructor TDeliveryTrip.create(aDepartureCity, aDestinationCity: string; aLoad: Integer);
begin
fDepartureCity := aDepartureCity;
fDestinationCity := aDestinationCity;
fLoad := aLoad;
fTripNumber := compileTripNum;
fDistance := 0;
end;
Step 2
Answer
This method is part of the constructor created in 3.1.1, and initializes the corresponding attributes for departure city, destination city, and load. It's important to ensure that the parameters are of the correct type and assigned correctly.
constructor TDeliveryTrip.create(aDepartureCity, aDestinationCity: string; aLoad: Integer);
begin
// Initialize attributes
fDepartureCity := aDepartureCity;
fDestinationCity := aDestinationCity;
fLoad := aLoad;
end;
Step 3
Answer
The accessor method getDistance retrieves the distance attribute of the Delivery trip object. It will return the value of the fDistance attribute.
Example:
function TDeliveryTrip.getDistance: Integer;
begin
Result := fDistance;
end;
Step 4
Answer
SetDistance allows the modification of the distance parameter for the Delivery trip object. It takes a parameter which will set the fDistance value.
Example:
procedure TDeliveryTrip.setDistance(aDistance: Integer);
begin
fDistance := aDistance;
end;
Step 5
Answer
This method will return a string representing the type of truck based on the load. It uses conditional statements to check the load weight and assigns the appropriate truck type.
Example:
function TDeliveryTrip.determineTruckType: string;
begin
if fLoad <= 1000 then
Result := 'Light Truck'
else if fLoad <= 5000 then
Result := 'Medium Truck'
else
Result := 'Heavy Truck';
end;
Report Improved Results
Recommend to friends
Students Supported
Questions answered