Difference between revisions of "Accumulators For Searching Tutorial"

From Progzoo
Jump to: navigation, search
(The biggest population)
(Which middle eastern country has the smallest population?)
 
(11 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
{{Accumulator Tutorial}}
 
{{Accumulator Tutorial}}
==Average Name Length==
+
==The biggest population==
<question xmlns:htm="http://www.w3.org/1999/xhtml" lang="java" className="Cia" copyFile="cia.txt"><prog><![CDATA[
+
<question xmlns:htm="http://www.w3.org/1999/xhtml" lang="java" className="Cia" copyFile="cia.txt"><prog>
 
import java.io.BufferedReader;
 
import java.io.BufferedReader;
 
import java.io.FileReader;
 
import java.io.FileReader;
public class Cia{
+
public class Cia
 +
{
 
   public static void main(String[] argv)
 
   public static void main(String[] argv)
                         throws Exception{
+
                         throws Exception
 +
  {
 
     BufferedReader fh =
 
     BufferedReader fh =
 
     new BufferedReader(new FileReader("cia.txt"));
 
     new BufferedReader(new FileReader("cia.txt"));
 
     String line;
 
     String line;
     while ((line=fh.readLine())!=null){
+
     while ((line=fh.readLine())!=null)
 +
    {
 
       String [] wrds = line.split("\t");
 
       String [] wrds = line.split("\t");
 
       String  name  = wrds[0];
 
       String  name  = wrds[0];
Line 17: Line 20:
 
   }
 
   }
 
}
 
}
   ]]></prog><answer><![CDATA[
+
   </prog><answer>
 
import java.io.BufferedReader;
 
import java.io.BufferedReader;
 
import java.io.FileReader;
 
import java.io.FileReader;
public class Cia{
+
public class Cia
 +
{
 
   public static void main(String[] argv)
 
   public static void main(String[] argv)
                         throws Exception{
+
                         throws Exception
 +
  {
 
     BufferedReader fh =
 
     BufferedReader fh =
 
       new BufferedReader(new FileReader("cia.txt"));
 
       new BufferedReader(new FileReader("cia.txt"));
 
     String line;
 
     String line;
     int acc = 0;
+
     int biggest = 0;
    int count = 0;
+
     while (null!=(line=fh.readLine()))
     while (null!=(line=fh.readLine())){
+
    {
 
       String [] wrds = line.split("\t");
 
       String [] wrds = line.split("\t");
 
       String  name  = wrds[0];
 
       String  name  = wrds[0];
 
       int  pop = Integer.parseInt(wrds[3]);
 
       int  pop = Integer.parseInt(wrds[3]);
       count++;
+
       if (pop&gt;biggest)
       acc = name.length();
+
       {
 +
        biggest = pop;
 +
      }
 
     }
 
     }
     System.out.println(acc/count);
+
     System.out.println(biggest);
 
   }
 
   }
 
}
 
}
   ]]></answer><shell lang="java" className="Cia" import="shells.xml#null"/>
+
   </answer><shell lang="java" className="Cia" import="shells.xml#null"/>
 
<p class="imper">
 
<p class="imper">
What is the average length of Country name.
+
What is the population of the largest country (by population).
 
</p>
 
</p>
 
<hint tease="How to use an accumulator">
 
<hint tease="How to use an accumulator">
 
Recall that there are three steps:
 
Recall that there are three steps:
*Declare and initialise your accumulator <i>before</i> the loop
+
*Declare and initialise your accumulator <i>before</i> the loop starts.
starts.
 
 
  int acc = 0;
 
  int acc = 0;
 
*Update the accumulator inside the loop if appropriate.
 
*Update the accumulator inside the loop if appropriate.
if (pop&gt;acc)
+
  if (pop&gt;acc)
{
+
  {
    acc = pop;
+
    acc = pop;
}
+
  }
 
*Use the result - in this case print it, <i>after</i> the loop.
 
*Use the result - in this case print it, <i>after</i> the loop.
 
  System.out.println(acc);
 
  System.out.println(acc);
Line 58: Line 64:
  
 
==Which country has the biggest population?==
 
==Which country has the biggest population?==
<question xmlns:htm="http://www.w3.org/1999/xhtml" lang="java" className="Cia" copyFile="cia.txt"><prog><![CDATA[
+
<question xmlns:htm="http://www.w3.org/1999/xhtml" lang="java" className="Cia" copyFile="cia.txt"><prog>
 
import java.io.BufferedReader;
 
import java.io.BufferedReader;
 
import java.io.FileReader;
 
import java.io.FileReader;
public class Cia{
+
public class Cia
 +
{
 
   public static void main(String[] argv)
 
   public static void main(String[] argv)
                         throws Exception{
+
                         throws Exception
 +
  {
 
     BufferedReader fh =
 
     BufferedReader fh =
 
     new BufferedReader(new FileReader("cia.txt"));
 
     new BufferedReader(new FileReader("cia.txt"));
 
     String line;
 
     String line;
     while ((line=fh.readLine())!=null){
+
     while ((line=fh.readLine())!=null)
 +
    {
 
       String [] wrds = line.split("\t");
 
       String [] wrds = line.split("\t");
 
       String  name  = wrds[0];
 
       String  name  = wrds[0];
Line 74: Line 83:
 
   }
 
   }
 
}
 
}
   ]]></prog><answer><![CDATA[
+
   </prog><answer>
 
import java.io.BufferedReader;
 
import java.io.BufferedReader;
 
import java.io.FileReader;
 
import java.io.FileReader;
public class Cia{
+
public class Cia
 +
{
 
   public static void main(String[] argv)
 
   public static void main(String[] argv)
                         throws Exception{
+
                         throws Exception
 +
  {
 
     BufferedReader fh =
 
     BufferedReader fh =
 
       new BufferedReader(new FileReader("cia.txt"));
 
       new BufferedReader(new FileReader("cia.txt"));
Line 85: Line 96:
 
     int biggest = 0;
 
     int biggest = 0;
 
     String bigName = "unknown";
 
     String bigName = "unknown";
     while (null!=(line=fh.readLine())){
+
     while (null!=(line=fh.readLine()))
 +
    {
 
       String [] wrds = line.split("\t");
 
       String [] wrds = line.split("\t");
 
       String  name  = wrds[0];
 
       String  name  = wrds[0];
 
       int  pop = Integer.parseInt(wrds[3]);
 
       int  pop = Integer.parseInt(wrds[3]);
       if (pop>biggest)
+
       if (pop&gt;biggest)
 
       {
 
       {
 
         biggest = pop;
 
         biggest = pop;
Line 98: Line 110:
 
   }
 
   }
 
}
 
}
   ]]></answer><shell lang="java" className="Cia" import="shells.xml#null"/>
+
   </answer><shell lang="java" className="Cia" import="shells.xml#null"/>
 
<p class="imper">
 
<p class="imper">
 
What is the name of the largest country (by population).
 
What is the name of the largest country (by population).
Line 107: Line 119:
 
</hint>
 
</hint>
 
</question>
 
</question>
 +
 
==Which middle eastern country has the smallest population?==
 
==Which middle eastern country has the smallest population?==
<question xmlns:htm="http://www.w3.org/1999/xhtml" lang="java" className="Cia" copyFile="cia.txt"><prog><![CDATA[
+
<question xmlns:htm="http://www.w3.org/1999/xhtml" lang="java" className="Cia" copyFile="cia.txt"><prog>
 
import java.io.BufferedReader;
 
import java.io.BufferedReader;
 
import java.io.FileReader;
 
import java.io.FileReader;
public class Cia{
+
public class Cia
 +
{
 
   public static void main(String[] argv)
 
   public static void main(String[] argv)
                         throws Exception{
+
                         throws Exception
 +
  {
 
     BufferedReader fh =
 
     BufferedReader fh =
 
     new BufferedReader(new FileReader("cia.txt"));
 
     new BufferedReader(new FileReader("cia.txt"));
 
     String line;
 
     String line;
     while ((line=fh.readLine())!=null){
+
     while ((line=fh.readLine())!=null)
 +
    {
 
       String [] wrds = line.split("\t");
 
       String [] wrds = line.split("\t");
 
       String  name  = wrds[0];
 
       String  name  = wrds[0];
Line 125: Line 141:
 
   }
 
   }
 
}
 
}
   ]]></prog><answer><![CDATA[
+
   </prog><answer>
 
import java.io.BufferedReader;
 
import java.io.BufferedReader;
 
import java.io.FileReader;
 
import java.io.FileReader;
public class Cia{
+
public class Cia
 +
{
 
   public static void main(String[] argv)
 
   public static void main(String[] argv)
                         throws Exception{
+
                         throws Exception
 +
  {
 
     BufferedReader fh =
 
     BufferedReader fh =
 
       new BufferedReader(new FileReader("cia.txt"));
 
       new BufferedReader(new FileReader("cia.txt"));
Line 136: Line 154:
 
     int smallest = 1000000000;
 
     int smallest = 1000000000;
 
     String smallName = "unknown";
 
     String smallName = "unknown";
     while (null!=(line=fh.readLine())){
+
     while (null!=(line=fh.readLine()))
 +
    {
 
       String [] wrds = line.split("\t");
 
       String [] wrds = line.split("\t");
 
       String  name  = wrds[0];
 
       String  name  = wrds[0];
 
       String region = wrds[1];
 
       String region = wrds[1];
 
       int  pop = Integer.parseInt(wrds[3]);
 
       int  pop = Integer.parseInt(wrds[3]);
       if (pop<smallest && region.equals("Middle East"))
+
       if (pop&lt;smallest &amp;&amp; region.equals("Middle East"))
 
       {
 
       {
 
         smallest = pop;
 
         smallest = pop;
Line 150: Line 169:
 
   }
 
   }
 
}
 
}
   ]]></answer><shell lang="java" className="Cia" import="shells.xml#null"/>
+
   </answer><shell lang="java" className="Cia" import="shells.xml#null"/>
 
<p class="imper">
 
<p class="imper">
 
What is the name of the country in region "Middle East" with the
 
What is the name of the country in region "Middle East" with the
Line 156: Line 175:
 
</p>
 
</p>
 
</question>
 
</question>
 +
{{Accumulator Tutorial}}

Latest revision as of 20:18, 12 September 2012

The biggest population

What is the population of the largest country (by population).

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

Which country has the biggest population?

What is the name of the largest country (by population).

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

Which middle eastern country has the smallest population?

What is the name of the country in region "Middle East" with the smallest population.

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