Difference between revisions of "DOM Tutorial Monopoly Questions (Medium)"

From Progzoo
Jump to: navigation, search
(How many properties?)
(Dog buys property)
 
(7 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 +
{{DOM Tutorials}}
 
These question concern the monopoly documents.
 
These question concern the monopoly documents.
 
Your code is tested against three different documents http://progzoo.net/monopoly1.xml
 
Your code is tested against three different documents http://progzoo.net/monopoly1.xml
Line 28: Line 29:
 
<shell import='shells_monopoly.xml#triple'/>
 
<shell import='shells_monopoly.xml#triple'/>
 
<p class='imper'>Show the number of properties held by player "ship" (count only property - not stations)</p>
 
<p class='imper'>Show the number of properties held by player "ship" (count only property - not stations)</p>
<prog>static void doIt(Document d){
+
<prog>static void doIt(Document d)
 +
{
 
   System.out.println(d.getDocumentElement());
 
   System.out.println(d.getDocumentElement());
 
}
 
}
Line 34: Line 36:
 
</prog>
 
</prog>
 
<answer>
 
<answer>
static void doIt(Document d){
+
static void doIt(Document d)
 +
{
 
   System.out.println(d.getElementById("ship")
 
   System.out.println(d.getElementById("ship")
 
         .getElementsByTagName("property").getLength());
 
         .getElementsByTagName("property").getLength());
Line 46: Line 49:
 
<p class='imper'>Player dog throws a 3. Give the label of the square he will land on.</p>
 
<p class='imper'>Player dog throws a 3. Give the label of the square he will land on.</p>
  
<prog>static void doIt(Document d){
+
<prog>static void doIt(Document d)
 +
{
 
   System.out.println(d.getDocumentElement());
 
   System.out.println(d.getDocumentElement());
 
}
 
}
Line 52: Line 56:
  
 
</prog>
 
</prog>
 +
<answer>
 +
static void doIt(Document d)
 +
{
 +
  Element current_sq = (Element) d.getElementById("dog").getParentNode();
 +
  // first jump
 +
  Element dest_sq = (Element) current_sq.getNextSibling();
 +
  if (dest_sq == null)
 +
  {
 +
    dest_sq = (Element)current_sq.getParentNode().getFirstChild();
 +
  }
 +
 
 +
  //second jump 
 +
  dest_sq = (Element) dest_sq.getNextSibling();
 +
  if (dest_sq == null)
 +
  {
 +
    dest_sq = (Element)current_sq.getParentNode().getFirstChild();
 +
  }
 +
 +
  //third jump 
 +
  dest_sq = (Element)dest_sq.getNextSibling();
 +
  if (dest_sq == null)
 +
  {
 +
    dest_sq = (Element)current_sq.getParentNode().getFirstChild();
 +
  }
 +
 
 +
  //output
 +
  System.out.println(dest_sq.getAttribute("label"));
 +
}
 +
 +
</answer>
 
</question>
 
</question>
  
Line 59: Line 93:
 
<p class='imper'>If one player holds all properties that are light-blue then give the id of that player. Otherwise give "none".</p>
 
<p class='imper'>If one player holds all properties that are light-blue then give the id of that player. Otherwise give "none".</p>
  
<prog>static void doIt(Document d){
+
<prog>static void doIt(Document d)
 +
{
 
   System.out.println(d.getDocumentElement());
 
   System.out.println(d.getDocumentElement());
 
}
 
}
Line 72: Line 107:
 
<p class='imper'>State the amount due in rent if you land on kings-cross.If the owner of kings-cross has 1 station pay 25, if he has 2 stations pay 50, if 3 stations then 150, if 4 stations pay 200. If the station is owned by the bank pay 0.</p>
 
<p class='imper'>State the amount due in rent if you land on kings-cross.If the owner of kings-cross has 1 station pay 25, if he has 2 stations pay 50, if 3 stations then 150, if 4 stations pay 200. If the station is owned by the bank pay 0.</p>
  
<prog>static void doIt(Document d){
+
<prog>static void doIt(Document d)
 +
{
 
   System.out.println(d.getDocumentElement());
 
   System.out.println(d.getDocumentElement());
 
}
 
}
Line 78: Line 114:
  
 
</prog>
 
</prog>
 +
<answer>
 +
static void doIt(Document d)
 +
{
 +
  Element parent = (Element) d.getElementById("kings-cross").getParentNode();
 +
  if (parent.getNodeName() == "bank")
 +
  {
 +
    System.out.println("0");
 +
  }
 +
  else
 +
  {
 +
    int count = parent.getElementsByTagName("station").getLength();
 +
 +
    switch (count)
 +
    {
 +
      case 1:  System.out.println("25"); break;
 +
      case 2:  System.out.println("50"); break;
 +
      case 3:  System.out.println("150"); break;
 +
      case 4:  System.out.println("400"); break;
 +
    }
 +
  }
 +
}
 +
</answer>
 
</question>
 
</question>
  
Line 85: Line 143:
 
<p class='imper'>Player "dog" throws a 5, lands on a property and buys it. Update the game document to reflect the new state of play.</p>
 
<p class='imper'>Player "dog" throws a 5, lands on a property and buys it. Update the game document to reflect the new state of play.</p>
  
<prog>static void doIt(Document d){
+
<prog>static void doIt(Document d)
 +
{
 
   Element e=d.getElementById("dog");
 
   Element e=d.getElementById("dog");
 
   e.setAttribute("cash","1234");
 
   e.setAttribute("cash","1234");

Latest revision as of 20:56, 12 September 2012

These question concern the monopoly documents. Your code is tested against three different documents http://progzoo.net/monopoly1.xml http://progzoo.net/monopoly2.xml and http://progzoo.net/monopoly3.xml

<!DOCTYPE game SYSTEM "monopoly.dtd">
<game>
 <board>
  <square label='GO'>
   <player id='dog'  cash='1500'/>
   <player id='ship' cash='1500'/>
   <player id='iron' cash='1500'/>
  </square>
  <square label='Old Kent Road' ref='old-kent-road'/>
  <square label='Community Chest'/>
  <square label='Whitechapel' ref='whitechapel'/>
  <square label='Income Tax (Pay 200)'/>
  <square label="King's Cross Station" ref='kings-cross'/>
 </board>
 <bank>
  <property id='old-kent-road' price='60'  colour='brown'/>
  <property id='whitechapel'   price='60'  colour='brown'/>
  <station  id='kings-cross'   price='200'/>
 </bank>
</game>

How many properties?

Show the number of properties held by player "ship" (count only property - not stations)


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

Move dog 3

Player dog throws a 3. Give the label of the square he will land on.



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

Light blues

If one player holds all properties that are light-blue then give the id of that player. Otherwise give "none".


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

King's cross rent

State the amount due in rent if you land on kings-cross.If the owner of kings-cross has 1 station pay 25, if he has 2 stations pay 50, if 3 stations then 150, if 4 stations pay 200. If the station is owned by the bank pay 0.



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

Dog buys property

Player "dog" throws a 5, lands on a property and buys it. Update the game document to reflect the new state of play.


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