Follow the instructions and templates please Instructions 1

Follow the instructions and templates, please.

Instructions

1 What to implement?
For this homework, you will implement a class Gym to simulate the Gymnasium using the Gate class. Please read the comments in the le Gym.java for the requirements of the methods.
2 What is provided
We provide a driver class Hwk4.java and a template Gym.java for the Gym class. You should use your solution to the class Gate.
3 What does output look like?
If you run the driver class Hwk4.java, you will see output that looks like the followings (due to random simulation, the actual numbers may dier):
After 5000 simulations: 482 persons are in the gym
Gate gate_0 is open to get in. 518 persons came in through this gate
Gate gate_1 is open to get in. 508 persons came in through this gate
Gate gate_2 is open to get in. 486 persons came in through this gate
Gate gate_3 is open to get out. 521 persons went out through this gate
Gate gate_4 is open to get out. 509 persons went out through this gate


Templates

Hwk4.java

package hwk4;

import java.util.Random;

public class Hwk4 {

   public static void main(String[] args) {
       int numOfGates = 5;
      
       Gym gym = new Gym(numOfGates);
       gym.open(3);
      
       int numOfSimulations = 5000;

       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);
           }
       }
       String output = String.format(\"After %d simulations: %d persons are in the gym\ \",
               numOfSimulations,
               gym.personsInside());
      
       System.out.printf(\"%s\ %s\", output, gym);
   }

}

Gate.java

package hwk4;

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 hwk4;

public class Gym {
   final Gate[] gates; // array of gates
   final int capacity; // capacity of the gym
  
   // initialize with number of gates set to 3 and capacity set to 1000
   public Gym() {
       // TODO
   }
   // initialize with specified number of gates and capacity set to 1000
   public Gym(int numOfGates) {
       // TODO
   }
   // initialize with specified number of gates and with specified capacity
   public Gym(int numOfGates, int capacity) {
       // TODO
   }
  
   // lock all gates
   public void lockDown() {
       // TODO
   }
  
   // open (roughly) half of the gates for in and the other half for out
   public void open() {
       // TODO
   }

   // 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) {
       // 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) {
       // TODO
   }
  
   // 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) {
       // TODO
   }
   // returns true if and only if capacity is not reached
   public boolean canGoIn() {
       // TODO
   }
   // returns true if and only if it is not empty
   public boolean canGoOut() {
       // TODO
   }
   // return the number of persons inside the gym
   public int personsInside() {
       // TODO
   }
  
   // print each gate of the gym
   @Override
   public String toString() {
       // TODO
   }
}

Solution

Please find the code for the above question as follows:-

Gym.java

package com.chegg.gym;

public class Gym {
   final Gate[] gates; // array of gates
   final int capacity; // capacity of the gym

   // initialize with number of gates set to 3 and capacity set to 1000
   public Gym() {
       // TODO
       capacity = 1000;
       gates = new Gate[3];
       for(int i=0; i<gates.length; i++){
           gates[i] = new Gate(\"gate_\"+i);
       }
   }

   // initialize with specified number of gates and capacity set to 1000
   public Gym(int numOfGates) {
       // TODO
       capacity = 1000;
       gates = new Gate[numOfGates];
       for(int i=0; i<gates.length; i++){
           gates[i] = new Gate(\"gate_\"+i);
       }
   }

   // initialize with specified number of gates and with specified capacity
   public Gym(int numOfGates, int capacity) {
       // TODO
       this.capacity = capacity;
       gates = new Gate[numOfGates];
       for(int i=0; i<gates.length; i++){
           gates[i] = new Gate(\"gate_\"+i);
       }
   }

   // lock all gates
   public void lockDown() {
       // TODO
       for (int i = 0; i < gates.length; i++) {
           gates[i].close();
       }
   }

   // open (roughly) half of the gates for in and the other half for out
   public void open() {
       // TODO
       for (int i = 0; i < gates.length / 2; i++) {
           gates[i].open(State.IN);
       }
       for (int i = gates.length / 2 + 1; i < gates.length; i++) {
           gates[i].open(State.OUT);
       }
   }

   // 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) {
       // TODO
       if (numOfInGate >= gates.length) {
           int i;
           for (i = 0; i < gates.length - 1; i++) {
               gates[i].setTripod(State.IN);
           }
           gates[i].setTripod(State.OUT);
       } else if (numOfInGate <= 0) {
           int i;
           for (i = 0; i < gates.length; i++) {
               gates[i].setTripod(State.OUT);
           }
           gates[i].setTripod(State.IN);
       } else{
           int i;
           for (i = 0; i < numOfInGate; i++) {
               gates[i].setTripod(State.IN);
           }
           for(;i<gates.length;i++){
               gates[i].setTripod(State.OUT);
           }
       }
   }

   // 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) {
       // TODO
       if (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) {
       // TODO
       if (gateNumber <= gates.length) {
           gates[gateNumber].getOut(1);
       }
   }

   // returns true if and only if capacity is not reached
   public boolean canGoIn() {
   // TODO
       int numPersonsInside = 0;
       for(int i=0; i<gates.length; i++){
           numPersonsInside += gates[i].numPersonGoneInside();
       }
       if(numPersonsInside<capacity){
           return true;
       }else
           return false;
   }

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

   // return the number of persons inside the gym
   public int personsInside() {
       // TODO
       int numPersonsInside = 0;
       for(int i=0; i<gates.length; i++){
           numPersonsInside += gates[i].numPersonGoneInside();
       }
       if(numPersonsInside>0){
           return numPersonsInside;
       }else
           return 0;
   }

   // print each gate of the gym
   @Override
   public String toString() {
       // TODO
       String gateStatus = \"\";
       for(int i=0; i<gates.length; i++){
           gateStatus += gates[i].toString();
       }
       return gateStatus;
   }
}

Follow the instructions and templates, please. Instructions 1 What to implement? For this homework, you will implement a class Gym to simulate the Gymnasium usi
Follow the instructions and templates, please. Instructions 1 What to implement? For this homework, you will implement a class Gym to simulate the Gymnasium usi
Follow the instructions and templates, please. Instructions 1 What to implement? For this homework, you will implement a class Gym to simulate the Gymnasium usi
Follow the instructions and templates, please. Instructions 1 What to implement? For this homework, you will implement a class Gym to simulate the Gymnasium usi
Follow the instructions and templates, please. Instructions 1 What to implement? For this homework, you will implement a class Gym to simulate the Gymnasium usi
Follow the instructions and templates, please. Instructions 1 What to implement? For this homework, you will implement a class Gym to simulate the Gymnasium usi

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site