Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // test class
- public class QA8310825ForYou {
- public static void main(String[] args) {
- LightSystemForYou system = new LightSystemForYou();
- system.showState();
- system.turnSwich1(); system.showState();
- system.turnSwich1(); system.showState();
- system.turnSwich2(); system.showState();
- system.turnSwich3(); system.showState();
- system.turnSwich2(); system.showState();
- system.turnSwich1(); system.showState();
- }
- }
- class LightSystemForYou {
- private static final int floor = 3;
- private int[] swiches = new int[floor];
- private int lamp;
- public LightSystemForYou() {
- for (int i = 0; i < floor; i++) {
- this.swiches[i] = 0; // Left: 0, Right: 1
- }
- this.lamp = 0; // Off: 0, On: 1
- }
- public void showState() {
- System.out.print("Lamp: ");
- if (this.lamp == 0) {
- System.out.print("Off");
- }
- else {
- System.out.print("On");
- }
- System.out.print(", swiches: [");
- for (int i = 0; i < floor; i++) {
- if (swiches[i] == 0) {
- System.out.print("Left");
- }
- else {
- System.out.print("Right");
- }
- if (i < floor - 1) {
- System.out.print(", ");
- }
- }
- System.out.println("]");
- }
- private void turn(int i) {
- if (this.swiches[i] == 0) {
- this.swiches[i] = 1;
- }
- else {
- this.swiches[i] = 0;
- }
- if (this.lamp == 0) {
- this.lamp = 1; return;
- }
- this.lamp = 0;
- }
- public void turnSwich1() {
- this.turn(0);
- }
- public void turnSwich2() {
- this.turn(1);
- }
- public void turnSwich3() {
- this.turn(2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement