Templates Hwk5java package hwk5 import javautilRandom public

Templates

Hwk5.java

package hwk5;

import java.util.Random;

public class Hwk5 {

   public static void main(String[] args) {

       int numOfGates = 5;

       Gym gym = new Gym(numOfGates, 1000, 0.6);

       gym.open(2);

       int numOfSimulations = 10000;

       Random random = new Random();

       for(int i=0; i<numOfSimulations; i++) {

           int gateNumber = random.nextInt(numOfGates);

           int inOrOut = random.nextInt(2);

           if(inOrOut == 1) {

               gym.getIn(gateNumber);

           }

           else {

               gym.getOut(gateNumber);

           }

           if (i > 0 && i % 1000 == 0) {

               String output = String.format(\"After %d simulations: %d persons are in the gym\ \",

                       i,

                       gym.personsInside());

               System.out.printf(\"%s\ %s\ \", output, gym);

           }

       }

   }

}

Gate.java

package hwk5;

enum State {IN, OUT, LOCKED}

public class Gate {

   private State tripod;

   // If negative, then it means the number of persons went outside through this gate

   private int personGoneInside = 0;

   private String name;

  

   // Gate Constructor

   public Gate(String name) { this(name, State.LOCKED); }

  

   public Gate(String name, State state) {

       this.name = name;

       this.tripod = state;

   }

   public int numPersonGoneInside() { return personGoneInside; }

  

   // To set the tripod direction

   public void setTripod(State dir) { tripod = dir; }

   // return true if and only if the gate is currently locked and will be unlocked after the call

   public boolean open(State dir) {

       boolean ret = tripod == State.LOCKED && dir != State.LOCKED;

       setTripod(dir);

       return ret;

   }

   // to close the gate

   public void close() { tripod = State.LOCKED; }

   // to check if the gate is locked or not

   public boolean isLocked() { return tripod == State.LOCKED; }

   // to return the tripod direction

   public State tripodState() { return tripod ; }

   public void getIn(int x) {

       if (tripod == State.IN) {

           personGoneInside += x;

       }

   }

   public void getOut(int x) {

       if (tripod == State.OUT) {

           personGoneInside -= x;

       }

   }

   // override of the toString method

   public String toString() {

       String state = \"\";

       switch(tripod) {

       case LOCKED: state = \"locked\"; break;

       case IN: state = \"open to get in\"; break;

       case OUT: state = \"open to get out\"; break;

       }

       String out;

       if(personGoneInside > 0) {

           out = \"came in through this gate\";

       }

       else {

           out = \"went out through this gate\";

       }

       return String.format(\"Gate %s is %s.\ %d persons %s\ \", name, state, Math.abs(personGoneInside), out);

   }

   @Override

   public boolean equals(Object that) {

       boolean ret;

       if(that instanceof Gate) {

           Gate thatGate = (Gate) that;

           ret = name.equals(thatGate.name) &&

                   tripod == thatGate.tripod &&

                   personGoneInside == thatGate.personGoneInside;

       }

       else {

           ret = false;

       }

       return ret;

   }

}


Gym.java

package hwk5;

public class Gym {

   final Gate[] gates;

   final int capacity;

   final GymController controller;

   // Initialize a gym controller with the \"occupancy\" parameter and \"this\"

   // and save it in the field \"controller\"

   public Gym(int numOfGates, int capacity, double occupancy) {

       // TODO

   }

   // Call previous constructor with default occupancy set to 0.5

/******************************************************************

   previous counstructor:

   // initialize with specified number of gates and with specified capacity

   public Gym(int numOfGates, int capacity) {

   gates = new Gate[numOfGates];

   this.capacity = capacity;

   for(int i = 0; i < gates.length; ++i){

   gates[i] = new Gate(\"gate_\" + String.valueOf(i));

   }

   }

   *******************************************************************/

   public Gym(int numOfGates, int capacity) {

       // TODO

   }

   // Call previous constructor with default capcity set to 1000

/******************************************************************

   previous counstructor:

   // initialize with specified number of gates and capacity set to 1000

   public Gym(int numOfGates) {

   gates = new Gate[numOfGates];

   capacity = 1000;

   for(int i = 0; i < gates.length; ++i){

   gates[i] = new Gate(\"gate_\" + String.valueOf(i));

   }

   }

   *******************************************************************/

   public Gym(int numOfGates) {

       // TODO

   }

   // Call previous constructor with default number of gates set to 3

/******************************************************************

   previous counstructor:

   // initialize with number of gates set to 3 and capacity set to 1000

   public Gym() {

   gates = new Gate[3];

   capacity = 1000;

   for(int i = 0; i < gates.length; ++i){

   gates[i] = new Gate(\"gate_\" + String.valueOf(i));

   }

   }

   *******************************************************************/

   public Gym() {

       // TODO

   }

// lock all gates

public void lockDown() {

for (Gate gate : gates) {

gate.close();

}

}

// open (roughly) half of the gates for in and the other half for out

public void open() {

open(gates.length / 2);

}

// if numOfInGate >= gates.length, then open (gates.length-1) gates for in and 1 for out

// if numOfInGate <= 0, then open 1 gate for in and rest for out

// otherwise, open \"numOfInGate\" gates for in and leave the rest for out

public void open(int numOfInGate) {

if(numOfInGate >= gates.length){

numOfInGate = gates.length - 1;

}

else if(numOfInGate <= 0){

numOfInGate = 1;

}

for(int i = 0; i < gates.length; ++i){

gates[i].open(i < numOfInGate ? State.IN : State.OUT);

}

}

   // find one gate that is not at the \"state\" (if exists) and set its tripod state to that \"state\"

   public void switchOneGate(State state) {

       // TODO

   }

  

   // Return the number of gates in \"state\"

   public int numberOfGatesInState(State state) {

       // TODO

   }

  

   // Return the total number of gates

   public int numberOfGates() {

       // TODO

   }

// enters 1 person at gates[gateNumber] if the persons inside do not exceed capacity

// do nothing is the gateNumber is not legal

public void getIn(int gateNumber) {

if(gateNumber >= 0 && gateNumber < gates.length){

gates[gateNumber].getIn(1);

}

}

// exits 1 person at gates[gateNumber] if there is at least 1 person inside

// do nothing is the gateNumber is not legal

public void getOut(int gateNumber) {

if(gateNumber >= 0 && gateNumber < gates.length){

gates[gateNumber].getOut(1);

}

}

   // returns true if and only if capacity is not reached

public boolean canGoIn() {

return personsInside() < capacity;

}

   }

// returns true if and only if it is not empty

public boolean canGoOut() {

return personsInside() > 0;

}

   }

// return the number of persons inside the gym

   // Call the onChange method of \"controller

   public int personsInside() {

       int sum = 0; // this is the variable to hold total number of persons inside

for (Gate gate : gates) {

sum += gate.numPersonGoneInside();

}

return sum;

       // TODO: call onChange method of \"controller\" with \"sum\"

   }

//print each gate of the gym

@Override

public String toString() {

String ret = \"\";

for (int i = 0; i < gates.length; ++i) {

if(gates[i].tripodState() == State.LOCKED){

ret += \"Gate gate_\" + i + \" is locked.\ 0 persons went out through this gate\ \ \";

}

else if(gates[i].tripodState() == State.IN){

int numPersons = (int) Math.abs(gates[i].numPersonGoneInside());

if(numPersons == 0)

ret += \"Gate gate_\" + i + \" is open to get in.\ \" + numPersons + \" persons went out through this gate\ \ \";

else

ret += \"Gate gate_\" + i + \" is open to get in.\ \" + numPersons + \" persons came in through this gate\ \ \";

}

else{

int numPersons = (int) Math.abs(gates[i].numPersonGoneInside());

ret += \"Gate gate_\" + i + \" is open to get out.\ \" + numPersons + \" persons went out through this gate\ \ \";

}

}

return ret;

}

}

}


GymController.java

package hwk5;

public class GymController {

   final double toleranceIncrement = 0.01;

   double outTolerance = toleranceIncrement, inTolerance = toleranceIncrement;

   final double occupancy; // target ratio of occupancy

   final Gym gym; // gym object under control

   // Save occupancy ratio and gym object

   // Make sure the occupancy parameter between 0 and 1; if not, set it to 0.5

   // Check tolerances to ensure they are legal with respect to the target occupancy ratio

   GymController(double occupancy, Gym gym)

{

       // TODO

   }

   // Make sure tolerances satisfy the condition

   // occupancy + outTolerance <= 1

   // occupancy - inTolerance >= 0

   //

   // If they are violated, then change the tolerances so that they are satisfied.

   private void checkTolerance()

{

this.outTolerance = Math.min(this.outTolerance, 1 - this.occupancy);

this.inTolerance = Math.min(this.inTolerance, this.occupancy);

   }

   // Increment out tolerance and

   // decrement in tolerance (if it is > tolerance increment)

   // and also ensure they are legal

   private void adjustToleranceForOut()

{

       // TODO

   }

   // Increment in tolerance and

   // decrement out tolerance (if it is > tolerance increment)

   // and also ensure they are legal

   private void adjustToleranceForIn()

{

       // TODO

   }

   // This is called by the personsInside method of the gym object

   // If the current occupancy ratio is great than the target occupancy + out tolerance

   // and there are more than one gate that are not in OUT state

   // then switch one gate to OUT state

   //

   // If the current occupancy ratio is less than the target occupancy - in tolerance

   // and there are more than one gate that are not in IN state

   // then switch one gate to IN state

   //  

   // When a gate is switched to IN or OUT, adjust corresponding tolerance

   public void onChange(int personsInside)

{

       // TODO

   }

}


Testing.java

package hwk5;

import static org.junit.Assert.*;

import org.junit.*;

public class Testing {

   Gym a;

   Gym b;

   Gym c;

   Gym d;

   GymController e;

   GymController f;

   @Before

   public void setUp()

   {

       this.a = new Gym();

       this.b = new Gym(0);

       this.c = new Gym(5,1000);

       this.d = new Gym(6,2000,0.6);

       this.e = new GymController(-0.1,d);

       this.f = new GymController(1.1,c);

   }

   @After

   public void tearDown()

   {    

       this.a = null;

       this.b = null;

       this.c = null;

   }

  

   @Test

   public void TestControllerConstructor1()

   {

       assertEquals(e.occupancy,0.5,0);

       assertEquals(e.gym,d);

       assertEquals(f.occupancy,0.5,0);

       assertEquals(f.gym,c);

       assertTrue(e.inTolerance<=e.occupancy);

       assertTrue(e.outTolerance<=1-e.occupancy);

   }

   @Test

   public void TestConstructor1()

   {

       Gate[] gates = new Gate[3];

       gates[0]= new Gate(\"gate_0\");

       gates[1]= new Gate(\"gate_1\");

       gates[2]= new Gate(\"gate_2\");

       assertEquals(this.a.getClass().getSimpleName().toString(), \"Gym\");

       assertEquals(1000,this.a.capacity);

       assertArrayEquals(a.gates, gates);

   }

   @Test

   public void TestConstructor2()

   {

       Gate[] gates = new Gate[0];

       assertEquals(this.b.getClass().getSimpleName().toString(), \"Gym\");

       assertEquals(1000,this.b.capacity);

       assertArrayEquals(b.gates, gates);

   }

   @Test

   public void TestConstructor3()

   {

       Gate[] gates = new Gate[5];

       gates[0]= new Gate(\"gate_0\");

       gates[1]= new Gate(\"gate_1\");

       gates[2]= new Gate(\"gate_2\");

       gates[3]= new Gate(\"gate_3\");

       gates[4]= new Gate(\"gate_4\");

       assertEquals(this.c.getClass().getSimpleName().toString(), \"Gym\");

       assertEquals(1000,this.c.capacity);

       assertArrayEquals(c.gates, gates);

   }

   @Test

   public void TestConstructor4()

   {

       Gate[] gates = new Gate[6];

       gates[0]= new Gate(\"gate_0\");

       gates[1]= new Gate(\"gate_1\");

       gates[2]= new Gate(\"gate_2\");

       gates[3]= new Gate(\"gate_3\");

       gates[4]= new Gate(\"gate_4\");

       gates[5]= new Gate(\"gate_5\");

       assertEquals(this.d.getClass().getSimpleName().toString(), \"Gym\");

       assertEquals(2000,this.d.capacity);

       assertArrayEquals(d.gates, gates);

   }

   @Test

   public void TestnumberOfGates()

   {

       assertEquals(6,this.d.numberOfGates());

   }

   @Test

   public void TestnumberOfGatesInState()

   {

       d.gates[0].setTripod(State.IN);

       d.gates[1].setTripod(State.IN);

       d.gates[2].setTripod(State.OUT);

       d.gates[3].setTripod(State.IN);

       d.gates[4].setTripod(State.OUT);

       d.gates[5].setTripod(State.LOCKED);

       assertEquals(d.numberOfGatesInState(State.IN),3);

       assertEquals(d.numberOfGatesInState(State.OUT),2);

       assertEquals(d.numberOfGatesInState(State.LOCKED),1);

   }

   @Test

   public void TestswitchOneGate()

   {

       d.gates[0].setTripod(State.IN);

       d.gates[1].setTripod(State.IN);

       d.gates[2].setTripod(State.OUT);

       d.gates[3].setTripod(State.IN);

       d.gates[4].setTripod(State.OUT);

       d.gates[5].setTripod(State.LOCKED);

       d.switchOneGate(State.OUT);

       assertEquals(d.numberOfGatesInState(State.IN),2);

       assertEquals(d.numberOfGatesInState(State.OUT),3);

       assertEquals(d.numberOfGatesInState(State.LOCKED),1);

       d.gates[0].setTripod(State.OUT);

       d.gates[1].setTripod(State.OUT);

       d.gates[2].setTripod(State.OUT);

       d.gates[3].setTripod(State.OUT);

       d.gates[4].setTripod(State.OUT);

       d.gates[5].setTripod(State.OUT);

       d.switchOneGate(State.OUT);

       assertEquals(d.numberOfGatesInState(State.OUT),6);      

   }

   @Test

   public void TestpersonsInside1()

   {

       d.gates[0].setTripod(State.IN);

       d.gates[1].setTripod(State.IN);

       d.gates[2].setTripod(State.OUT);

       d.gates[3].setTripod(State.IN);

       d.gates[4].setTripod(State.OUT);

       d.gates[0].getIn(650);

       d.gates[1].getIn(150);

       d.gates[2].getOut(200);

       d.gates[3].getIn(550);

       d.gates[4].getOut(150);//no switch

       d.personsInside();

       assertEquals(d.numberOfGatesInState(State.IN),4);

       assertEquals(d.numberOfGatesInState(State.OUT),1);

       assertEquals(d.controller.inTolerance,0.02,0.0);

       assertEquals(d.controller.outTolerance,0.01,0.0);

       assertEquals(d.personsInside(),1000);

   }

   @Test

   public void TestpersonsInside2()

   {  

       d.gates[0].setTripod(State.IN);

       d.gates[1].setTripod(State.IN);

       d.gates[2].setTripod(State.OUT);

       d.gates[3].setTripod(State.IN);

       d.gates[4].setTripod(State.OUT);

       d.gates[0].getIn(1050);

       d.gates[1].getIn(150);

       d.gates[2].getOut(200);

       d.gates[3].getIn(550);

       d.gates[4].getOut(150);//no switch

  

       d.personsInside();

       assertEquals(d.numberOfGatesInState(State.IN),2);

       assertEquals(d.numberOfGatesInState(State.OUT),3);

       assertEquals(d.controller.inTolerance,0.01,0.0);

       assertEquals(d.controller.outTolerance,0.02,0.0);

       assertEquals(d.personsInside(),1400);

   }

   @Test

   public void TestpersonsInside3()

   {

       d.gates[0].setTripod(State.IN);

       d.gates[1].setTripod(State.IN);

       d.gates[2].setTripod(State.IN);

       d.gates[3].setTripod(State.IN);

       d.gates[4].setTripod(State.IN);

       d.getIn(3000);

       d.personsInside();

       assertEquals(d.numberOfGatesInState(State.IN),5);

       assertEquals(d.numberOfGatesInState(State.OUT),0);

       assertEquals(d.controller.inTolerance,0.01,0.0);

       assertEquals(d.controller.outTolerance,0.01,0.0);

       assertEquals(d.personsInside(),0);

   }

}

Homework 5 October 8, 2016 1 What to implement? For this homework, you will implement a class Gy Controller and revise the class Gym of Hwk4 to simulate the UWM Gymns. You also use the Gate class from Hwk3 The purpose of GynController is to allow you to set a target occupancy ratio. For example, if the target occurpancy is 0.5 and the capacity of the gym is 1000, then ideally there should be 500 persons inside the gym. However, if there are less than 500 persons inside, then we should open a gate to people to come in. On the other hand, if there are more than 500 persons inside, we should open a gate for people to get out. To avoid ing gates for or out too freqently we incle tolerance values when comparing the current occupancy ratio with the target occupancy There is ain tolerance and out tolerance For example, we only turn a gate for people to get out if current occupancy-target occupancy + out tolerance We only turn a gate for people to come in if current occupancy

Solution


package hwk5;

import java.util.Random;

//Hwk5.java
public class Hwk5 {

public static void main(String[] args) {
int numOfGates = 5;

Gym gym = new Gym(numOfGates, 1000, 0.6);
gym.open(2);

int numOfSimulations = 10000;

Random random = new Random();
for(int i=0; i<numOfSimulations; i++) {

int gateNumber = random.nextInt(numOfGates);
int inOrOut = random.nextInt(2);

if(inOrOut == 1) {
gym.getIn(gateNumber);
}
else {
gym.getOut(gateNumber);
}
if (i > 0 && i % 1000 == 0) {
String output = String.format(\"After %d simulations: %d persons are in the gym\ \",
i,
gym.personsInside());

System.out.printf(\"%s\ %s\ \", output, gym);
}
}
}

}
///////////////////////////////////////////////////////////////////

//Gate.java

package hwk5;

enum State {IN, OUT, LOCKED}

public class Gate {
private State tripod;
// If negative, then it means the number of persons went outside through this gate
private int personGoneInside = 0;
private String name;
  
// Gate Constructor
public Gate(String name) { this(name, State.LOCKED); }
  
public Gate(String name, State state) {
this.name = name;
this.tripod = state;
}

public int numPersonGoneInside() { return personGoneInside; }
  
// To set the tripod direction
public void setTripod(State dir) { tripod = dir; }

// return true if and only if the gate is currently locked and will be unlocked after the call
public boolean open(State dir) {
boolean ret = tripod == State.LOCKED && dir != State.LOCKED;
setTripod(dir);
return ret;
}

// to close the gate
public void close() { tripod = State.LOCKED; }

// to check if the gate is locked or not
public boolean isLocked() { return tripod == State.LOCKED; }

// to return the tripod direction
public State tripodState() { return tripod ; }

public void getIn(int x) {
if (tripod == State.IN) {
personGoneInside += x;
}
}
public void getOut(int x) {
if (tripod == State.OUT) {
personGoneInside -= x;
}
}

// override of the toString method
public String toString() {
String state = \"\";
switch(tripod) {
case LOCKED: state = \"locked\"; break;
case IN: state = \"open to get in\"; break;
case OUT: state = \"open to get out\"; break;
}
String out;
if(personGoneInside > 0) {
out = \"came in through this gate\";
}
else {
out = \"went out through this gate\";
}
return String.format(\"Gate %s is %s.\ %d persons %s\ \", name, state, Math.abs(personGoneInside), out);
}

@Override
public boolean equals(Object that) {
boolean ret;
if(that instanceof Gate) {
Gate thatGate = (Gate) that;
ret = name.equals(thatGate.name) &&
tripod == thatGate.tripod &&
personGoneInside == thatGate.personGoneInside;
}
else {
ret = false;
}
return ret;
}
}

/////////////////////////////////////////////////////////////
//Gym.java

package hwk5;

public class Gym {
final Gate[] gates;
final int capacity;
final GymController controller;

// Initialize a gym controller with the \"occupancy\" parameter and \"this\"
// and save it in the field \"controller\"
public Gym(int numOfGates, int capacity, double occupancy) {
gates=new Gate[numOfGates];
this.capacity=capacity;
controller=new GymController(occupancy,this);
}
// Call previous constructor with default occupancy set to 0.5
/* *****************************************************************
previous counstructor:
// initialize with specified number of gates and with specified capacity
public Gym(int numOfGates, int capacity) {
gates = new Gate[numOfGates];
this.capacity = capacity;
for(int i = 0; i < gates.length; ++i){
gates[i] = new Gate(\"gate_\" + String.valueOf(i));
}
}
*******************************************************************/
public Gym(int numOfGates, int capacity) {
gates = new Gate[numOfGates];
this.capacity = capacity;
for(int i = 0; i < gates.length; ++i){
gates[i] = new Gate(\"gate_\" + String.valueOf(i));
}
controller=null;
}
// Call previous constructor with default capcity set to 1000
/*
*****************************************************************
previous counstructor:
// initialize with specified number of gates and capacity set to 1000
public Gym(int numOfGates) {
gates = new Gate[numOfGates];
capacity = 1000;
for(int i = 0; i < gates.length; ++i){
gates[i] = new Gate(\"gate_\" + String.valueOf(i));
}
}
****************************************************************** */

public Gym(int numOfGates) {
gates = new Gate[numOfGates];
capacity = 1000;
for(int i = 0; i < gates.length; ++i){
gates[i] = new Gate(\"gate_\" + String.valueOf(i));
}
controller=null;
}
// Call previous constructor with default number of gates set to 3
/* *****************************************************************
previous counstructor:
// initialize with number of gates set to 3 and capacity set to 1000
public Gym() {
gates = new Gate[3];
capacity = 1000;
for(int i = 0; i < gates.length; ++i){
gates[i] = new Gate(\"gate_\" + String.valueOf(i));
}
}
*******************************************************************/
public Gym() {
gates = new Gate[3];
capacity = 1000;
for(int i = 0; i < gates.length; ++i){
gates[i] = new Gate(\"gate_\" + String.valueOf(i));
}
controller=null;
}
// lock all gates
public void lockDown() {
for (Gate gate : gates) {
gate.close();
}
}
// open (roughly) half of the gates for in and the other half for out
public void open() {
open(gates.length / 2);
}
// if numOfInGate >= gates.length, then open (gates.length-1) gates for in and 1 for out
// if numOfInGate <= 0, then open 1 gate for in and rest for out
// otherwise, open \"numOfInGate\" gates for in and leave the rest for out
public void open(int numOfInGate) {
if(numOfInGate >= gates.length){
numOfInGate = gates.length - 1;
}
else if(numOfInGate <= 0){
numOfInGate = 1;
}
for(int i = 0; i < gates.length; ++i){
gates[i].open(i < numOfInGate ? State.IN : State.OUT);
}
}

// find one gate that is not at the \"state\" (if exists) and set its tripod state to that \"state\"
public void switchOneGate(State state) {

for(int i=0;i<gates.length;i++)
{
if(gates[i].tripodState()!=state)
{
gates[i].setTripod(state);
}
}
}
  
// Return the number of gates in \"state\"
public int numberOfGatesInState(State state) {
  
int n=0;
for(int i=0;i<gates.length;i++)
{
if(gates[i].tripodState()==state)
{
n++;
}
}
return n;
}
  
// Return the total number of gates
public int numberOfGates() {
return gates.length;
}
  
// enters 1 person at gates[gateNumber] if the persons inside do not exceed capacity
// do nothing is the gateNumber is not legal
public void getIn(int gateNumber) {
if(gateNumber >= 0 && gateNumber < gates.length){
gates[gateNumber].getIn(1);
}
}
// exits 1 person at gates[gateNumber] if there is at least 1 person inside
// do nothing is the gateNumber is not legal
public void getOut(int gateNumber) {
if(gateNumber >= 0 && gateNumber < gates.length){
gates[gateNumber].getOut(1);
}
}
  
// returns true if and only if capacity is not reached
public boolean canGoIn() {
//personsInside
int sum=0;
for(int i=0;i<gates.length;i++)
{
sum+=gates[i].numPersonGoneInside();
}
return sum < capacity;
}


// returns true if and only if it is not empty
public boolean canGoOut() {
//personsInside
int sum=0;
for(int i=0;i<gates.length;i++)
{
sum+=gates[i].numPersonGoneInside();
}
return sum> 0;
}

// return the number of persons inside the gym
// Call the onChange method of \"controller
public int personsInside() {
int sum = 0; // this is the variable to hold total number of persons inside

for (Gate gate : gates) {
sum += gate.numPersonGoneInside();
}
// TODO: call onChange method of \"controller\" with \"sum\"
controller.onChange(sum);
return sum;

  
  

}

//print each gate of the gym
@Override
public String toString() {
String ret = \"\";
for (int i = 0; i < gates.length; ++i) {
if(gates[i].tripodState() == State.LOCKED){
ret += \"Gate gate_\" + i + \" is locked.\ 0 persons went out through this gate\ \ \";
}
else if(gates[i].tripodState() == State.IN){
int numPersons = (int) Math.abs(gates[i].numPersonGoneInside());
if(numPersons == 0)
ret += \"Gate gate_\" + i + \" is open to get in.\ \" + numPersons + \" persons went out through this gate\ \ \";
else
ret += \"Gate gate_\" + i + \" is open to get in.\ \" + numPersons + \" persons came in through this gate\ \ \";
}
else{
int numPersons = (int) Math.abs(gates[i].numPersonGoneInside());
ret += \"Gate gate_\" + i + \" is open to get out.\ \" + numPersons + \" persons went out through this gate\ \ \";
}
}
return ret;
}
}

////////////////////////////////////////////////
//GymController.java

package hwk5;

public class GymController {
final double toleranceIncrement = 0.01;

double outTolerance = toleranceIncrement, inTolerance = toleranceIncrement;

final double occupancy; // target ratio of occupancy
final Gym gym; // gym object under control

// Save occupancy ratio and gym object
// Make sure the occupancy parameter between 0 and 1; if not, set it to 0.5
// Check tolerances to ensure they are legal with respect to the target occupancy ratio
GymController(double occupancy, Gym gym)
{
if(occupancy>=0 && occupancy<1)
this.occupancy=occupancy;
else
this.occupancy=0.5;
this.gym=gym;
checkTolerance();
}

// Make sure tolerances satisfy the condition
// occupancy + outTolerance <= 1
// occupancy - inTolerance >= 0
//
// If they are violated, then change the tolerances so that they are satisfied.
private void checkTolerance()
{
this.outTolerance = Math.min(this.outTolerance, 1 - this.occupancy);
this.inTolerance = Math.min(this.inTolerance, this.occupancy);
}

// Increment out tolerance and
// decrement in tolerance (if it is > tolerance increment)
// and also ensure they are legal
private void adjustToleranceForOut()
{
outTolerance++;
if(inTolerance>toleranceIncrement)
inTolerance--;
checkTolerance();
}

// Increment in tolerance and
// decrement out tolerance (if it is > tolerance increment)
// and also ensure they are legal
private void adjustToleranceForIn()
{
inTolerance++;
if(outTolerance>toleranceIncrement)
outTolerance--;
checkTolerance();
}

// This is called by the personsInside method of the gym object
// If the current occupancy ratio is great than the target occupancy + out tolerance
// and there are more than one gate that are not in OUT state
// then switch one gate to OUT state
//
// If the current occupancy ratio is less than the target occupancy - in tolerance
// and there are more than one gate that are not in IN state
// then switch one gate to IN state
//
// When a gate is switched to IN or OUT, adjust corresponding tolerance
public void onChange(int personsInside)
{
if(personsInside>(occupancy+outTolerance) )
{
if(gym.numberOfGatesInState(State.OUT)>1)
{
gym.switchOneGate(State.OUT);
adjustToleranceForOut();
}
}
if(personsInside<(occupancy+outTolerance) )
{
if(gym.numberOfGatesInState(State.IN)>1)
{
gym.switchOneGate(State.IN);
adjustToleranceForIn();
}
}

}
}


//////////////////////////////////////////
//Testing.java

package hwk5;
import static org.junit.Assert.*;

import org.junit.*;

public class Testing {

Gym a;
Gym b;
Gym c;
Gym d;
GymController e;
GymController f;

@Before
public void setUp()
{
this.a = new Gym();
this.b = new Gym(0);
this.c = new Gym(5,1000);
this.d = new Gym(6,2000,0.6);
this.e = new GymController(-0.1,d);
this.f = new GymController(1.1,c);
}

@After
public void tearDown()
{
this.a = null;
this.b = null;
this.c = null;
}
  
@Test
public void TestControllerConstructor1()
{
assertEquals(e.occupancy,0.5,0);
assertEquals(e.gym,d);
assertEquals(f.occupancy,0.5,0);
assertEquals(f.gym,c);
assertTrue(e.inTolerance<=e.occupancy);
assertTrue(e.outTolerance<=1-e.occupancy);
}

@Test
public void TestConstructor1()
{

Gate[] gates = new Gate[3];
gates[0]= new Gate(\"gate_0\");
gates[1]= new Gate(\"gate_1\");
gates[2]= new Gate(\"gate_2\");

assertEquals(this.a.getClass().getSimpleName().toString(), \"Gym\");
assertEquals(1000,this.a.capacity);
assertArrayEquals(a.gates, gates);


}

@Test
public void TestConstructor2()
{
Gate[] gates = new Gate[0];
assertEquals(this.b.getClass().getSimpleName().toString(), \"Gym\");
assertEquals(1000,this.b.capacity);
assertArrayEquals(b.gates, gates);



}

@Test
public void TestConstructor3()
{
Gate[] gates = new Gate[5];
gates[0]= new Gate(\"gate_0\");
gates[1]= new Gate(\"gate_1\");
gates[2]= new Gate(\"gate_2\");
gates[3]= new Gate(\"gate_3\");
gates[4]= new Gate(\"gate_4\");

assertEquals(this.c.getClass().getSimpleName().toString(), \"Gym\");
assertEquals(1000,this.c.capacity);
assertArrayEquals(c.gates, gates);


}

@Test
public void TestConstructor4()
{
Gate[] gates = new Gate[6];
gates[0]= new Gate(\"gate_0\");
gates[1]= new Gate(\"gate_1\");
gates[2]= new Gate(\"gate_2\");
gates[3]= new Gate(\"gate_3\");
gates[4]= new Gate(\"gate_4\");
gates[5]= new Gate(\"gate_5\");

assertEquals(this.d.getClass().getSimpleName().toString(), \"Gym\");
assertEquals(2000,this.d.capacity);
assertArrayEquals(d.gates, gates);
}

@Test
public void TestnumberOfGates()
{
assertEquals(6,this.d.numberOfGates());
}

@Test
public void TestnumberOfGatesInState()
{
d.gates[0].setTripod(State.IN);
d.gates[1].setTripod(State.IN);
d.gates[2].setTripod(State.OUT);
d.gates[3].setTripod(State.IN);
d.gates[4].setTripod(State.OUT);
d.gates[5].setTripod(State.LOCKED);
assertEquals(d.numberOfGatesInState(State.IN),3);
assertEquals(d.numberOfGatesInState(State.OUT),2);
assertEquals(d.numberOfGatesInState(State.LOCKED),1);
}

@Test
public void TestswitchOneGate()
{
d.gates[0].setTripod(State.IN);
d.gates[1].setTripod(State.IN);
d.gates[2].setTripod(State.OUT);
d.gates[3].setTripod(State.IN);
d.gates[4].setTripod(State.OUT);
d.gates[5].setTripod(State.LOCKED);
d.switchOneGate(State.OUT);
assertEquals(d.numberOfGatesInState(State.IN),2);
assertEquals(d.numberOfGatesInState(State.OUT),3);
assertEquals(d.numberOfGatesInState(State.LOCKED),1);

d.gates[0].setTripod(State.OUT);
d.gates[1].setTripod(State.OUT);
d.gates[2].setTripod(State.OUT);
d.gates[3].setTripod(State.OUT);
d.gates[4].setTripod(State.OUT);
d.gates[5].setTripod(State.OUT);
d.switchOneGate(State.OUT);

assertEquals(d.numberOfGatesInState(State.OUT),6);
}

@Test
public void TestpersonsInside1()
{
d.gates[0].setTripod(State.IN);
d.gates[1].setTripod(State.IN);
d.gates[2].setTripod(State.OUT);
d.gates[3].setTripod(State.IN);
d.gates[4].setTripod(State.OUT);

d.gates[0].getIn(650);
d.gates[1].getIn(150);
d.gates[2].getOut(200);
d.gates[3].getIn(550);
d.gates[4].getOut(150);//no switch

d.personsInside();
assertEquals(d.numberOfGatesInState(State.IN),4);
assertEquals(d.numberOfGatesInState(State.OUT),1);
assertEquals(d.controller.inTolerance,0.02,0.0);
assertEquals(d.controller.outTolerance,0.01,0.0);
assertEquals(d.personsInside(),1000);
}

@Test
public void TestpersonsInside2()
{
d.gates[0].setTripod(State.IN);
d.gates[1].setTripod(State.IN);
d.gates[2].setTripod(State.OUT);
d.gates[3].setTripod(State.IN);
d.gates[4].setTripod(State.OUT);

d.gates[0].getIn(1050);
d.gates[1].getIn(150);
d.gates[2].getOut(200);
d.gates[3].getIn(550);
d.gates[4].getOut(150);//no switch
  

d.personsInside();
assertEquals(d.numberOfGatesInState(State.IN),2);
assertEquals(d.numberOfGatesInState(State.OUT),3);
assertEquals(d.controller.inTolerance,0.01,0.0);
assertEquals(d.controller.outTolerance,0.02,0.0);
assertEquals(d.personsInside(),1400);
}
@Test
public void TestpersonsInside3()
{
d.gates[0].setTripod(State.IN);
d.gates[1].setTripod(State.IN);
d.gates[2].setTripod(State.IN);
d.gates[3].setTripod(State.IN);
d.gates[4].setTripod(State.IN);

d.getIn(3000);

d.personsInside();
assertEquals(d.numberOfGatesInState(State.IN),5);
assertEquals(d.numberOfGatesInState(State.OUT),0);
assertEquals(d.controller.inTolerance,0.01,0.0);
assertEquals(d.controller.outTolerance,0.01,0.0);
assertEquals(d.personsInside(),0);
}
}

Templates Hwk5.java package hwk5; import java.util.Random; public class Hwk5 { public static void main(String[] args) { int numOfGates = 5; Gym gym = new Gym(nu
Templates Hwk5.java package hwk5; import java.util.Random; public class Hwk5 { public static void main(String[] args) { int numOfGates = 5; Gym gym = new Gym(nu
Templates Hwk5.java package hwk5; import java.util.Random; public class Hwk5 { public static void main(String[] args) { int numOfGates = 5; Gym gym = new Gym(nu
Templates Hwk5.java package hwk5; import java.util.Random; public class Hwk5 { public static void main(String[] args) { int numOfGates = 5; Gym gym = new Gym(nu
Templates Hwk5.java package hwk5; import java.util.Random; public class Hwk5 { public static void main(String[] args) { int numOfGates = 5; Gym gym = new Gym(nu
Templates Hwk5.java package hwk5; import java.util.Random; public class Hwk5 { public static void main(String[] args) { int numOfGates = 5; Gym gym = new Gym(nu
Templates Hwk5.java package hwk5; import java.util.Random; public class Hwk5 { public static void main(String[] args) { int numOfGates = 5; Gym gym = new Gym(nu
Templates Hwk5.java package hwk5; import java.util.Random; public class Hwk5 { public static void main(String[] args) { int numOfGates = 5; Gym gym = new Gym(nu
Templates Hwk5.java package hwk5; import java.util.Random; public class Hwk5 { public static void main(String[] args) { int numOfGates = 5; Gym gym = new Gym(nu
Templates Hwk5.java package hwk5; import java.util.Random; public class Hwk5 { public static void main(String[] args) { int numOfGates = 5; Gym gym = new Gym(nu
Templates Hwk5.java package hwk5; import java.util.Random; public class Hwk5 { public static void main(String[] args) { int numOfGates = 5; Gym gym = new Gym(nu
Templates Hwk5.java package hwk5; import java.util.Random; public class Hwk5 { public static void main(String[] args) { int numOfGates = 5; Gym gym = new Gym(nu
Templates Hwk5.java package hwk5; import java.util.Random; public class Hwk5 { public static void main(String[] args) { int numOfGates = 5; Gym gym = new Gym(nu
Templates Hwk5.java package hwk5; import java.util.Random; public class Hwk5 { public static void main(String[] args) { int numOfGates = 5; Gym gym = new Gym(nu
Templates Hwk5.java package hwk5; import java.util.Random; public class Hwk5 { public static void main(String[] args) { int numOfGates = 5; Gym gym = new Gym(nu
Templates Hwk5.java package hwk5; import java.util.Random; public class Hwk5 { public static void main(String[] args) { int numOfGates = 5; Gym gym = new Gym(nu
Templates Hwk5.java package hwk5; import java.util.Random; public class Hwk5 { public static void main(String[] args) { int numOfGates = 5; Gym gym = new Gym(nu
Templates Hwk5.java package hwk5; import java.util.Random; public class Hwk5 { public static void main(String[] args) { int numOfGates = 5; Gym gym = new Gym(nu
Templates Hwk5.java package hwk5; import java.util.Random; public class Hwk5 { public static void main(String[] args) { int numOfGates = 5; Gym gym = new Gym(nu
Templates Hwk5.java package hwk5; import java.util.Random; public class Hwk5 { public static void main(String[] args) { int numOfGates = 5; Gym gym = new Gym(nu
Templates Hwk5.java package hwk5; import java.util.Random; public class Hwk5 { public static void main(String[] args) { int numOfGates = 5; Gym gym = new Gym(nu
Templates Hwk5.java package hwk5; import java.util.Random; public class Hwk5 { public static void main(String[] args) { int numOfGates = 5; Gym gym = new Gym(nu
Templates Hwk5.java package hwk5; import java.util.Random; public class Hwk5 { public static void main(String[] args) { int numOfGates = 5; Gym gym = new Gym(nu
Templates Hwk5.java package hwk5; import java.util.Random; public class Hwk5 { public static void main(String[] args) { int numOfGates = 5; Gym gym = new Gym(nu
Templates Hwk5.java package hwk5; import java.util.Random; public class Hwk5 { public static void main(String[] args) { int numOfGates = 5; Gym gym = new Gym(nu
Templates Hwk5.java package hwk5; import java.util.Random; public class Hwk5 { public static void main(String[] args) { int numOfGates = 5; Gym gym = new Gym(nu

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site