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)                            ______0 or 1  _______
       
    2. toss.nextInt(2) == 0                   ______true or false ___
       
    3. Coin Q2 = new Coin();
      Q2.headCount();                         _0-1000 or appoximately 500_

     

    import java.util.HashMap;

    public class Codes
    {
        HashMap <String> codeNames;
        public Codes()
        {
            codeNames = new HashMap <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 <String, TicketMachine> machines = 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();
        }
    }

  2. (3) Give the object diagram corresponding to C201.


     
  3. (2) For class Codes what is:
    1. Codes C201 = new Codes();
      C201.encode("Kim");                        ______"9t3bs"______
       
    2. Codes C201 = new Codes();
      C201.encode("James");                    _______null________
       
  4. (4) For class Machines what is:
    1. Machines tms = new Machines();
      tms.getMachine("50 Cents");            t0 or TicketMachine(50)_
    2. Machines tms = new Machines();
      tms.get("25 Cents");                        _______25__________

     

  5. (3) Give the object diagram corresponding to tms.