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.
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.
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 2 steps with 1 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Concepts of Database Management](https://www.bartleby.com/isbn_cover_images/9781337093422/9781337093422_smallCoverImage.gif)
![Prelude to Programming](https://www.bartleby.com/isbn_cover_images/9780133750423/9780133750423_smallCoverImage.jpg)
![Sc Business Data Communications and Networking, T…](https://www.bartleby.com/isbn_cover_images/9781119368830/9781119368830_smallCoverImage.gif)