Photo AI
Question 3
Delivery trip system You are to complete the code as specified in QUESTION 3.1 and QUESTION 3.2. 3.1 Trip details 3.1.1 Function compileTripNum: - Repeat Genera... show full transcript
Step 1
Answer
To implement the compileTripNum
function, we need to generate a random four or five-digit number, ensuring the last digit is not zero. This can be achieved using the following pseudo-code:
function compileTripNum: Integer;
var
sTripNum: String;
begin
repeat
sTripNum := IntToStr(Random(10)) +
IntToStr(Random(10)) +
IntToStr(Random(9) + 1);
until (sTripNum[Length(sTripNum)] <> '0');
Result := StrToInt(sTripNum);
end;
Step 2
Answer
The constructor should accept parameters for departure city, destination city, and load. The implementation can be:
constructor TDeliveryTrip.Create(aDepartureCity, aDestinationCity: String; aLoad: Integer);
begin
fDeparture := aDepartureCity;
fDestination := aDestinationCity;
fLoad := aLoad;
fTripNumber := compileTripNum;
fDistance := 0;
end;
Step 3
Step 4
Answer
To set the distance, the procedure should read from a file and extract city information. Here is a sample implementation:
procedure TDeliveryTrip.setDistance;
var
line: String;
begin
AssignFile(InputFile, 'distance.txt');
Reset(InputFile);
while not EOF(InputFile) do
begin
ReadLn(InputFile, line);
// Process the line to set distance
end;
CloseFile(InputFile);
end;
Step 5
Answer
This function determines the type of truck based on the load. Here is how this can be implemented:
function TDeliveryTrip.determineTruckType: String;
begin
if fLoad = 0 then
Result := 'No distance'
else if fLoad <= 1000 then
Result := 'Light truck'
else if fLoad <= 5000 then
Result := 'Medium truck'
else
Result := 'Heavy truck';
end;
Step 6
Answer
For the button that creates a trip, extract relevant values and instantiate the object:
procedure TForm1.btnCreateTripClick(Sender: TObject);
var
objTrip: TDeliveryTrip;
begin
objTrip := TDeliveryTrip.Create(edtDepartureCity.Text, edtDestinationCity.Text, StrToInt(edtLoad.Text));
// Display the object details
MemoOutput.Text := objTrip.ToString;
end;
Step 7
Answer
This button reads the distance related to the trip:
procedure TForm1.btnSetDistanceClick(Sender: TObject);
var
departureCity, destinationCity: String;
begin
// Implementation to read cities and set distance
// Example output for distance
MemoOutput.Text := Format('Distance between %s and %s is ...', [departureCity, destinationCity]);
end;
Step 8
Report Improved Results
Recommend to friends
Students Supported
Questions answered