Advertisement
cd62131

qa8310825 light system for you

Oct 18th, 2013
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.64 KB | None | 0 0
  1. // test class
  2. public class QA8310825ForYou {
  3.   public static void main(String[] args) {
  4.     LightSystemForYou system = new LightSystemForYou();
  5.     system.showState();
  6.     system.turnSwich1(); system.showState();
  7.     system.turnSwich1(); system.showState();
  8.     system.turnSwich2(); system.showState();
  9.     system.turnSwich3(); system.showState();
  10.     system.turnSwich2(); system.showState();
  11.     system.turnSwich1(); system.showState();
  12.   }
  13. }
  14. class LightSystemForYou {
  15.   private static final int floor = 3;
  16.   private int[] swiches = new int[floor];
  17.   private int lamp;
  18.  
  19.   public LightSystemForYou() {
  20.     for (int i = 0; i < floor; i++) {
  21.       this.swiches[i] = 0; // Left: 0, Right: 1
  22.     }
  23.     this.lamp = 0; // Off: 0, On: 1
  24.   }
  25.  
  26.   public void showState() {
  27.     System.out.print("Lamp: ");
  28.     if (this.lamp == 0) {
  29.       System.out.print("Off");
  30.     }
  31.     else {
  32.       System.out.print("On");
  33.     }
  34.     System.out.print(", swiches: [");
  35.     for (int i = 0; i < floor; i++) {
  36.       if (swiches[i] == 0) {
  37.         System.out.print("Left");
  38.       }
  39.       else {
  40.         System.out.print("Right");
  41.       }
  42.       if (i < floor - 1) {
  43.         System.out.print(", ");
  44.       }
  45.     }
  46.     System.out.println("]");
  47.   }
  48.  
  49.   private void turn(int i) {
  50.     if (this.swiches[i] == 0) {
  51.       this.swiches[i] = 1;
  52.     }
  53.     else {
  54.       this.swiches[i] = 0;
  55.     }
  56.     if (this.lamp == 0) {
  57.       this.lamp = 1; return;
  58.     }
  59.     this.lamp = 0;
  60.   }
  61.  
  62.   public void turnSwich1() {
  63.     this.turn(0);
  64.   }
  65.  
  66.   public void turnSwich2() {
  67.     this.turn(1);
  68.   }
  69.  
  70.   public void turnSwich3() {
  71.     this.turn(2);
  72.   }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement