class Bug { private final int orgPosition = 0; private int currentPosition; private boolean direction; public Bug() { direction = true; currentPosition = orgPosition; } public void move() { if...


class Bug {
    private final int orgPosition = 0;
    private int currentPosition;
    private boolean direction;


    public Bug() {
        direction = true;
        currentPosition = orgPosition;
    }


    public void move() {
        if (direction) {
            ++currentPosition;
        } else {
            --currentPosition;
        }
    }


    public void turn() {
        direction = !direction;
    }


    public int getPosition() {
        return currentPosition;
    }


    public boolean getDirection() {
        return direction;
    }


    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("Position = " + currentPosition + ", Direction = ");
        sb.append(direction ? "RIGHT" : "LEFT");
        return sb.toString();
    }


}


-------------------------------------TestDriver.java-------------------------------------


import java.util.Scanner;


class TestDriver {
    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        Bug b = new Bug();
        int choice = 0;
        do {
            System.out.print("Select an action: \n1. Move\n2. Turn\n3. Output\n4. Exit\nEnter your choice : ");
            choice = input.nextInt();
            System.out.println();
            switch (choice) {
            case 1:
                b.move();
                System.out.println("Moved 1 unit towards " + (b.getDirection() ? "RIGHT" : "LEFT") + '\n');
                break;
            case 2:
                b.turn();
                System.out.println("Reversed direction\n");
                break;
            case 3:
                System.out.println(b.toString() + '\n');
                break;
            default:
                break;
            }


        } while (choice != 4);
    }
}


Please Can someone explained to me this code in the form of cooments so as to give me an inside to why it is designed this way.Also what will be be the UML diagram for the above code.



Jun 04, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here