Complete set: Intro to C - Bill Buchanan
Complete set: Intro to C - Bill Buchanan
Complete set: Intro to C - Bill Buchanan
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
5.15 Methods Tu<strong>to</strong>rial<br />
We return <strong>to</strong> the Megalomania solution for this tu<strong>to</strong>rial. We will be creating methods<br />
<strong>to</strong> process the Country class. In each case we should create methods in the<br />
Country.cs file and call them from the interface.<br />
13) Create routines <strong>to</strong> increment the population as in the lecture notes.<br />
a) Create a but<strong>to</strong>n that increments the population of country index 5. Implement<br />
IncPop so that the following code can be attached <strong>to</strong> a new but<strong>to</strong>n on the<br />
form:<br />
Country target = ((Country)world[5]);<br />
target.IncPop(1000000);<br />
You should notice the population of Angola go from about 10 million <strong>to</strong><br />
about 11 million.<br />
b) Create the method Annex – this country adds in all of the population, area and<br />
gdp of the victim country. The victim country still exists but with an area,<br />
population and gdp of zero. Attach the following code <strong>to</strong> a new but<strong>to</strong>n and<br />
observe what happens <strong>to</strong> Jersey and the Isle of Man.<br />
private void but<strong>to</strong>n2_Click(object sender, System.EventArgs e)<br />
{<br />
Country uk = (Country) world[248];<br />
Country jersey = (Country)world[121];<br />
Country iom = (Country)world[150];<br />
uk.Annex(jersey);<br />
uk.Annex(iom);<br />
}<br />
duction <strong>to</strong> .NET<br />
<strong>Intro</strong><br />
c) Create the + opera<strong>to</strong>r (See chapter 6 of Jesse Liberty). This is opera<strong>to</strong>r creates a<br />
new country with the combined population, gdp and area of the two participants.<br />
The name of the new country should be <strong>set</strong> <strong>to</strong> “New Country”. The<br />
region should be <strong>set</strong> – if the two countries are in different regions an exception<br />
should be raised. Create a but<strong>to</strong>n <strong>to</strong> unite North and South Korea with<br />
the following code:<br />
private void but<strong>to</strong>n3_Click(object sender, System.EventArgs e)<br />
{<br />
Country northKorea = (Country)world[129];<br />
Country southKorea = (Country)world[130];<br />
world.Add(northKorea+southKorea);<br />
}<br />
d) You should find the new country at the bot<strong>to</strong>m of the list.<br />
Agilent .NET Course: Module 5, page<br />
15