954246编程设计调试、讲解Programming程序、C++编程语言辅导
解析R语言编程|辅导Web开发
Practice 954246 01/2563 Lab 12 Object-Oriented Programming 2
1/5
Lab 12 Object-Oriented Programming 2
Note: The underlined text is an input data.
1. Create class Bus stored in file Bus.php that extends class Car from full example of lecture.
Class Bus can load the passengers intoit byusing method load($number_of_passenger)
and unload the passengers by using method unload($number_of_passenger) then the
fuel used can be calculated per passenger run from the following formula. Moreover, each
Bus has its own capacity (maximum number of passengers) setting by constructor.
𝑓𝑢𝑒𝑙 𝑢𝑠𝑒𝑑 𝑝𝑒𝑟 𝑝𝑎𝑠𝑠𝑒𝑛𝑔𝑒𝑟 𝑟𝑢𝑛 (𝐿) =
𝑝𝑎𝑠𝑠𝑒𝑛𝑔𝑒𝑟 𝑑𝑖𝑠𝑡𝑎𝑛𝑐𝑒 (𝑘𝑚)
120
×
𝑝𝑖𝑠𝑡𝑜𝑛 𝑣𝑜𝑙𝑢𝑚𝑒 (𝑐𝑐)
1000
+
70 × 𝑛𝑢𝑚𝑏𝑒𝑟 𝑜𝑓 𝑝𝑎𝑠𝑠𝑒𝑛𝑔𝑒𝑟𝑠 × 𝑝𝑎𝑠𝑠𝑒𝑛𝑔𝑒𝑟 𝑑𝑖𝑠𝑡𝑎𝑛𝑐𝑒 (𝑘𝑚)
10000
Remark: Beware, the number of passengers can be changed over time so you must have
property to keep fuel used, calculating fuel used on showing is not correct.
The method showLongInfo() will print out the following format, same as Car but extends
with number of current passenger.
Owner: owner
Running distance: dist km
Fuel used: fuel L
Current passenger: number_of_passengers
Then write the program that waits for inputting owner name,piston volume and capacity then
wait for the following command.
0 stop engine
1 start engine
r run for the given km
+ load the given number of passengers into bus
- unload the given number of passengers out of bus
i show information (engine is off only)
e exit
h print this help
Practice 954246 01/2563 Lab 12 Object-Oriented Programming 2
2/5
If the number of passengers greater than capacity it will print out the following message to
STDERR then return false, in method load().
Number of passengers greater than capacity!!!
If the number of passengers lessthan 0 it will print out the following message to STDERR then
return false, in method unload().
Number of passengers less than 0!!!
Example 01: php ass-01.php
Input (owner cc capacity): Stieve 30000 40
command (h for help): h
0 stop engine
1 start engine
r run for the given km
+ load the given number of passengers into bus
- unload the given number of passengers out of bus
i show information (engine is off only)
e exit
h print this help
command (h for help): r 50
Cannot run, engine is off!!!
command (h for help): 1
command (h for help): r 200
command (h for help): - 20
Number of passengers less than 0!!!
command (h for help): + 30
command (h for help): r 1000
command (h for help): + 20
Number of passengers greater than capacity!!!
command (h for help): r 500
command (h for help): - 10
command (h for help): r 500
command (h for help): + 20
command (h for help): r 1000
command (h for help): - 30
command (h for help): i
Cannot show, engine is on!!!
command (h for help): 0
command (h for help): i
Owner: Stieve
Running distance: 3,200 km
Fuel used: 1,465.00 L
Current passengers: 10
command (h for help): e
Practice 954246 01/2563 Lab 12 Object-Oriented Programming 2
3/5
2. Create class Pet stored in file Pet.php that implements interface Runnable and ShowInfo
and class PetLover stored in file PetLover.php that extends class Person and implements
interface ShowInfo from full example of lecture.
Class Pet consist of the following members.
• $name: assigned by constructor.
• $distance: accumulateddistance from method runFor().
• getName():get the pet name.
• showInfo(): print out the following format.
Name: pet_name
• showLongInfo(): print out the following format.
Name: pet_name
Running distance: dist km
Class PetLover consist of the following members (extends from parentclass).
• takePet(Pet $pet): take an instance of Pet with his/her.
• releasePet(Pet $pet): will releasethe given instance of Pet from takenpets.
• showInfo(): print out the following format, same as .
Name: pet_lover_name
• showLongInfo(): print out the following format.
Name: pet_lover_name
Running distance: dist km
Current taken pets: pet1_name, pet2_name, ...
Remark: PetLover can take more than one pet on the same time.
If PetLover do runFor() all taken pets will runFor() together so you must
override method runFor() in class PetLover.
All pets always have the difference name.
You can use === to check for the same object.
Practice 954246 01/2563 Lab 12 Object-Oriented Programming 2
4/5
Write the program that reads the input file with the followingformat.
pet_lover_name
number_of_pets
pet1_name
pet2_name
...
number_of_command
command1 value1
command2 value2
...
The commands consist of:
t take the given pet name
re release the given pat name
r pet lover run for the given km
Example Input: ass-02-input.txt
Beware that command can take pet that already taken or can release non-taken pet and those
command is no effect. After all commands have been executed then show information from
showLongInfo() for pet lover and all pets.
- QQ:99515681
- 邮箱:99515681@qq.com
- 工作时间:8:00-21:00
- 微信:codinghelp
联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!