Exercise 5        Name __________________        Score __/16

 

 import java.util.Random;
 public class Coin
 {
     private Random toss;

     public Coin()
    {
       toss = new Random();
    }

    public boolean heads()
   {
        return toss.nextInt(2) == 0;
   }
    public int headCount()
   {
       int count = 0;
       for (int i=1; i<=1000; i++)
              if ( heads() )
                     count = count + 1;
       return count;
    }
}
  1. (4) From the class Coin above, what is (some are ranges, others approximate answers):
     
    1. toss.nextInt(2)                            ______  _______
       
    2. toss.nextInt(2) == 0                   ___________ ___
       
    3. Coin Q2 = new Coin();
      Q2.headCount();                         ______________

 

import java.util.HashMap;

public class Codes
{
    HashMap <String, String> codeNames;
    public Codes()
    {
        codeNames = new HashMap <String, String>();
        codeNames.put("Jason", "4?fxy");
        codeNames.put("Geoff", "%$#!");
        codeNames.put("Kim", "9t3bs");
    }

    public String encode(String name)
    {
        return codeNames.get(name);
    }    
}
import java.util.HashMap;

public class Machines
{
    TicketMachine t0, t1;
    HashMap machines <String, TicketMachine> = new HashMap <String, TicketMachine> ();

    public Machines()
    {
        t0=new TicketMachine(50);
        t1=new TicketMachine(25);
        machines.put("50 Cents", t0);
        machines.put("25 Cents", t1);
    }

    public TicketMachine getMachine(String key)
    {
        return machines.get(key);
    }

    public int get(String key)
    {
        return getMachine(key).getPrice();
    }
}

 

  1. (3) Give the object diagram corresponding to:

    Codes C201 = new Codes();







     

  2. (2) For class Codes what is:
    1. Codes C201 = new Codes();
      C201.encode("Kim");                        ____________
       
    2. Codes C201 = new Codes();
      C201.encode("James");                    ____________
       
  3. (4) For class Machines what is:
    1. Machines tms = new Machines();
      tms.getMachine("50 Cents");            ____________
       
    2. Machines tms = new Machines();
      tms.get("25 Cents");                        ____________

     

  4. (3) Give the object diagram corresponding to:

    Machines tms = new Machines();