Head first C # Chinese Version chapter 12th review and foresight page529

Source: Internet
Author: User

 

 

Compile hive
The code is your task.



① Compile hive
Framework Code of the class





Just like writing flower
Similar to the class, you should first
Hive
Class to write a skeleton code. The class diagram is shown in the right figure. Set
Honey
Set to read-only automatic attribute,
Locations
If it is private,
Beecount
It is only used internally, so it can also be a private field.



② Define hive
Constant



Initial number of bees (6
) Requires a constant, the amount of honey at the beginning (
3.2
), The maximum number of honey that can be carried by the honeycomb (
15
), Pollen-to-Honey conversion rate (
0.25
), The maximum number of bees (
8
), The minimum number of honey that can give birth to young bees (
4
).



You need to give these constants A good name and a suitable type. For types, do not just focus on their initial values, but also pay attention to the values of these constants. Double
Best and
Double
Used together,
Int
Best and
Int
.



③ Write code to operate the location


First, write getlocation
() Method. It must accept
String
, In
Locations
Find and return the relevant location in the dictionary. If it cannot be found,
Argumentexception
.



Then, write initializelocation
() Method. This method sets the following locations in the beehive:



◆ Portal (600
,
100
)

◆ Place of conservation (95
,
174
)

◆ Honey Factory (157
,
98
)

◆ Exit (194
,
213
)



Each of these maps to the 2D occupied by the honeycomb.
A point in the space. Later, make sure that the simulator overwrites all these points. In this simulator, we assume that there is only one honeycomb, and the points are fixed. If you want to create multiple honeycomb, you may want to set the point to the coordinates relative to the honeycomb instead of the whole world.



④ Create hive
Constructor



When a beehive is initialized, the honey volume is set to the initial value. You need to set all locations in the beehive and create a random
. Then, call
Addbee
() Method
--
Each time a young bee is created, the newly created
Random
Instance.



Addbee () requires
Random
Object because it adds a random vertex to the nursery
--
In this way, young bees will not overlap.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.