Difference between revisions of "Ruby:Simple Printing Tutorial"

From Progzoo
Jump to: navigation, search
(Show the population in millions)
Line 3: Line 3:
 
==What are the regions?==
 
==What are the regions?==
 
<question className="Cia" title="What are the regions?" copyfile="bbc.txt">
 
<question className="Cia" title="What are the regions?" copyfile="bbc.txt">
<p> 
 
 
The method  <code>oneLine</code>  will get called over 200 times.
 
The method  <code>oneLine</code>  will get called over 200 times.
 
Each time the five parameters     
 
Each time the five parameters     
 
<pre> name region area pop gdp</pre> will have different values.
 
<pre> name region area pop gdp</pre> will have different values.
</p>
 
<p>
 
 
The example program lists the region for each country.
 
The example program lists the region for each country.
</p>
 
 
<p class="imper">
 
<p class="imper">
 
Change the program so that it prints the name instead.
 
Change the program so that it prints the name instead.
Line 18: Line 14:
 
   puts region
 
   puts region
 
end
 
end
</prog>
+
</prog><answer>
<answer>
+
 
def oneLine(name, region,area,pop,gdp)
 
def oneLine(name, region,area,pop,gdp)
 
   puts name
 
   puts name
 
end
 
end
</answer>
+
</answer></question>
</question>
+
  
 
==Show the name and the population==
 
==Show the name and the population==
 
<question className="Cia" title="Show the name and the population" copyFile="bbc.txt">
 
<question className="Cia" title="Show the name and the population" copyFile="bbc.txt">
<blurb>
 
 
This program prints the name and the area for each country. Notice that the <code>+"&#160;"+</code> puts a space between the two variables.
 
This program prints the name and the area for each country. Notice that the <code>+"&#160;"+</code> puts a space between the two variables.
 
<p class='imper'>Change the program so that it prints the name followed by the population instead of the area.</p>
 
<p class='imper'>Change the program so that it prints the name followed by the population instead of the area.</p>
Line 36: Line 29:
 
to convert the integer pop into a string.
 
to convert the integer pop into a string.
 
</hint>   
 
</hint>   
</blurb>
 
 
<prog>def oneLine(name, region,area,pop,gdp)
 
<prog>def oneLine(name, region,area,pop,gdp)
 
   puts region+" "+area.to_s
 
   puts region+" "+area.to_s
 
end
 
end
</prog>
+
</prog><answer>
<answer>
+
 
def oneLine(name, region,area,pop,gdp)
 
def oneLine(name, region,area,pop,gdp)
 
   puts name+" "+pop.to_s
 
   puts name+" "+pop.to_s
 
end
 
end
  </answer>
+
</answer></question>
</question>
+
  
 
==Show the population in millions==
 
==Show the population in millions==
Line 54: Line 44:
 
   puts region
 
   puts region
 
end
 
end
</prog>   
+
</prog>  <answer>
<answer>
+
 
def oneLine(name, region,area,pop,gdp)
 
def oneLine(name, region,area,pop,gdp)
 
   puts name+" "+(pop/1000000).to_s
 
   puts name+" "+(pop/1000000).to_s
end</answer>
+
end
</question>
+
</answer></question>

Revision as of 15:28, 15 April 2008

What are the regions?

The method oneLine will get called over 200 times. Each time the five parameters

 name region area pop gdp
will have different values.

The example program lists the region for each country.

Change the program so that it prints the name instead.

[Font] [Default] [Show] [Resize] [History] [Profile]

Show the name and the population

This program prints the name and the area for each country. Notice that the +" "+ puts a space between the two variables.

Change the program so that it prints the name followed by the population instead of the area.

[Font] [Default] [Show] [Resize] [History] [Profile]

Show the population in millions

Show the name and the population in millions. You must divide the population by one million.

[Font] [Default] [Show] [Resize] [History] [Profile]